Use Custom Properties on Computer Tiers to Determine Shutdown and Startup Order of Virtual Machines in a Service (Script)

 

Updated: May 13, 2016

Applies To: System Center 2012 R2 Virtual Machine Manager, System Center 2012 - Virtual Machine Manager

You can create and add custom properties to objects in System Center 2012 – Virtual Machine Manager (VMM). To create a new custom property, use the New-SCCustomProperty cmdlet. When you create or update a custom property, you can determine which objects the property can be applied to by using the AddMember parameter. You can apply custom properties to virtual machines, virtual machine templates, hosts, host clusters, host groups, service templates, computer tiers, and clouds. After you have created a custom property and added to an object, you can then add a value to the custom property and take actions on the object based on the custom property value.

Disclaimer

The following script creates a custom property for computer tier objects. It then applies values to the custom properties that will determine the startup and shutdown order of the virtual machines in the tiers.

  
<#  
  Description:   This script creates a custom property for computer tier objects.  
                 The script then applies values to the custom properties on the  
                 computer tiers. Based on property values, the script stops the   
                 virtual machines in the specified order, and then starts the   
                 virtual machines, also in the specified order.  
#>  
  
# Create custom properties for the computer tiers.  
$CustomProp = New-SCCustomProperty -Name StopStartOrder -AddMember "ComputerTier"  
  
# Get the computer tiers on which you want to set the shutdown and startup order.  
$Service = Get-SCService -Name "NewService6"  
$Tiers = Get-SCComputerTier -Service $Service  
$TierNumber = $Tiers.count  
  
# Set the shutdown/startup order custom property on the computer tiers.  
$ComputerTier1 = Get-SCComputerTier -Service $Service | where {$_.Name -eq "Web Tier"}  
Set-SCCustompropertyValue -CustomProperty $CustomProp -InputObject $ComputerTier1 -Value "1"  
$ComputerTier2 = Get-SCComputerTier -Service $Service | where {$_.Name -eq "ComputerTier2"}  
Set-SCCustompropertyValue -CustomProperty $CustomProp -InputObject $ComputerTier2 -Value "2"  
  
# Stop the virtual machines in order before stopping the service.  
$i = 1  
While ($i -le $TierNumber)  
{  
   Get-SCComputerTier -Service $Service | where {$_.CustomProperty.Values -eq "$i"} | Get-SCVirtualMachine | Stop-SCVirtualMachine -Shutdown  
  
   $i = $i+1  
}  
  
# Stop the service.  
Stop-SCService -Service $Service  
  
# Pause to ensure that the service is stopped.  
Start-Sleep "30"  
  
# Start the virtual machines in order before starting the service.  
$i = 1  
While ($i -le $TierNumber)  
{  
   Get-SCComputerTier -Service $Service | where {$_.CustomProperty.Values -eq "$i"} | Get-SCVirtualMachine | Start-SCVirtualMachine  
  
   $i = $i+1  
}  
  
# Start the service.  
Start-SCService -Service $Service