0

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!

Eric
  • 1
  • 1
  • 1
  • EDIT: I'm, not certain what happened but my code (above) is working correctly now. This is resolved. – Eric Mar 26 '14 at 18:31

1 Answers1

1

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
Rich
  • 4,134
  • 3
  • 26
  • 45
  • Rich, thanks for the suggestion. I tried it and initially got an error when the script hit 'objItem.ElementName' as apparently the ElementName property does not exist in Win32_ComputerSystem. (I confirmed on [MSDN](http://msdn.microsoft.com/en-us/library/aa394102(v=vs.85).aspx).) – Eric Mar 26 '14 at 15:35
  • That said, I still have no luck. I get only the physical Hardware Model "PowerEdge R720" and the Description "AT/AT Compatabile" as results. (NOTE: I get the same results on a different, and functional, Hyper-V Host.) – Eric Mar 26 '14 at 15:43
  • .Name does indeed return the name of the host. – Eric Mar 26 '14 at 15:45
  • I've updated the code to reflect our discussion. If this has resolved your question please mark this as the answer. – Rich Mar 26 '14 at 16:16
  • I still have no luck. If I just echo the .Model, .Name, and .Description outside the 'If' in your code the only result I get is info about the Host itself. I've confirmed this is the same behavior a different Hyper-V Host generates. (Note: That host correctly displays its guests using my original code.) – Eric Mar 26 '14 at 17:04