Why does this WMI Invoke not work?
- From: Kerem Gümrükcü <kareem114@xxxxxxxxxxx>
- Date: Thu, 26 Jun 2008 01:04:26 +0200
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
--
----------------------- 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."
.
- Follow-Ups:
- RE: Why does this WMI Invoke not work?
- From: urkec
- RE: Why does this WMI Invoke not work?
- Prev by Date: Re: Regex Usage, or use Substring?
- Next by Date: Re: C# additional visual components.
- Previous by thread: NSAP Address (ISO TP4)
- Next by thread: RE: Why does this WMI Invoke not work?
- Index(es):
Relevant Pages
|