473,320 Members | 1,846 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,320 software developers and data experts.

Why does this WMI Invoke not work?

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 Grüsse / Best regards / Votre bien devoue
Kerem Gümrükcü
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
1 4424
"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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

31
by: Prabhat | last post by:
Hi All, The "Session_OnStart" event doesnot fire if the website has .htm file as the startup document. If I change the extension of the default document from ..htm to .asp then I can see the...
11
by: Michi Henning | last post by:
Hi, I'm using a blocking Select() call on a socket with a timeout value of -1. I'd expect the call to block indefinitely, but it doesn't. When I use Poll() instead, a timeout of -1 works fine...
2
by: rawCoder | last post by:
Hi I am having this InvalidOperationException with message Cannot call Invoke or InvokeAsync on a control until the window handle has been created This is raised when i try to invoke a method...
19
by: trint | last post by:
Ok, I start my thread job: Thread t = new Thread(new ThreadStart(invoicePrintingLongRunningCodeThread)); t.IsBackground = true; t.Start(); There are lots of calls to controls and many...
1
by: Tommy Svensson \(InfoGrafix\) | last post by:
Hi, Can I use P/Invoke to obtain a C++ class type contained in a dll? I know how to get structs and how to pass classes as parameters to native code but how do I get C++ unmanaged classes from a...
7
by: stephan querengaesser | last post by:
hi ng, i try to invoke a webservice-method with an filter-object, that contains value types. if i don´t want to filter the return value of the method, i have to pass a new instance of the...
1
by: Steve | last post by:
I need to update my UI from a Process or worker thread. I did some readinf and basically ended up adapting an MS example to fot my needs. It all made sense until I tried it :) My process...
89
by: Cuthbert | last post by:
After compiling the source code with gcc v.4.1.1, I got a warning message: "/tmp/ccixzSIL.o: In function 'main';ex.c: (.text+0x9a): warning: the 'gets' function is dangerous and should not be...
1
by: =?ISO-8859-1?Q?Lasse_V=E5gs=E6ther_Karlsen?= | last post by:
I get the above error in some of the ASP.NET web applications on a server, and I need some help figuring out how to deal with it. This is a rather long post, and I hope I have enough details that...
2
by: Adam Benson | last post by:
Hi, If you're not on the UI thread - responding to a network event, say - and you want to read the SelectedIndex of a combo box, do you have to do an Invoke? Or is it just a simple data read...
0
by: DolphinDB | last post by:
The formulas of 101 quantitative trading alphas used by WorldQuant were presented in the paper 101 Formulaic Alphas. However, some formulas are complex, leading to challenges in calculation. Take...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.