Search code examples
c#.net.net-3.5wmiremote-process

C# - WMI InvalidOperationException when trying to run a script on a remote machine


I'm trying to execute a script on a remote machine in the same domain, with the same user account logged on to both the machines. Note the script is also stored on yet another machine.

Code

string prop = propertyName
object[] cmd = { String.Format("cscript \\\\machine\\script.wsf", envId, application) };
ManagementClass mc = new ManagementClass("\\\\" + prop + "\\root\\cimv2\\Win32_Process");

mc.InvokeMethod("Create", cmd);

Exception

[InvalidOperationException: Operation is not valid due to the current state of the object.]
   System.Management.ManagementObject.InvokeMethod(String methodName, Object[] args) +388806

Conclusion

I'm new to using WMI, so I'm not sure if what I'm doing wrong is obvious, though looking around this exception type is used in many situations, and am having trouble finding out what the actual issue is, so help would be greatly appreciated.


Solution

  • It turns out that the windows service hosting the process was not running under an account that had permission to execute on the target machine.

    My mistake!