0

如标题所示,我目前无法在 VBScript 中使用 WMI 查询检索虚拟机列表。Hyper-V 管理器正确识别有问题的主机上的 3 个虚拟机,但是当我查询 WMI 时,我只看到主机本身。

这是一个示例 VBScript(由 WMI Code Creator 提供):

strComputer = "."
Set objWMIService = GetObject("winmgmts:\\" & strComputer & "\root\virtualization\v2") 
Set colItems = objWMIService.ExecQuery("SELECT * FROM Msvm_ComputerSystem",,48) 
For Each objItem in colItems
    Wscript.Echo "-----------------------------------"
    Wscript.Echo "Msvm_ComputerSystem instance"
    Wscript.Echo "-----------------------------------"
    Wscript.Echo "Description: " & objItem.Description
    Wscript.Echo "ElementName: " & objItem.ElementName
Next

输出:

Microsoft (R) Windows Script Host Version 5.8
Copyright (C) Microsoft Corporation. All rights reserved.
-----------------------------------
Msvm_ComputerSystem instance
-----------------------------------
Description: Microsoft Hosting Computer System
ElementName: TEST-VH

非常感谢您的想法,建议或岩石,谢谢!

4

1 回答 1

1

您可以将其提高一级并直接获取计算机名称,然后比较模型并进行相应的提取。我没有安装任何虚拟机来尝试这个。但是试一试,让我知道它是否有效。

strComputer = "."
Set objWMIService = GetObject("winmgmts:\\" & strComputer & "\root\cimv2")
Set colItems = objWMIService.ExecQuery("Select * from Win32_ComputerSystem",,48)

For Each objItem in colItems
strModel = objItem.Model
If instr(strModel, "Virtual Machine") Then
    Wscript.Echo "-----------------------------------"
    Wscript.Echo "Msvm_ComputerSystem instance"
    Wscript.Echo "-----------------------------------"
    Wscript.Echo "Description: " & objItem.Description
    Wscript.Echo "HostName: " & objItem.Name
End if
Next
于 2014-03-25T18:39:16.990 回答