none
Script to Start All VMs on a Hyper-V Server?

    Question

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

Answers

  •  

    '               Script starts 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 "------------------------ ---------------------------------- -------------"

    ' 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(25 - 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" and vm.Description <> "Microsoft Hosting Computer System" Then
      Wscript.Echo Space(25) & "Starting " & vm.ElementName

      RequestReturn = vm.RequestStateChange(2)

      if RequestReturn = 4096 Then
       Wscript.Echo Space(25) & "Start request submitted"
      Else
       Wscript.Echo Space(25) & "Something goofy happened (" & RequestReturn & ")"
      End If
     End If
    Next
    Wscript.Echo VbCrLF


     


    Tony Soper
    • Marked as answer by tonysoper_MSFT Thursday, October 30, 2008 7:22 PM
    Thursday, October 30, 2008 7:08 PM

All replies

  •  

    '               Script starts 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 "------------------------ ---------------------------------- -------------"

    ' 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(25 - 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" and vm.Description <> "Microsoft Hosting Computer System" Then
      Wscript.Echo Space(25) & "Starting " & vm.ElementName

      RequestReturn = vm.RequestStateChange(2)

      if RequestReturn = 4096 Then
       Wscript.Echo Space(25) & "Start request submitted"
      Else
       Wscript.Echo Space(25) & "Something goofy happened (" & RequestReturn & ")"
      End If
     End If
    Next
    Wscript.Echo VbCrLF


     


    Tony Soper
    • Marked as answer by tonysoper_MSFT Thursday, October 30, 2008 7:22 PM
    Thursday, October 30, 2008 7:08 PM
  •  

    '               Script starts 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 "------------------------ ---------------------------------- -------------"

    ' 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(25 - 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" and vm.Description <> "Microsoft Hosting Computer System" Then
      Wscript.Echo Space(25) & "Starting " & vm.ElementName

      RequestReturn = vm.RequestStateChange(2)

      if RequestReturn = 4096 Then
       Wscript.Echo Space(25) & "Start request submitted"
      Else
       Wscript.Echo Space(25) & "Something goofy happened (" & RequestReturn & ")"
      End If
     End If
    Next
    Wscript.Echo VbCrLF


     


    Tony Soper
    Thursday, October 30, 2008 7:11 PM