Scale In a Computer Tier (Script)

 

Updated: May 13, 2016

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

You can reduce the amount of resources being used by a deployed service by scaling in the service. To scale in a service, you can remove a virtual machine from a computer tier, or change the state of the virtual machine. You define whether a computer tier can be scaled in by setting the InstanceMinimumCount parameter for a computer tier template.

The script in this topic includes two parameter sets. Both parameter sets require you to provide the service and computer tier. However, you can specify that the virtual machine be removed by using the RemoveVM switch, or take an action on the virtual machine by using the VMAction parameter.

Disclaimer

The following script checks whether you are able to scale in a tier by ensuring that removing a virtual machine from service will not cause the service to go below the minimum machine count for the tier. The script then locates the most recently created virtual machine that is running in the computer tier and either removes the virtual machine, or places the virtual machine in a choice of non-running states.

  
#   Description:   The script verifies that you are able to scale in a tier and, if so,  
#                  the script locates the most recently created virtual machine running  
#                  on the computer tier and either removes the virtual machine or stops,  
#                  pauses, stores, or saves the state of the virtual machine.  
  
Param (  
   [parameter(Mandatory=$true,ParameterSetName="RemoveVM")]  
   [Switch]  
   $RemoveVM,  
  
   [parameter(Mandatory=$true,ParameterSetName="RetainVM")]  
   [ValidateSet("Stop","Pause","SaveState","Store")]  
   [String[]]  
   $VMAction,  
  
   [parameter(Mandatory=$true)]  
   [String]  
   $Service=$(throw "Please provide the name of a service."),  
  
   [parameter(Mandatory=$true)]  
   [String]  
   $ComputerTier=$(throw "Please provide the name of a computer tier."),  
  
   [parameter(Mandatory=$false,ParameterSetName="RetainVM")]  
   [String]  
   $LibraryServer,  
  
   [parameter(Mandatory=$false,ParameterSetName="RetainVM")]  
   [String]  
   $LibraryPath  
   )  
  
# Get the service and the computer tier.  
$ServiceInstance = Get-SCService -Name $Service  
$Tier = Get-SCComputerTier -Service $ServiceInstance | where {$_.Name -eq $ComputerTier}  
  
# Ensure that you are able to scale the tier in.   
If ($Tier.VMs.count -le $Tier.InstanceMinimumCount) {throw "You have reached the instance minimum for this tier."}  
  
# Find the most recently created virtual machine in the tier that is running.  
$VMs = @(Get-SCVirtualMachine | where {$_.ComputerTier -eq $Tier -and $_.Status -eq "Running"}) | sort -Property CreationTime -Descending  
$VM = $VMs[0]  
  
If ($RemoveVM)  
{  
   Stop-SCVirtualMachine -VM $VM -Force  
   Remove-SCVirtualMachine -VM $VM  
}  
Else  
{  
   If ($VMAction -eq "Stop")  
   {  
      Stop-SCVirtualMachine -VM $VM  
   }  
  
   ElseIf ($VMAction -eq "Pause")  
   {  
      Suspend-SCVirtualMachine -VM $VM  
   }  
  
   ElseIf ($VMAction -eq "SaveState")  
   {  
      Stop-SCVirtualMachine -VM $VM -SaveState  
   }  
  
   ElseIf ($VMAction -eq "Store")  
   {  
      Save-SCVirtualMachine -VM $VM -LibraryServer $LibraryServer -SharePath $LibraryPath  
   }  
  
   Else  
   {  
      throw "You did not provide a valid action for the virtual machine."  
   }  
}  
  

See Also

Scale Out a Computer Tier (Script)