C# - 尝试在远程计算机上运行脚本时出现 WMI InvalidOperationException
我正在尝试在同一域中的远程计算机上执行脚本,并使用相同的用户帐户登录到两台计算机。请注意,该脚本还存储在另一台计算机上。
代码
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);
例外
[InvalidOperationException: Operation is not valid due to the current state of the object.]
System.Management.ManagementObject.InvokeMethod(String methodName, Object[] args) +388806
结论
我是使用 WMI 的新手,所以我不确定我做错的事情是否很明显,尽管看起来围绕此异常类型在许多情况下都会使用,并且我无法找出实际问题是什么,因此将不胜感激。
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.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
你的“\root\cimv2\Win32_Process”应该是“\root\cimv2:Win32_Process”
注意冒号,这是我使用的。祝你好运!
Your "\root\cimv2\Win32_Process" should be "\root\cimv2:Win32_Process"
Note the colon, this is what I use. Good luck!
我有同样的问题。
result.InvokeMethod("GetOwner",ownerArgs)
问题是我试图在使用以下 WMI 查询检索到的对象上执行该操作。
SELECT ProcessId, ExecutablePath, CommandLine FROM Win32_Process
将其更改为
SELECT * FROM Win32_Process
修复了该问题。我可能还特别要求提供用户名和域。
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.
事实证明,托管该进程的 Windows 服务并未在有权在目标计算机上执行的帐户下运行。
我的错误!
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!