Thursday, May 28, 2009

Script to List the VMs in a Hyper-V Host

This script will collect the list and status of the VMs hosted in a  hyper-V host.

strComputer = "."
Set objWMIService = GetObject("winmgmts:\\" & strComputer & "\root\virtualization")
Set vmcollection = objWMIService.ExecQuery("SELECT * FROM Msvm_ComputerSystem",,48)
Wscript.Echo VbCrLF
Wscript.Echo "Name Description State "
Wscript.Echo "--------------------------------------- ---------------------------------- -------------"

For Each vm in vmcollection
VMStateCode = vm.EnabledState
Select Case VMStateCode
Case 2 VMState = "Running"
Case 3 VMState = "PowerOff"
Case 4 VMState = "ShuttingDown"
Case 10 VMState = "Reset"
Case 32768 VMState = "Paused"
Case 32770 VMState = "Starting"
Case 32771 VMState = "SnapshotInProgress"
Case 32772 VMState = "Migrating"
Case 32773 VMState = "Saving"
Case 32774 VMState = "Stopping"
Case 32776 VMState = "Pausing"
Case 32777 VMState = "Resuming"
Case 32769 VMState = "Saved"
Case Else VMState = "Unclassified (so far)"
End Select

Wscript.Echo vm.ElementName & Space(40 - Len(vm.ElementName)) & _
vm.Description & Space(35 - Len(vm.Description)) & _
VMState & " (" &VMStateCode & ")"
Next


Technorati Tags: ,,

No comments:

Post a Comment