Why does this WMI Invoke not work?

K

Kerem Gümrükcü

Hi,

can someone please tell me why the **** this does not
work as expected:


First at all, thats what a WMI parameter looks like:

public class WMIParameter {

private string pName;
private Type pType;
private object pValue;

public WMIParameter(string Name, Type Type, object Value) {
this.pName = Name;
this.pValue = Value;
this.pType = Type;
}
public Type Type {
get {
return this.pType;
}
}
public string Name
{
get
{
return this.pName;
}
}
public object Value
{
get
{
return this.pValue;
}
}
}


private static ManagementBaseObject
ExecuteWMIMethodOnRemoteMachine_ThrowsException(string TargetMachine,
string ManagementPathString,
string MethodName,
WMIParameter[] inParams, string UserName, string Password)
{
try
{
ConnectionOptions connOptions = new ConnectionOptions();
connOptions.EnablePrivileges = true;
connOptions.Impersonation = ImpersonationLevel.Impersonate;
if (UserName != string.Empty && Password != string.Empty)
{
connOptions.Username = UserName;
connOptions.SecurePassword = CreateSecureString(Password);
}
ManagementScope manScope = new
ManagementScope(String.Format(@"\\{0}\ROOT\CIMV2", TargetMachine),
connOptions);
manScope.Connect();
ObjectGetOptions objectGetOptions = new ObjectGetOptions();
ManagementPath managementPath = new ManagementPath(ManagementPathString);
ManagementClass processClass = new ManagementClass(manScope, managementPath,
objectGetOptions);
ManagementBaseObject Params = processClass.GetMethodParameters(MethodName);
foreach (WMIParameter wmip in inParams)
{
Params[wmip.Name] = Convert.ChangeType(wmip.Value,wmip.Type);
}
ManagementBaseObject outParams;
if (inParams == null)
{
outParams = processClass.InvokeMethod(MethodName, null, null);
}
else
{
outParams = processClass.InvokeMethod(MethodName, Params, null);
}
return outParams;
}
catch (Exception e)
{
throw e;
}
}

I always get a error saying "The Methods Parameter are invalid".
Thats how i call this:

WMIParameter[] wmip = new WMIParameter[2];
wmip[0] = new WMIParameter("Reserved", typeof(Int32), 0);
wmip[1] = new WMIParameter("Flags", typeof(Int32), (Int32)ew);

ExecuteWMIMethodOnRemoteMachine_ThrowsException(TargetMachineName,
"Win32_OperatingSystem",
"Win32Shutdown",
wmip,
TargetMachineUserName,
TargetMachineUserPassword);



I want to hold the call to the WMI methods as transparent as possible, so
hats why i wrote this function. Please can you show me, or if possible
correct this function,...


TIA,...

Regards

Kerem


--
 
U

urkec

Kerem Gümrükcü said:
Hi,

can someone please tell me why the **** this does not
work as expected:

private static ManagementBaseObject
ExecuteWMIMethodOnRemoteMachine_ThrowsException(string TargetMachine,
string ManagementPathString,
string MethodName,
WMIParameter[] inParams, string UserName, string Password)
{
try
{
ConnectionOptions connOptions = new ConnectionOptions();
connOptions.EnablePrivileges = true;
connOptions.Impersonation = ImpersonationLevel.Impersonate;
if (UserName != string.Empty && Password != string.Empty)
{
connOptions.Username = UserName;
connOptions.SecurePassword = CreateSecureString(Password);
}
ManagementScope manScope = new
ManagementScope(String.Format(@"\\{0}\ROOT\CIMV2", TargetMachine),
connOptions);
manScope.Connect();
ObjectGetOptions objectGetOptions = new ObjectGetOptions();
ManagementPath managementPath = new ManagementPath(ManagementPathString);
ManagementClass processClass = new ManagementClass(manScope, managementPath,
objectGetOptions);
ManagementBaseObject Params = processClass.GetMethodParameters(MethodName);
foreach (WMIParameter wmip in inParams)
{
Params[wmip.Name] = Convert.ChangeType(wmip.Value,wmip.Type);
}
ManagementBaseObject outParams;
if (inParams == null)
{
outParams = processClass.InvokeMethod(MethodName, null, null);
}


Win32_OperatingSystem.Win32Shutdown() is an instance method and you are
invoking it on a class (processClass). Here is a sample:


ManagementClass OSClass =
new ManagementClass("Win32_OperatingSystem");

ManagementBaseObject inParams =
OSClass.Methods["Win32Shutdown"].InParameters;

inParams["Flags"] = 0;

// This line will throw invalid method parameters exception:
//
//OS.InvokeMethod("Win32Shutdown", inParams, null);
//

foreach (ManagementObject OSInstance in OSClass.GetInstances())
{
OSInstance.InvokeMethod("Win32Shutdown", inParams, null);
}


This code works for me locally to log off the current user. There is no need
to set the Reserved parameter as it defaults to 0 and is ignored. I don't
know if there are other issues with your code.

Hope this helps.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top