none
Script to List all VMs on a Hyper-V Server?

    Question

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

Answers

  •  

    '               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 by tonysoper_MSFT Thursday, October 30, 2008 7:22 PM
    Thursday, October 30, 2008 7:05 PM

All replies

  •  

    '               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 by tonysoper_MSFT Thursday, October 30, 2008 7:22 PM
    Thursday, October 30, 2008 7:05 PM
  • Thanks Tony,

    Can we get all the details in to a txt file.

    Cheers

    Appi.

     


    Papi Reddy
    Monday, May 9, 2011 7:07 AM
  • If you have System Center Virtual Machine Manager (SCVMM) installed:

    PS > Add-PSSnapin Microsoft.SystemCenter.VirtualMachineManager
    PS > Get-VMMServer -ComputerName <VMServerName>
    PS > Get-VM
    Shay Levy [MVP]
    PowerShay.com
    PowerShell Toolbar
    Monday, May 9, 2011 8:20 AM
    Moderator
  • Hi Shay,

    thanks for your reply.

    yeah SCVMM installed on my server (2008 R2) and the above command seems we need manually enter each and every VM details, I'need a script that will automatically generate a list of vms and their state in to text file.

     

    - Appi.


    Papi Reddy
    Monday, May 9, 2011 8:39 AM
  • I recommend exporting to a csv file so you can import it back and get objects with properties (instead of a plain text file that you need to parse to get the exported information).

     

    Get-VM | Select-Object Name,Status | Export-Csv .\VMs.csv -NoTypeInformation


    Shay Levy [MVP]
    PowerShay.com
    PowerShell Toolbar
    • Proposed as answer by papireddy Monday, May 9, 2011 9:16 AM
    Monday, May 9, 2011 8:43 AM
    Moderator
  • Thanks Shay, I got It.
    Papi Reddy
    • Proposed as answer by SBKURID Saturday, March 17, 2012 6:20 AM
    • Unproposed as answer by SBKURID Saturday, March 17, 2012 6:20 AM
    Monday, May 9, 2011 9:16 AM
  • Thanks Shay, I got It.
    Papi Reddy

    strComputer = "."
    Set objWMIService = GetObject("winmgmts:\\" & strComputer & "\root\CIMV2")
    Set colItems = objWMIService.ExecQuery( _
        "SELECT * FROM Win32_PerfFormattedData_VidPerfProvider_HyperVVMVidPartition",,48)
    For Each objItem in colItems
        Wscript.Echo "Name: " & objItem.Name
    Next
    Saturday, March 17, 2012 6:21 AM