【问题标题】:how to get WMI events?如何获取 WMI 事件?
【发布时间】:2012-10-04 12:21:00
【问题描述】:

如何列出注册到 Msvm_ComputerSystem 类的 WMI 事件。我创建了一个虚拟机并有一个事件来注册poweronEvent。现在我想列出注册的事件。请帮忙。

【问题讨论】:

    标签: c# powershell wmi wmi-query hyper-v


    【解决方案1】:

    您可以尝试使用此代码 - 基于 ObjectQuery

                VMState vmState = VMState.Undefined;
                var connectionOptions = new ConnectionOptions();
                connectionOptions.Username = userName;
                connectionOptions.Password = password;
    
                var managementScope= new ManagementScope(string.Format(@"\\{0}\root\virtualization", hostServer), connectionOptions );
                manScope.Connect();  
    
                var objectQuery= new ObjectQuery("SELECT * FROM Msvm_ComputerSystem");
                var managementObjectSearcher  = new ManagementObjectSearcher(managementScope, objectQuery);
                var collection = managementObjectSearcher.Get();
    
                foreach (var managementObject  in collection )
                {
                   Console.WriteLine(managementObject["Yourkey"].ToString());
                }
    

    【讨论】:

    • 我尝试了 tis 代码,但出现以下异常:Exception.System.Management.ManagementException 未处理 HResult=-2146233087 未找到。而且我不明白你为什么要使用 VMState。
    猜你喜欢
    • 1970-01-01
    • 2011-01-08
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多