Q. How can I easily change the status of all my Microsoft Hyper-V server’s virtual machines (VMs)?

A. A while ago, I wanted to back up my Hyper-V server's VMs, but first I needed them to hibernate. So, I wrote the script below to modify all the VMs’ states. The script has an option to change those VMs currently in a passed state, and includes commands to ignore one designated VM to back up the rest through Microsoft System Center Data Protection Manager (DPM) 2007. Because DPM 2007 itself was running on a VM, I had to include commands not to stop that machine. The script ignores the Hyper-V server’s local host name.

' sethypervstateall.vbs John Savill
If Wscript.Arguments.Count Wscript.Echo "Arguments <desired state> \[<current state>\] \[\] required. For example:" & vbCrLf _
& "cscript sethypervstateall.vbs 32769"
Wscript.Echo "Valid desired states 2 (Enabled), 3 (Disabled), 32768 (Paused), and 32769 (Suspended)" Wscript.Quit(0)
End If
strDesiredState = Trim(Wscript.Arguments(0))
intDesiredState = strDesiredState + 0

strQueryString="."

If Wscript.Arguments.Count > 1 Then
strCurrentState = Trim(Wscript.Arguments(1))
intCurrentState = strCurrentState + 0
strQueryString = "Select * from Msvm_ComputerSystem where EnabledState=" & intCurrentState
Else
strQueryString = "Select * from Msvm_ComputerSystem"
End If

If Wscript.Arguments.Count > 2 Then
strExcludeComputer = Trim(Wscript.Arguments(2))
Else
strExcludeComputer = "NOTUSEDNONONO"
End If

strComputer = "."

Set objNetwork = WScript.CreateObject("WScript.Network")
strLocalServer = objNetwork.ComputerName

Wscript.Echo "Local server - " & strLocalServer

Set objWMIService = GetObject("winmgmts:" _
& "\{impersonationLevel=impersonate\}!\\" _
& strComputer & "\root\Virtualization")

Set colVMs = objWMIService.ExecQuery(strQueryString)
Select Case intDesiredState
Case 0
strNewState = "Unknown (0)"
Case 2
strNewState = "Enabled (2)"
Case 3
strNewState = "Disabled (3)"
Case 32768
strNewState = "Paused (32768)"
Case 32769
strNewState = "Suspended (32769)"
End Select

For Each objVM in colVMs
if (StrComp(strExcludeComputer,Trim(objVM.ElementName),vbTextCompare) <> 0) and (StrComp(strLocalServer,Trim(objVM.ElementName),vbTextCompare) <> 0) then
Select Case objVM.EnabledState
Case 0
strState = "Unknown (0)"
Case 2
strState = "Enabled (2)"
Case 3
strState = "Disabled (3)"
Case 32768
strState = "Paused (32768)"
Case 32769
strState = "Suspended (32769)"
Case 32770
strState = "Starting (32770)"
Case 32771
strState = "Snapshotting (32771)"
Case 32772
strState = "Migrating (32772)"
Case 32773
strState = "Saving (32774)"
Case 32774
strState = "Stopping (32774)"
Case 32775
strState = "Deleted (32775)"
Case 32776
strState = "Pausing (32776)"
End Select
Wscript.Echo "Will change state of " _
& objVM.ElementName & " from state " & strState & " to " & strNewState

objVM.RequestStateChange(intDesiredState)
end if
Next

WScript.Sleep 60000 ' want to sleep at the end so giving it 60 seconds for all to be put to sleep/resume

The following command sets the currently operating VMs to a suspended state, except for savdalsq101.

cscript sethypervstateall.vbs 32769 2 savdalsql01

Here’s the result:

Microsoft (R) Windows Script Host Version 5.7
Copyright (C) Microsoft Corporation. All rights reserved.

Local server - SAVDALVS01
Will change state of savdaldc01 from state Enabled (2) to Suspended (32769)

To return the suspended VMs to operation, use the following code as a guide:

cscript sethypervstateall.vbs 2 32769 savdalsql01

The result is:

Microsoft (R) Windows Script Host Version 5.7
Copyright (C) Microsoft Corporation. All rights reserved.

Local server - SAVDALVS01
Will change state of savdaldc01 from state Suspended (32769) to Enabled (2)
Will change state of Vista Client from state Suspended (32769) to Enabled (2)

Notice that a second server is started because the first VM was already suspended. If you omit the last two parameters, the command would change every VM to the specified state.

Hide comments

Comments

  • Allowed HTML tags: <em> <strong> <blockquote> <br> <p>

Plain text

  • No HTML tags allowed.
  • Web page addresses and e-mail addresses turn into links automatically.
  • Lines and paragraphs break automatically.
Publish