开发者

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

开发者 https://www.devze.com 2023-02-05 09:13 出处:网络
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.

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.ManagementObjec开发者_如何转开发t.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.


Your "\root\cimv2\Win32_Process" should be "\root\cimv2:Win32_Process"

Note the colon, this is what I use. Good luck!


I had the same issue.

result.InvokeMethod("GetOwner", ownerArgs)

The problem was that I was trying to execute that on an object I retrieved with the following WMI query.

SELECT ProcessId, ExecutablePath, CommandLine FROM Win32_Process

Changing it to SELECT * FROM Win32_Process fixed it.

I could've probably, also, specifically requested Username and Domain.


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!

0

精彩评论

暂无评论...
验证码 换一张
取 消