如何获得WMI活动?

时间:2012-10-04 12:21:01

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

如何列出注册到Msvm_ComputerSystem类的WMI事件。我创建了一个vm并且有一个事件来注册poweronEvent。现在我要列出已注册的事件。请帮助。

1 个答案:

答案 0 :(得分:0)

您可以尝试使用此代码 - 基于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());
            }