Win32_CompurterSystem: Rename, JoinDomainOrWorkgroup, UnjoinDomainOrWorkgroup - HELP
-
In C#, using WMI has been great for access SI data. For reasons still unbeknownst to me, I cannot get a ManagementBaseObject instance (lets's call it mob) to properly invoke any of these function using InvokeMethod. For example:
mof = "Rename";
name = "NEWNAME";
password = "adminpw";
user = "administrator";object[] args = new args[3];
args[0] = name;
args[1] = password;
args[1] = user;ManagementBaseObject mob = new ManagementBaseObject();
long retval = (long) mob.InvokeMethod(mof, name, password, user);if (retval == 0)
Console.Writeline("Reboot PC for RENAME to take effect");
else
Console.Writeline("RENAME error {0}");Everytime I get Invalid Method Parameter(s) message. If anyone knows how to succcessfully call Rename, JoinDomainOrWorkgroup, etc from C#, please share the technique. Thanks
-
In C#, using WMI has been great for access SI data. For reasons still unbeknownst to me, I cannot get a ManagementBaseObject instance (lets's call it mob) to properly invoke any of these function using InvokeMethod. For example:
mof = "Rename";
name = "NEWNAME";
password = "adminpw";
user = "administrator";object[] args = new args[3];
args[0] = name;
args[1] = password;
args[1] = user;ManagementBaseObject mob = new ManagementBaseObject();
long retval = (long) mob.InvokeMethod(mof, name, password, user);if (retval == 0)
Console.Writeline("Reboot PC for RENAME to take effect");
else
Console.Writeline("RENAME error {0}");Everytime I get Invalid Method Parameter(s) message. If anyone knows how to succcessfully call Rename, JoinDomainOrWorkgroup, etc from C#, please share the technique. Thanks
-
The examples on MSDN are for Win32_Process and they work when I test. For some reason, Win32_ComputerSystem is behaving differently. I think there must a different way of calling the Win32_ComputerSystem functions. Here is the exact code that yields this result every time! :wtf: Exception caught!!! System.Management: Invalid method Parameter(s)
public bool RenameComputer(string name, string password, string user) { bool ok = true; string mofName = "Rename"; try { //Get an input parameters object for this method ManagementClass processClass = new ManagementClass("Win32\_ComputerSystem"); object\[\] methodArgs = { name, password, user}; for (int i = 0; i < methodArgs.Length; ++i) Console.WriteLine("methodArgs\[{0}\] = {1}", i, methodArgs\[i\]); object retval = processClass.InvokeMethod(mofName, methodArgs); Console.WriteLine("InvokeMethod Status {0}, Process ID {1}", retval, methodArgs\[3\]); } catch (Exception e) { Console.WriteLine("Exception caught!!!"); Console.WriteLine("{0}: {1}", e.Source, e.Message); ok = false; } return (ok); }
Has anyone been sucessful in calling Win32_ComputerSystem InvokeMethod() for Rename, or JoinDomainOrWorkgroup or UnjoinDomainOrWorkgroup. Help is greatly appreciated by me and anyone else dealing with this challenge.
-
The examples on MSDN are for Win32_Process and they work when I test. For some reason, Win32_ComputerSystem is behaving differently. I think there must a different way of calling the Win32_ComputerSystem functions. Here is the exact code that yields this result every time! :wtf: Exception caught!!! System.Management: Invalid method Parameter(s)
public bool RenameComputer(string name, string password, string user) { bool ok = true; string mofName = "Rename"; try { //Get an input parameters object for this method ManagementClass processClass = new ManagementClass("Win32\_ComputerSystem"); object\[\] methodArgs = { name, password, user}; for (int i = 0; i < methodArgs.Length; ++i) Console.WriteLine("methodArgs\[{0}\] = {1}", i, methodArgs\[i\]); object retval = processClass.InvokeMethod(mofName, methodArgs); Console.WriteLine("InvokeMethod Status {0}, Process ID {1}", retval, methodArgs\[3\]); } catch (Exception e) { Console.WriteLine("Exception caught!!!"); Console.WriteLine("{0}: {1}", e.Source, e.Message); ok = false; } return (ok); }
Has anyone been sucessful in calling Win32_ComputerSystem InvokeMethod() for Rename, or JoinDomainOrWorkgroup or UnjoinDomainOrWorkgroup. Help is greatly appreciated by me and anyone else dealing with this challenge.
Win32_ComputerSystem.Rename is an instance method so you first need to get the Win32_ComputerSystem instance to invoke it on:
ManagementClass computerSystemClass =
new ManagementClass("Win32_ComputerSystem");object[] methodArgs = { name, password, user };
foreach (ManagementObject computerSystem in computerSystemClass.GetInstances())
{
object retval = computerSystem.InvokeMethod(mofName, methodArgs);
}computerSystemClass.GetInstances() will return only one instance but you still need to use foreach. To avoid it, you could use the ManagementObject constructor that accepts the object path to get the instance:
ManagementObject computerSystem =
new ManagementObject("Win32_ComputerSystem.Name='SomeName'");but you would have to get the computer name that you want to change first.
In January you said "Money in April" - That was two years ago! B. Python
-
Win32_ComputerSystem.Rename is an instance method so you first need to get the Win32_ComputerSystem instance to invoke it on:
ManagementClass computerSystemClass =
new ManagementClass("Win32_ComputerSystem");object[] methodArgs = { name, password, user };
foreach (ManagementObject computerSystem in computerSystemClass.GetInstances())
{
object retval = computerSystem.InvokeMethod(mofName, methodArgs);
}computerSystemClass.GetInstances() will return only one instance but you still need to use foreach. To avoid it, you could use the ManagementObject constructor that accepts the object path to get the instance:
ManagementObject computerSystem =
new ManagementObject("Win32_ComputerSystem.Name='SomeName'");but you would have to get the computer name that you want to change first.
In January you said "Money in April" - That was two years ago! B. Python