Script to Save State on all Runnign Hyper-V VMs? Need a script to save state on all the VMs running on a server with the Hyper-V role?<hr size="1" align="left" width="25%">Tony Soper© 2009 Microsoft Corporation. All rights reserved.Sat, 20 Jun 2009 07:59:14 Z98e3d42e-88c0-4a15-974f-532dc55123c5http://social.technet.microsoft.com/Forums/en-US/ITCG/thread/98e3d42e-88c0-4a15-974f-532dc55123c5#98e3d42e-88c0-4a15-974f-532dc55123c5http://social.technet.microsoft.com/Forums/en-US/ITCG/thread/98e3d42e-88c0-4a15-974f-532dc55123c5#98e3d42e-88c0-4a15-974f-532dc55123c5tonysohttp://social.technet.microsoft.com/Profile/en-US/?user=tonysoScript to Save State on all Runnign Hyper-V VMs? Need a script to save state on all the VMs running on a server with the Hyper-V role?<hr size="1" align="left" width="25%">Tony SoperThu, 30 Oct 2008 18:57:22 Z2008-10-30T18:57:22Zhttp://social.technet.microsoft.com/Forums/en-US/ITCG/thread/98e3d42e-88c0-4a15-974f-532dc55123c5#672afbbf-8193-458a-9997-0a8b3f3ecbbfhttp://social.technet.microsoft.com/Forums/en-US/ITCG/thread/98e3d42e-88c0-4a15-974f-532dc55123c5#672afbbf-8193-458a-9997-0a8b3f3ecbbftonysohttp://social.technet.microsoft.com/Profile/en-US/?user=tonysoScript to Save State on all Runnign Hyper-V VMs?<p>'               Script that saves the state of running VMs on a Hyper-V enabled server<br>'               Cobbled together by <a href="mailto:Johnkel@Microsoft.com">Johnkel@Microsoft.com</a><br>'<br>strComputer = &quot;.&quot; <br>Set objWMIService = GetObject(&quot;winmgmts:\\&quot; &amp; strComputer &amp; &quot;\root\virtualization&quot;) <br>Set vmcollecion = objWMIService.ExecQuery(&quot;SELECT * FROM Msvm_ComputerSystem&quot;,,48) </p> <p>Wscript.Echo VbCrLF <br>Wscript.Echo &quot;Name                                    Description                        State &quot;<br>Wscript.Echo &quot;--------------------------------------- ---------------------------------- -------------&quot;</p> <p>' loop through all instances in collection<br>For each vm in vmcollecion<br> ' decode system state, based on codes identified via trial and error<br> VMStateCode = vm.EnabledState<br> Select Case VMStateCode <br>         Case 2 VMState = &quot;Running&quot;<br>          Case 3      VMState = &quot;PowerOff&quot;<br>          Case 4      VMState = &quot;ShuttingDown&quot;<br>         Case 10    VMState = &quot;Reset&quot;<br>         Case 32768 VMState = &quot;Paused&quot;<br>         Case 32770 VMState = &quot;Starting&quot;<br>  Case 32771 VMState = &quot;SnapshotInProgress&quot;<br>         Case 32772 VMState = &quot;Migrating&quot;<br>         Case 32773 VMState = &quot;Saving&quot;<br>         Case 32774 VMState = &quot;Stopping&quot;<br>         Case 32776 VMState = &quot;Pausing&quot; <br>         Case 32777 VMState = &quot;Resuming&quot;<br>         Case 32769 VMState = &quot;Saved&quot;<br>  Case  Else  VMState = &quot;Unclassified (so far)&quot;<br> End Select</p> <p> ' print out VM info state<br> Wscript.Echo VbCrLF <br> Wscript.Echo  vm.ElementName &amp; Space(40 - Len(vm.ElementName)) &amp; _<br>    vm.Description &amp; Space(35 - Len(vm.Description)) &amp; _ <br>   VMState &amp; &quot; (&quot; &amp; VMStateCode &amp; &quot;)&quot;</p> <p> ' request state change if appropriate - the host should never be eligible<br> if ( VMState = &quot;Running&quot; or VMState = &quot;Paused&quot;) and vm.Description &lt;&gt; &quot;Microsoft Hosting Computer System&quot; Then<br>  Wscript.Echo Space(40) &amp; &quot;Saving &quot; &amp; vm.ElementName</p> <p>  RequestReturn = vm.RequestStateChange(32769)</p> <p>  if RequestReturn = 4096 Then<br>   Wscript.Echo Space(40) &amp; &quot;Save request submitted&quot;<br>  Else<br>   Wscript.Echo Space(40) &amp; &quot;Something goofy happened (&quot; &amp; RequestReturn &amp; &quot;)&quot;<br>  End If<br> End If<br>Next<br>Wscript.Echo VbCrLF </p> <hr align=left width="25%" size=1> Tony SoperThu, 30 Oct 2008 18:59:23 Z2008-10-30T18:59:58Zhttp://social.technet.microsoft.com/Forums/en-US/ITCG/thread/98e3d42e-88c0-4a15-974f-532dc55123c5#895b6884-df27-43b3-98ad-f05f37920885http://social.technet.microsoft.com/Forums/en-US/ITCG/thread/98e3d42e-88c0-4a15-974f-532dc55123c5#895b6884-df27-43b3-98ad-f05f37920885bsd01http://social.technet.microsoft.com/Profile/en-US/?user=bsd01Script to Save State on all Runnign Hyper-V VMs? hi,<br><br>in my case this script doesn't work. I think the reason is, the HOST-Name is in vmcollection too..<br><br>greetings<br>bsd01Thu, 27 Nov 2008 15:39:12 Z2008-11-27T15:39:12Zhttp://social.technet.microsoft.com/Forums/en-US/ITCG/thread/98e3d42e-88c0-4a15-974f-532dc55123c5#294906e8-b486-4386-a66b-2accda6bd6a5http://social.technet.microsoft.com/Forums/en-US/ITCG/thread/98e3d42e-88c0-4a15-974f-532dc55123c5#294906e8-b486-4386-a66b-2accda6bd6a5Daniel Capillahttp://social.technet.microsoft.com/Profile/en-US/?user=Daniel%20CapillaScript to Save State on all Runnign Hyper-V VMs?Aloha,<br/> <br/> since i searched a long time for a working script to do different state-changes to a virtual machine, i wrote a powershell-script that does most of the needed things. Maybe it helps - even if this topic is old.<br/> <br/> Updated versions can be found at http://capilla.codeplex.com/<br/> <br/> <br/> <br/> # Daniel Capilla (anyWARE AG Mainz)<br/> # Date: 2009-06-19<br/> # Version 0.7<br/> # - Initial stable beta<br/> # Version 0.7.1<br/> # - stable release<br/> # - added error handling and output<br/> # - restructuring script<br/> # Updated versions can be found at http://capilla.codeplex.com/<br/> # Syntax: .\hvcmd.ps1 [start|stop|pause|save|stop|shutdown|snapshot] [machinename]<br/> #<br/> # If you would like to start the script via task, use this commandline for example:<br/> # powershell d:\hvcmd.ps1 &quot;pause srv01&quot;<br/> <br/> <br/> #Getting Parameters from commandline<br/>   Param($operation, $servername) <br/> <br/> <br/> #Getting initial VM session names<br/>   $Vm = Get-WmiObject -Namespace root\virtualization  -Query &quot;Select * From Msvm_ComputerSystem Where ElementName = '$servername'&quot;<br/>   $VM_Service = get-wmiobject -namespace root\virtualization Msvm_VirtualSystemManagementService<br/>   $ListofVMs = get-wmiobject -namespace root\virtualization Msvm_ComputerSystem -filter  “ElementName &lt;&gt; Name ”<br/> <br/> <br/> #If there is no machine with the desired name, display a warning message<br/>   if ($VM.ElementName -ne $servername)<br/>   {<br/>     write-warning &quot;Virtual machine does not exist!&quot;<br/>     break<br/>   }<br/>       <br/> #Setting the &quot;state&quot; for later actions      <br/>   if ($operation -eq 'start') <br/>   {<br/>     $state = &quot;started&quot;<br/>     $stateId = 2<br/>   }<br/>   elseif ($operation -eq 'stop') <br/>   {<br/>     $state = &quot;stoped&quot;<br/>     $stateId = 3<br/>   }<br/>   elseif ($operation -eq 'pause') <br/>   {<br/>     $state = &quot;paused&quot;<br/>     $stateId = 32768<br/>   }<br/>   elseif ($operation -eq 'save') <br/>   {<br/>     $state = &quot;saved&quot;<br/>     $stateId = 32769<br/>   }<br/>   elseif (($operation -eq 'shutdown') -and ($servername -ne $NUL))<br/>   {<br/>     #$VM_Service = get-wmiobject -namespace root\virtualization Msvm_VirtualSystemManagementService<br/>     $ListofVMs = get-wmiobject -namespace root\virtualization Msvm_ComputerSystem -filter  “ElementName &lt;&gt; Name ”<br/>     $Vm = Get-WmiObject -Namespace root\virtualization  -Query &quot;Select * From Msvm_ComputerSystem Where ElementName = '$servername'&quot;<br/>     $ShutdownIC = Get-WmiObject -Namespace root\virtualization  -Query &quot;Associators of {$Vm} Where AssocClass=Msvm_SystemDevice ResultClass=Msvm_ShutdownComponent&quot;<br/>     $ShutdownIC.InitiateShutdown(&quot;TRUE&quot;, &quot;Automated shutdown via script&quot;)    <br/>     $state = &quot;shutdown initiated&quot;<br/>     #Do this break because of avoiding errors - not fine but working<br/>     break    <br/>   }<br/>   elseif (($operation -eq 'snapshot')  -and ($servername -ne $NUL))<br/>   {<br/>     #This part took a long time to get it to work, since the state &quot;snapshot&quot; is not recognized, or<br/>     #let's say - wrong recognized in some cases - the state-id &quot;32771&quot; did not work properly for us<br/>     #$VM_Service = get-wmiobject -namespace root\virtualization Msvm_VirtualSystemManagementService<br/>     #$ListofVMs = get-wmiobject -namespace root\virtualization Msvm_ComputerSystem -filter  “ElementName &lt;&gt; Name ”<br/> <br/>     foreach ($VM in [array] $ListOfVMs)<br/>     {<br/>       # $VM.ElementName <br/>       # $VM.__PATH<br/>       IF ($VM.ElementName -eq $servername)<br/>       {  <br/>         $VM_service.CreateVirtualSystemSnapShot($VM.__PATH)<br/>         $state = &quot;snapshot initiated&quot;<br/>         $stateId = (32771)<br/>        }  <br/>     }<br/>       $servername + &quot; &quot; + $state<br/>       break<br/>   }<br/> <br/> #Check if specified command is valid<br/>   else <br/>   {   <br/>     write-warning &quot;Unknown command.&quot;<br/>     &quot;Syntax: .\hvcmd.ps1 [command] [machinename]&quot; <br/>     &quot;Commands: start stop pause save stop shutdown snapshot&quot;<br/>     &quot;Machinename: Name of the Virtual instance (for example: server01)&quot;<br/>     break <br/> }<br/> <br/> <br/> #Get a handle to the VM object<br/>   $Core = get-wmiobject -namespace root\virtualization -class Msvm_Computersystem -filter &quot;ElementName = '$servername'&quot; <br/> <br/> #Set the state<br/>   $status = $Core.RequestStateChange($stateId) <br/> <br/> #Actual state<br/>  if ($status.ReturnValue -ne 32775) <br/>  {<br/>    $servername + &quot; &quot; + $state<br/>  }<br/>   elseif ($status.ReturnValue -eq 32775)<br/>   {    <br/>     &quot;Nothing changed - allready in this state.&quot;  <br/>   } <br/> <br/> <br/> <br/> Regards<br/> Daniel CapillaSat, 20 Jun 2009 07:59:13 Z2009-06-20T07:59:13Z