Pergunta

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.

Foi útil?

Solução 3

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!

Outras dicas

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.

Licenciado em: CC-BY-SA com atribuição
Não afiliado a StackOverflow
scroll top