By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
455,566 Members | 1,783 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 455,566 IT Pros & Developers. It's quick & easy.

Why does this WMI Invoke not work?

P: n/a
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(st ring 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(Ta rgetMachineName,
"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
--
-----------------------
Beste Grsse / Best regards / Votre bien devoue
Kerem Gmrkc
Latest Project: http://www.codeplex.com/restarts
Latest Open-Source Projects: http://entwicklung.junetz.de
-----------------------
"This reply is provided as is, without warranty express or implied."

Jun 27 '08 #1
Share this Question
Share on Google+
1 Reply


P: n/a
"Kerem Gümrükcü" wrote:
Hi,

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

private static ManagementBaseObject
ExecuteWMIMethodOnRemoteMachine_ThrowsException(st ring 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.

--
urkec
Jun 27 '08 #2

This discussion thread is closed

Replies have been disabled for this discussion.