Eric
Eric

Reputation: 1

Microsoft Server 2012 R2 WMI (virtualization/v2) not detecting virtual machines - VBScript

As the title indicates, I'm currently unable to retrieve a list of virtual machines with a WMI query in VBScript. Hyper-V manager is correctly identifying 3 Virtual Machines on the Host in question, but when I query WMI I only see the Host itself.

Here's a sample VBScript (courtesy of 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

Output:

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

Ideas, suggestions, or rocks to look under would be greatly appreciated, thanks!

Upvotes: 0

Views: 3400

Answers (1)

Rich
Rich

Reputation: 4170

You can take it one level higher and grab the computer names directly then compare the model and extract accordingly. I do not have any VM's installed to try this. But give it a shot and let me know if it works.

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

Upvotes: 1

Related Questions