Odeslat dotazOdeslat dotaz
 

OdpovědětScript to Start All VMs on a Hyper-V Server?

Odpovědi

  • 30. října 2008 19:08tonyso Uživatelské medaileUživatelské medaileUživatelské medaileUživatelské medaileUživatelské medaile
     OdpovědětObsahuje kód
     

    '               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

Všechny reakce

  • 30. října 2008 19:08tonyso Uživatelské medaileUživatelské medaileUživatelské medaileUživatelské medaileUživatelské medaile
     OdpovědětObsahuje kód
     

    '               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
  • 30. října 2008 19:11tonyso Uživatelské medaileUživatelské medaileUživatelské medaileUživatelské medaileUživatelské medaile
     Obsahuje kód
     

    '               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