tags:

views:

281

answers:

1

Basically I am new to WMI and I can't figure out what I am missing here or how to debug this...

    ConnectionOptions connectOptions = new ConnectionOptions();
    connectOptions.Username = user;
    connectOptions.Password = password;
    machine = "remoteMachine.com";

    ManagementScope scope = new ManagementScope(
        @"\\" + machine + @"\root\cimv2",
        connectOptions);

    scope.Connect();

    ManagementPath path = new ManagementPath(@"\\" + machine + @"\root\cimv2\Win23_Process");

    ManagementClass proc = new ManagementClass(scope, path, new ObjectGetOptions());

    ManagementBaseObject args = proc.GetMethodParameters("Create");

That last line, where I call a method on proc, causes this InvalidOperationException... doesn't tell me too much :( I get the same result when calling other methods on proc, such as GetInstances(). When debugging through the code, it does appear that I can connect to the remote machine successfully.

System.InvalidOperationException was unhandled
  Message="Operation is not valid due to the current state of the object."
  Source="System.Management"
  StackTrace:
       at System.Management.ManagementObject.GetMethodParameters(String methodName, ManagementBaseObject& inParameters, IWbemClassObjectFreeThreaded& inParametersClass, IWbemClassObjectFreeThreaded& outParametersClass)
       at System.Management.ManagementObject.GetMethodParameters(String methodName)
+1  A: 

You've got a typo in your sample code, not sure if it's in your real code or not. You've got Win23_Process, which should be Win32_Process. Also Create for Win32_Process needs arguments such as command-line, directory, etc.

Ted Elliott
Well that is a major fail :( +1 for pair programming!
RichAmberale
That is 1/2 the answer. The other problem is that I needed to use a relative path for ManagementPath since the base path is already specified in the ManagementScope.
RichAmberale
I thought that looked a little suspect as well, but I wasn't sure if it was valid or not. Not sure what you are trying to do with WMI, but you might give powershell a try. It's a little easier on the fingers.
Ted Elliott