WMI - 直接访问 Win32_OperatingSystem 的单例实例

发布于 2024-07-04 06:36:11 字数 1836 浏览 6 评论 0原文

我在直接访问通过 WMI 公开的 Win32_OperatingSystem 管理类时遇到问题。

它是一个单例类,我非常确定“Win32_OperatingSystem=@”是获取单例实例的正确路径语法。

对 InvokeMethod 的调用会产生问题底部列出的异常,访问 ClassPath 属性(注释行)也是如此。

我究竟做错了什么?

[我知道我可以使用 ManagementObjectSearcher/ObjectQuery 返回 Win32_OperatingSystem 的集合(其中仅包含一个),但由于我知道它是一个单例,所以我想直接访问它。]


ManagementScope cimv2 = InitScope(string.Format(@"\\{0}\root\cimv2", this.Name));

ManagementObject os = new ManagementObject(
    cimv2,
    new ManagementPath("Win32_OperatingSystem=@"),
    new ObjectGetOptions());

//ManagementPath p = os.ClassPath;

os.InvokeMethod("Reboot", null);

System.Management.ManagementException 被捕获 消息=“无效的对象路径” 来源=“系统.管理” 堆栈跟踪: 在 System.Management.ManagementException.ThrowWithExtendedInfo(管理状态错误代码) 在 System.Management.ManagementObject.Initialize(Boolean getObject) 在 System.Management.ManagementBaseObject.get_wbemObject() 在 System.Management.ManagementObject.get_ClassPath() 在 System.Management.ManagementObject.GetMethodParameters(字符串方法名称、 ManagementBaseObject&inParameters、IWbemClassObjectFreeThreaded&inParametersClass、IWbemClassObjectFreeThreaded&outParametersClass) 在 System.Management.ManagementObject.InvokeMethod(String methodName, Object[] args)


感谢您的回复。

Nick - 我不知道如何去做:)

Uros - 我的印象是它是一个单例类,因为 MSDN 页面。 此外,在 WBEMTest 实用程序中打开该类会显示


实例对话框显示:“1 个对象”和“最大批次:1”在这些字段中,并列出“Win32_OperatingSystem=@”

ManagementScope 已验证为正常工作,所以我不知道发生了什么。 我是 WMI 新手,但这似乎是最简单的用例之一!

I've having trouble directly accessing the Win32_OperatingSystem management class that is exposed via WMI.

It is a singleton class, and I'm pretty certain "Win32_OperatingSystem=@" is the correct path syntax to get the instance of a singleton.

The call to InvokeMethod produces the exception listed at the bottom of the question, as does accessing the ClassPath property (commented line).

What am I doing wrong?

[I'm aware that I can use ManagementObjectSearcher/ObjectQuery to return a collection of Win32_OperatingSystem (which would contain only one), but since I know it is a singleton, I want to access it directly.]


ManagementScope cimv2 = InitScope(string.Format(@"\\{0}\root\cimv2", this.Name));

ManagementObject os = new ManagementObject(
    cimv2,
    new ManagementPath("Win32_OperatingSystem=@"),
    new ObjectGetOptions());

//ManagementPath p = os.ClassPath;

os.InvokeMethod("Reboot", null);

System.Management.ManagementException was caught
Message="Invalid object path "
Source="System.Management"
StackTrace:
at System.Management.ManagementException.ThrowWithExtendedInfo(ManagementStatus errorCode)
at System.Management.ManagementObject.Initialize(Boolean getObject)
at System.Management.ManagementBaseObject.get_wbemObject()
at System.Management.ManagementObject.get_ClassPath()
at System.Management.ManagementObject.GetMethodParameters(String methodName, ManagementBaseObject& inParameters, IWbemClassObjectFreeThreaded& inParametersClass, IWbemClassObjectFreeThreaded& outParametersClass)
at System.Management.ManagementObject.InvokeMethod(String methodName, Object[] args)


Thanks for the replies.

Nick - I don't know how to go about doing that :)

Uros - I was under the impression that it was a singleton class because of this MSDN page. Also, opening the class in the WBEMTest utility shows this.


The instances dialog shows: "1 objects" and "max. batch: 1" in those fields and lists "Win32_OperatingSystem=@"

The ManagementScope is verified as working, so I don't know what's up. I'm a WMI novice, but this seems like one of the simplest use cases!

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

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

发布评论

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

评论(5

素年丶 2024-07-11 06:36:11

Win32_OperatingSystem 不是单例类 - 如果检查它的限定符,您会发现没有为其定义单例限定符,因此您必须使用 ManagementObjectSearcher.Get() 或 ManagementClass.GetInstances(),即使只有该类的一个实例。 Win32_OperatingSystem 关键属性是 Name,因此可以选择直接获取实例,

ManagementObject OS = new ManagementObject(@"Win32_OperatingSystem.Name='OSname'")

但根据我的经验,OSName 始终类似于:

“Microsoft Windows XP Professional|C:\WINDOWS|\Device\Harddisk0\Partition1”,

因此使用 ManagementObjectSearcher可能是最简单的解决方案。

Win32_OperatingSystem is not a singleton class - if you check its qualifiers, you'll see that there is no Singleton qualifier defined for it, so you'll have to use ManagementObjectSearcher.Get() or ManagementClass.GetInstances() even though there is only one instance of the class. Win32_OperatingSystem key property is Name, so there is an option to get the instance directly, using

ManagementObject OS = new ManagementObject(@"Win32_OperatingSystem.Name='OSname'")

but in my experience, OSName is always something like:

"Microsoft Windows XP Professional|C:\WINDOWS|\Device\Harddisk0\Partition1"

so using ManagementObjectSearcher is probably the easiest solution.

赴月观长安 2024-07-11 06:36:11

我刚刚尝试了这个简单的应用程序,运行良好,

using System;
using System.Management;

namespace WmiPlay
{
    class Program
    {
        static void Main(string[] args)
        {
            try
            {
                ManagementScope cimv2 = new ManagementScope(@"\\.\root\cimv2");
                ManagementObject os = new ManagementObject(cimv2, new ManagementPath("Win32_OperatingSystem=@"), new ObjectGetOptions());
                Console.Out.WriteLine(os);
            }
            catch (Exception ex)
            {
                Console.Error.WriteLine(ex);
            }
        }
    }
}

看看这是否适合您? 我确实在 Visual Studio 中运行了它,我通常在 Vista x64 下以管理员身份运行它。

I've just tried this simple app that worked ok

using System;
using System.Management;

namespace WmiPlay
{
    class Program
    {
        static void Main(string[] args)
        {
            try
            {
                ManagementScope cimv2 = new ManagementScope(@"\\.\root\cimv2");
                ManagementObject os = new ManagementObject(cimv2, new ManagementPath("Win32_OperatingSystem=@"), new ObjectGetOptions());
                Console.Out.WriteLine(os);
            }
            catch (Exception ex)
            {
                Console.Error.WriteLine(ex);
            }
        }
    }
}

See if this works for you? I did run it in Visual Studio which I normally run as administrator under Vista x64.

变身佩奇 2024-07-11 06:36:11

我不是 100% 确定答案,但是您是否尝试过使用 Reflector 来查看 ManagementObjectSearcher 的作用? 它可能会给您一些关于您做错了什么的线索。

I'm not 100% sure of the answer, but have you tried using reflector to look at what ManagementObjectSearcher does? It may give you some clue as to what you are doing wrong.

一张白纸 2024-07-11 06:36:11

我可能会构造一个查询来获取 Primary = true 的实例。 我有一段时间没有使用 Win32_OperatingSystem,但我似乎记得获得了多个实例,并且当前启动的实例的 Primary 等于 true。

I would probably construct a query that gets the instance where Primary = true. I haven't used Win32_OperatingSystem in a while, but I seem to remember getting multiple instances, and the one that was currently booted had Primary equal to true.

短暂陪伴 2024-07-11 06:36:11

邓肯写道:

实例对话框在这些字段中显示“1 个对象”和“最大批次:1”,并列出“Win32_OperatingSystem=@”

它看起来确实应该工作。 您可以使用另一个单例类测试您的代码,例如:

“Win32_WmiSetting=@”

,看看是否仍然出现异常。

Duncan wrote:

The instances dialog shows: "1 objects" and "max. batch: 1" in those fields and >lists "Win32_OperatingSystem=@"

It sure looks like it should work. You could test your code with another singleton class, like:

"Win32_WmiSetting=@"

and see if you still get the exception.

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