strComputer = "."
Set objWMIService = GetObject("winmgmts:\\" & strComputer & "\root\virtualization")
Set vmcollecion = objWMIService.ExecQuery("SELECT * FROM Msvm_ComputerSystem",,48)
Wscript.Echo VbCrLF
Wscript.Echo "Name Description State "
Wscript.Echo "--------------------------------------- ---------------------------------- -------------"
' loop through all instances in collection
For each vm in vmcollecion
' decode system state, based on codes identified via trial and error
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
' print out VM info state
Wscript.Echo VbCrLF
Wscript.Echo vm.ElementName & Space(40 - Len(vm.ElementName)) & _
vm.Description & Space(35 - Len(vm.Description)) & _
VMState & " (" & VMStateCode & ")"
' request state change if appropriate - the host should never be eligible
if ( VMState = "Running" or VMState = "Paused") and vm.Description <> "Microsoft Hosting Computer System" Then
Wscript.Echo Space(40) & "Saving " & vm.ElementName
RequestReturn = vm.RequestStateChange(32769)
if RequestReturn = 4096 Then
Wscript.Echo Space(40) & "Save request submitted"
Else
Wscript.Echo Space(40) & "Something goofy happened (" & RequestReturn & ")"
End If
End If
Next
Wscript.Echo VbCrLF