Answered by:
Help: C# WMI InvokeMethod always failure

Question
-
function code:
public static void RenameHostname(string hostname)
{ManagementClass MC= new ManagementClass("Win32_ComputerSystem");
ManagementBaseObject inParams = MC.GetMethodParameters("rename");
inParams["Name"] = hostname.Trim();
ManagementBaseObject outParams = MC.InvokeMethod("rename", inParams, null);
}the purpose of this code is to rename computer name, but it always prompt out "Invalid method Parameter(s)" on last line.
When I try to get "inParams" in Immediate Windows
show as below:
?inParams
{System.Management.ManagementBaseObject}
base {System.ComponentModel.Component}: {System.Management.ManagementBaseObject}
ClassPath: Function evaluation timed out.
Properties: Function evaluation disabled because a previous function evaluation timed out. You must continue execution to reenable function evaluation.
Qualifiers: Function evaluation disabled because a previous function evaluation timed out. You must continue execution to reenable function evaluation.
SystemProperties: Function evaluation disabled because a previous function evaluation timed out. You must continue execution to reenable function evaluation.Is there any one could help ?
夏天
Saturday, December 29, 2012 1:13 AM
Answers
-
var mc = new ManagementClass("Win32_ComputerSystem"); var p = mc.GetMethodParameters("rename"); foreach (var v in p.Properties) Console.WriteLine(v.Name);
Reveals 3 parameters.
Name
Password
UserNameThis signature unintentionally left blank.
- Marked as answer by Bob Shen Tuesday, January 8, 2013 2:24 AM
Saturday, December 29, 2012 5:02 AM -
According to the description of InvokeMethod and Win32_ComputerSystem, you can consider an alternative way: constructing a new array consisting of new computer name, password and username. Sometimes password and username can be null. So try this:
object [] p = new [] { hostname.Trim(), null, null};
ManagementBaseObject outParams = MC.InvokeMethod("Rename", p);
If it does not work, then maybe it expects a username and password?
- Marked as answer by Bob Shen Tuesday, January 8, 2013 2:24 AM
Saturday, December 29, 2012 11:31 AM
All replies
-
var mc = new ManagementClass("Win32_ComputerSystem"); var p = mc.GetMethodParameters("rename"); foreach (var v in p.Properties) Console.WriteLine(v.Name);
Reveals 3 parameters.
Name
Password
UserNameThis signature unintentionally left blank.
- Marked as answer by Bob Shen Tuesday, January 8, 2013 2:24 AM
Saturday, December 29, 2012 5:02 AM -
According to the description of InvokeMethod and Win32_ComputerSystem, you can consider an alternative way: constructing a new array consisting of new computer name, password and username. Sometimes password and username can be null. So try this:
object [] p = new [] { hostname.Trim(), null, null};
ManagementBaseObject outParams = MC.InvokeMethod("Rename", p);
If it does not work, then maybe it expects a username and password?
- Marked as answer by Bob Shen Tuesday, January 8, 2013 2:24 AM
Saturday, December 29, 2012 11:31 AM