调用 WMI 对象上的方法时出现 InvalidOperationException

发布于 2024-08-06 06:35:38 字数 1283 浏览 5 评论 0原文

基本上我是 WMI 的新手,我不知道我在这里缺少什么或如何调试它...

    ConnectionOptions connectOptions = new ConnectionOptions();
    connectOptions.Username = user;
    connectOptions.Password = password;
    machine = "remoteMachine.com";

    ManagementScope scope = new ManagementScope(
        @"\\" + machine + @"\root\cimv2",
        connectOptions);

    scope.Connect();

    ManagementPath path = new ManagementPath(@"\\" + machine + @"\root\cimv2\Win23_Process");

    ManagementClass proc = new ManagementClass(scope, path, new ObjectGetOptions());

    ManagementBaseObject args = proc.GetMethodParameters("Create");

最后一行,我在 proc 上调用一个方法,导致这个 InvalidOperationException...也没有告诉我much :( 在 proc 上调用其他方法(例如 GetInstances())时,我得到相同的结果。通过代码调试时,我确实可以成功连接到远程计算机。

System.InvalidOperationException was unhandled
  Message="Operation is not valid due to the current state of the object."
  Source="System.Management"
  StackTrace:
       at System.Management.ManagementObject.GetMethodParameters(String methodName, ManagementBaseObject& inParameters, IWbemClassObjectFreeThreaded& inParametersClass, IWbemClassObjectFreeThreaded& outParametersClass)
       at System.Management.ManagementObject.GetMethodParameters(String methodName)

Basically I am new to WMI and I can't figure out what I am missing here or how to debug this...

    ConnectionOptions connectOptions = new ConnectionOptions();
    connectOptions.Username = user;
    connectOptions.Password = password;
    machine = "remoteMachine.com";

    ManagementScope scope = new ManagementScope(
        @"\\" + machine + @"\root\cimv2",
        connectOptions);

    scope.Connect();

    ManagementPath path = new ManagementPath(@"\\" + machine + @"\root\cimv2\Win23_Process");

    ManagementClass proc = new ManagementClass(scope, path, new ObjectGetOptions());

    ManagementBaseObject args = proc.GetMethodParameters("Create");

That last line, where I call a method on proc, causes this InvalidOperationException... doesn't tell me too much :( I get the same result when calling other methods on proc, such as GetInstances(). When debugging through the code, it does appear that I can connect to the remote machine successfully.

System.InvalidOperationException was unhandled
  Message="Operation is not valid due to the current state of the object."
  Source="System.Management"
  StackTrace:
       at System.Management.ManagementObject.GetMethodParameters(String methodName, ManagementBaseObject& inParameters, IWbemClassObjectFreeThreaded& inParametersClass, IWbemClassObjectFreeThreaded& outParametersClass)
       at System.Management.ManagementObject.GetMethodParameters(String methodName)

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(1

‖放下 2024-08-13 06:35:38

您的示例代码中有一个拼写错误,不确定它是否在您的真实代码中。您已经得到了 Win23_Process,它应该是 Win32_Process。 Create for Win32_Process 还需要命令行、目录等参数。

You've got a typo in your sample code, not sure if it's in your real code or not. You've got Win23_Process, which should be Win32_Process. Also Create for Win32_Process needs arguments such as command-line, directory, etc.

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