Script Center > Scripting Forums > The Official Scripting Guys Forum! > Script to List all VMs on a Hyper-V Server?
Ask a questionAsk a question
 

AnswerScript to List all VMs on a Hyper-V Server?

  • Thursday, October 30, 2008 7:05 PMtonyso Users MedalsUsers MedalsUsers MedalsUsers MedalsUsers Medals
     
    Need a script to list all the VMs on a server with the Hyper-V role enabled? 
    Tony Soper

Answers

  • Thursday, October 30, 2008 7:05 PMtonyso Users MedalsUsers MedalsUsers MedalsUsers MedalsUsers Medals
     AnswerHas Code
     

    '               Script shows the state of all VMs on a Hyper-V enabled server
    '               Cobbled together by Johnkel@Microsoft.com
    '
    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 "--------------------------------------- ---------------------------------- -------------"

    For Each vm in vmcollecion

     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




    Tony Soper
    • Marked As Answer bytonysoperMSFTThursday, October 30, 2008 7:22 PM
    •  

All Replies

  • Thursday, October 30, 2008 7:05 PMtonyso Users MedalsUsers MedalsUsers MedalsUsers MedalsUsers Medals
     AnswerHas Code
     

    '               Script shows the state of all VMs on a Hyper-V enabled server
    '               Cobbled together by Johnkel@Microsoft.com
    '
    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 "--------------------------------------- ---------------------------------- -------------"

    For Each vm in vmcollecion

     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




    Tony Soper
    • Marked As Answer bytonysoperMSFTThursday, October 30, 2008 7:22 PM
    •