C# - 尝试在远程计算机上运行脚本时出现 WMI InvalidOperationException

发布于 2024-10-12 11:48:52 字数 716 浏览 3 评论 0原文

我正在尝试在同一域中的远程计算机上执行脚本,并使用相同的用户帐户登录到两台计算机。请注意,该脚本还存储在另一台计算机上。

代码

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

昔日梦未散 2024-10-19 11:48:52

你的“\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!

夏末的微笑 2024-10-19 11:48:52

我有同样的问题。

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.

很酷又爱笑 2024-10-19 11:48:52

事实证明,托管该进程的 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!

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文