Why does this WMI Invoke not work?

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • =?iso-8859-1?B?S2VyZW0gR/xtcvxrY/w=?=

    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(st ring 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 ManagementBaseO bject
    ExecuteWMIMetho dOnRemoteMachin e_ThrowsExcepti on(string TargetMachine,
    string ManagementPathS tring,
    string MethodName,
    WMIParameter[] inParams, string UserName, string Password)
    {
    try
    {
    ConnectionOptio ns connOptions = new ConnectionOptio ns();
    connOptions.Ena blePrivileges = true;
    connOptions.Imp ersonation = ImpersonationLe vel.Impersonate ;
    if (UserName != string.Empty && Password != string.Empty)
    {
    connOptions.Use rname = UserName;
    connOptions.Sec urePassword = CreateSecureStr ing(Password);
    }
    ManagementScope manScope = new
    ManagementScope (String.Format( @"\\{0}\ROOT\CI MV2", TargetMachine),
    connOptions);
    manScope.Connec t();
    ObjectGetOption s objectGetOption s = new ObjectGetOption s();
    ManagementPath managementPath = new ManagementPath( ManagementPathS tring);
    ManagementClass processClass = new ManagementClass (manScope, managementPath,
    objectGetOption s);
    ManagementBaseO bject Params = processClass.Ge tMethodParamete rs(MethodName);
    foreach (WMIParameter wmip in inParams)
    {
    Params[wmip.Name] = Convert.ChangeT ype(wmip.Value, wmip.Type);
    }
    ManagementBaseO bject outParams;
    if (inParams == null)
    {
    outParams = processClass.In vokeMethod(Meth odName, null, null);
    }
    else
    {
    outParams = processClass.In vokeMethod(Meth odName, 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("R eserved", typeof(Int32), 0);
    wmip[1] = new WMIParameter("F lags", typeof(Int32), (Int32)ew);

    ExecuteWMIMetho dOnRemoteMachin e_ThrowsExcepti on(TargetMachin eName,
    "Win32_Operatin gSystem",
    "Win32Shutdown" ,
    wmip,
    TargetMachineUs erName,
    TargetMachineUs erPassword);



    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."

  • =?Utf-8?B?dXJrZWM=?=

    #2
    RE: Why does this WMI Invoke not work?

    "Kerem Gümrükcü" wrote:
    Hi,
    >
    can someone please tell me why the **** this does not
    work as expected:
    >
    private static ManagementBaseO bject
    ExecuteWMIMetho dOnRemoteMachin e_ThrowsExcepti on(string TargetMachine,
    string ManagementPathS tring,
    string MethodName,
    WMIParameter[] inParams, string UserName, string Password)
    {
    try
    {
    ConnectionOptio ns connOptions = new ConnectionOptio ns();
    connOptions.Ena blePrivileges = true;
    connOptions.Imp ersonation = ImpersonationLe vel.Impersonate ;
    if (UserName != string.Empty && Password != string.Empty)
    {
    connOptions.Use rname = UserName;
    connOptions.Sec urePassword = CreateSecureStr ing(Password);
    }
    ManagementScope manScope = new
    ManagementScope (String.Format( @"\\{0}\ROOT\CI MV2", TargetMachine),
    connOptions);
    manScope.Connec t();
    ObjectGetOption s objectGetOption s = new ObjectGetOption s();
    ManagementPath managementPath = new ManagementPath( ManagementPathS tring);
    ManagementClass processClass = new ManagementClass (manScope, managementPath,
    objectGetOption s);
    ManagementBaseO bject Params = processClass.Ge tMethodParamete rs(MethodName);
    foreach (WMIParameter wmip in inParams)
    {
    Params[wmip.Name] = Convert.ChangeT ype(wmip.Value, wmip.Type);
    }
    ManagementBaseO bject outParams;
    if (inParams == null)
    {
    outParams = processClass.In vokeMethod(Meth odName, null, null);
    }

    Win32_Operating System.Win32Shu tdown() is an instance method and you are
    invoking it on a class (processClass). Here is a sample:


    ManagementClass OSClass =
    new ManagementClass ("Win32_Operati ngSystem");

    ManagementBaseO bject inParams =
    OSClass.Methods["Win32Shutd own"].InParameters;

    inParams["Flags"] = 0;

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

    foreach (ManagementObje ct OSInstance in OSClass.GetInst ances())
    {
    OSInstance.Invo keMethod("Win32 Shutdown", 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

    Comment

    Working...