我是WMI类和hyper-v的新手,当我试图通过Msvm_ComputerSystem获取主机和所有虚拟机的信息时,结果只显示了主机信息,代码如下所示(只是提供了非常简单的示例)但是,当我使用powershel l命令Get-VM时,我可以获得虚拟机信息,顺便说一下,我在win 10上使用hyper-v并尝试在本地控制它。任何帮助都将受到高度赞赏。
ManagementScope scope = new ManagementScope("\\root\\virtualization\\V2");
string query = string.Format("SELECT * FROM Msvm_ComputerSystem");
ObjectQuery vmquery = new ObjectQuery(query);
ManagementObjectSearcher vmsearcher = new ManagementObjectSearcher(scope, vmquery);
ManagementObjectCollection vmCollection = vmsearcher.Get();
if (vmCollection.Count == 0)
{
Console.WriteLine("No virtual machine found");
}
ManagementObject vm = null;
foreach (ManagementObject instance in vmCollection)
{
vm = instance;
Console.WriteLine("\nName: {0} \nStatus:{1} \nDescription: ",
vm["ElementName"].ToString(),
vm["EnabledState"].ToString(),
vm["Description"].ToString());
}
}
答案 0 :(得分:0)
使用MS的WMI代码创建器从Here
下载它可以帮助您检查所有内容
我的C#示例代码获取VM列表
public void listvms()
{
ManagementObjectSearcher searcher =
new ManagementObjectSearcher("\\root\\virtualization\\V2",
"SELECT ElementName FROM Msvm_ComputerSystem where Description='Microsoft Virtual Machine'");
if (searcher.Get().Count != 0)
{
foreach (ManagementObject queryObj in searcher.Get())
{
vmnamelist.Items.Add(queryObj["ElementName"]);
}
}
}