Manage a virtual machine scale set with Azure PowerShell

Throughout the lifecycle of a virtual machine scale set, you may need to run one or more management tasks. Additionally, you may want to create scripts that automate various lifecycle-tasks. This article details some of the common Azure PowerShell cmdlets that let you perform these tasks.

To complete these management tasks, you need the latest Azure PowerShell module. For information on how to install and use the latest version, see Getting started with Azure PowerShell. If you need to create a virtual machine scale set, you can create a scale set in the Azure portal.

View information about a scale set

To view the overall information about a scale set, use Get-AzureRmVmss. The following example gets information about the scale set named myScaleSet in the myResourceGroup resource group. Enter your own names as follows:

Get-AzureRmVmss -ResourceGroupName "myResourceGroup" -VMScaleSetName "myScaleSet"

View VMs in a scale set

To view a list of VM instance in a scale set, use Get-AzureRmVmssVM. The following example list all VM instances in the scale set named myScaleSet and in the myResourceGroup resource group. Provide your own values for these names:

Get-AzureRmVmssVM -ResourceGroupName "myResourceGroup" -VMScaleSetName "myScaleSet"

To view additional information about a specific VM instance, add the -InstanceId parameter to Get-AzureRmVmssVM and specify an instance to view. The following example views information about VM instance 0 in the scale set named myScaleSet and the myResourceGroup resource group. Enter your own names as follows:

Get-AzureRmVmssVM -ResourceGroupName "myResourceGroup" -VMScaleSetName "myScaleSet" -InstanceId "0"

Change the capacity of a scale set

The preceding commands showed information about your scale set and the VM instances. To increase or decrease the number of instances in the scale set, you can change the capacity. The scale set automatically creates or removes the required number of VMs, then configures the VMs to receive application traffic.

First, create a scale set object with Get-AzureRmVmss, then specify a new value for sku.capacity. To apply the capacity change, use Update-AzureRmVmss. The following example updates myScaleSet in the myResourceGroup resource group to a capacity of 5 instances. Provide your own values as follows:

# Get current scale set
$vmss = Get-AzureRmVmss -ResourceGroupName "myResourceGroup" -VMScaleSetName "myScaleSet"

# Set and update the capacity of your scale set
$vmss.sku.capacity = 5
Update-AzureRmVmss -ResourceGroupName "myResourceGroup" -VMScaleSetName "myScaleSet" -VirtualMachineScaleSet $vmss 

If takes a few minutes to update the capacity of your scale set. If you decrease the capacity of a scale set, the VMs with the highest instance IDs are removed first.

Stop and start VMs in a scale set

To stop one or more VMs in a scale set, use Stop-AzureRmVmss. The -InstanceId parameter allows you to specify one or more VMs to stop. If you do not specify an instance ID, all VMs in the scale set are stopped. To stop multiple VMs, separate each instance ID with a comma.

The following example stops instance 0 in the scale set named myScaleSet and the myResourceGroup resource group. Provide your values as follows:

Stop-AzureRmVmss -ResourceGroupName "myResourceGroup" -VMScaleSetName "myScaleSet" -InstanceId "0"

By default, stopped VMs are deallocated and do not incur compute charges. If you wish the VM to remain in a provisioned state when stopped, add the -StayProvisioned parameter to the preceding command. Stopped VMs that remain provisioned incur regular compute charges.

Start VMs in a scale set

To start one or more VMs in a scale set, use Start-AzureRmVmss. The -InstanceId parameter allows you to specify one or more VMs to start. If you do not specify an instance ID, all VMs in the scale set are started. To start multiple VMs, separate each instance ID with a comma.

The following example starts instance 0 in the scale set named myScaleSet and the myResourceGroup resource group. Provide your values as follows:

Start-AzureRmVmss -ResourceGroupName "myResourceGroup" -VMScaleSetName "myScaleSet" -InstanceId "0"

Restart VMs in a scale set

To restart one or more VMs in a scale set, use Retart-AzureRmVmss. The -InstanceId parameter allows you to specify one or more VMs to restart. If you do not specify an instance ID, all VMs in the scale set are restarted. To restart multiple VMs, separate each instance ID with a comma.

The following example restarts instance 0 in the scale set named myScaleSet and the myResourceGroup resource group. Provide your values as follows:

Restart-AzureRmVmss -ResourceGroupName "myResourceGroup" -VMScaleSetName "myScaleSet" -InstanceId "0"

Remove VMs from a scale set

To remove one or more VMs in a scale set, use Remove-AzureRmVmss. The -InstanceId parameter allows you to specify one or more VMs to remove. If you do not specify an instance ID, all VMs in the scale set are removed. To remove multiple VMs, separate each instance ID with a comma.

The following example removes instance 0 in the scale set named myScaleSet and the myResourceGroup resource group. Provide your values as follows:

Remove-AzureRmVmss -ResourceGroupName "myResourceGroup" -VMScaleSetName "myScaleSet" -InstanceId "0"

Next steps

Other common tasks for scale sets include how to deploy an application, and upgrade VM instances. You can also use Azure PowerShell to configure auto-scale rules.