Q. How can I quickly create a list of all virtual machines (VMs) on a list of Hyper-V servers from the command line?

John Savill

June 4, 2011

2 Min Read
ITPro Today logo in a gray background | ITPro Today

A. I wanted to create a list of all VMs, showing full VM name, for a list of Hyper-V servers. I created the VBScript below for the purpose. You just pass it Hyper-V servers, separated by spaces, and it will list out the VMs on the server for each passed server. I also added a function to show current status as well, but you don't have to use that—you can output in whatever format you want.

' listvms.vbs
' John Savill 5/19/2011
'
For count = 0 To (WScript.Arguments.Count-1)
strHVServer = WScript.Arguments.Item(count)
Set objWMIService = GetObject("winmgmts:\" & strHVServer & "rootvirtualization")
Set arrVMs = objWMIService.ExecQuery("SELECT * FROM Msvm_ComputerSystem",,48)
For Each VM In arrVMs
Call convertVMStateToText(VM.EnabledState,strState)
WScript.StdOut.WriteLine VM.ElementName & "|" & strHVServer & "|" & strState
Next
Next

sub convertVMStateToText(ByVal varEnabledState, inStrVMState)
select case varEnabledState
case 0
inStrVMState = "unknown"
case 2
inStrVMState = "running"
case 3
inStrVMState = "powered off"
case 32768
inStrVMState = "paused"
case 32770
inStrVMState = "starting"
case 32773
inStrVMState = "saving"
case 32774
inStrVMState = "stopping"
case 32776
inStrVMState = "pausing"
case 32777
inStrVMState = "resuming"
case else
inStrVMState = "don't know"
end select
end sub

To call it, I would just use

cscript listvbs.vbs

For example,

D:projectsVBScripts>cscript listvms.vbs savdalbfs01.savilltech.net savdalbfs02

Microsoft (R) Windows Script Host Version 5.8
Copyright (C) Microsoft Corporation. All rights reserved.

SAVDALBFS01|savdalbfs01.savilltech.net|running
savdalync01|savdalbfs01.savilltech.net|running
savdalclient.savilltech.net|savdalbfs01.savilltech.net|running
APPVSequence|savdalbfs01.savilltech.net|powered off
savdalappv01|savdalbfs01.savilltech.net|running
PXETest|savdalbfs01.savilltech.net|powered off
savdalts01|savdalbfs01.savilltech.net|running
savdalex10|savdalbfs01.savilltech.net|running
savdalrodc01|savdalbfs02|running
savdalclient3.savilltech.net|savdalbfs02|running
savdaldc10|savdalbfs02|running
suseentdesk|savdalbfs02|powered off
savloncli02|savdalbfs02|powered off
savdalclient4.savilltech.net|savdalbfs02|running

About the Author

Sign up for the ITPro Today newsletter
Stay on top of the IT universe with commentary, news analysis, how-to's, and tips delivered to your inbox daily.

You May Also Like