Using SCVMM Cmdlets to change VM startup and shutdown actions

I was recently contacted by a customer running VMM 2008 R2 managing a farm of virtual machines.  This customer had a business requirement where all virtual machines must have an action when physical server stops as "Turn off virtual machine".  Originally, the environment was scoped for lab testing.  As time passed, workloads running on these virtual machines began to be used more as production than test.  Therefore, requirements changed and a new action when physical server stops as "SaveState" was needed.  The automatic start action did not require a change as it was already set to "Automatically turn on the virtual machine if it was running when physical server stopped." The customer was concerned about all the mouse clicks and administrative time required from the Virtual machine Manager Administrator Console required to make this change to 300 virtual machines.  Surely there must be an easier way to accomplish this task?  Further, the customer virtual machines resided in a HostGroup specific for his line of business defined in SCVMM. 

I downloaded the System Center Virtual Machine Manager 2008 R2 Cmdlet Reference and wrote the follwing script which will change the action when physical server stops to "SaveVM"  for all virtual machines in customer host group. 

* Notice line 7 where a Stop action value is evaluated.  I added this so script is smart enough to change property of VMs which are not in compliance only

Param($VMMServer)
Add-PSSnapin Microsoft.SystemCenter.VirtualMachineManager -ErrorAction SilentlyContinue
Get-VMMServer -ComputerName $VMMServer
$HostedVMs = @(Get-VM) | where {$_.HostGroupPath -like "All Hosts\TEST\*"}
foreach ($i in $HostedVMs)
{
 if ($i.StopAction.value__ -ne 0)
 {
  Set-VM $i -StopAction SaveVM
 }
}

   <#
.SYNOPSIS
  You will need to pass in the VMM Server name at run time and modify line 4 with appropriate Hostgroup. This script will set properties to all VMs in that Hostgroup defined
.DESCRIPTION
  Set action when physical server stops to Saved State for VMs running in specific host group
.Example
  Script requires one parameter containing the VMMServer
#>

You will begin to see jobs running in VMM Administrator Console similar to below:

I would also like to point out that you can change line 6 and leverage Set-VM to change any number of other properties of your VM.

This script sets the "Action when physical server stops" to "Shut down guest OS"

 Param($VMMServer)
Add-PSSnapin Microsoft.SystemCenter.VirtualMachineManager -ErrorAction SilentlyContinue
Get-VMMServer -ComputerName $VMMServer
$HostedVMs = @(Get-VM) | where {$_.HostGroupPath -like "All Hosts\TEST\*"}
foreach ($i in $HostedVMs)
{
 if ($i.StopAction.value__ -ne 2)
 {
  Set-VM $i -StopAction ShutdownGuestOS
 }
}

   <#
.SYNOPSIS
  You will need to pass in the VMM Server name at run time and modify line 4 with appropriate Hostgroup. This script will set properties to all VMs running in that hostgroup
.DESCRIPTION
  Set action when physical server stops to Shutdown for VMs running in specific host group
.Example
  Script requires one parameter containing the VMMServer
#>

 

 Dave Guenthner [MSFT]
This posting is provided "AS IS" with no warranties, and confers no rights.

Comments

  • Anonymous
    June 09, 2011
    Dave, your blog saves the day again. The ONLY response that big offers for the search stopaction.value 2 Thought you might want to know. Great script.