Редактиране

Споделяне чрез


Manage a Virtual Machine Scale Set with Azure PowerShell

Note

Many of the steps listed in this document apply to Virtual Machine Scale Sets using Uniform Orchestration mode. We recommend using Flexible Orchestration for new workloads. For more information, see Orchesration modes for Virtual Machine Scale Sets in Azure.

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.

If you need to create a Virtual Machine Scale Set, you can create a scale set with Azure PowerShell.

Note

We recommend that you use the Azure Az PowerShell module to interact with Azure. To get started, see Install Azure PowerShell. To learn how to migrate to the Az PowerShell module, see Migrate Azure PowerShell from AzureRM to Az.

View information about a scale set

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

Get-AzVmss -ResourceGroupName "myResourceGroup" -VMScaleSetName "myScaleSet"

View VMs in a scale set

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

Get-AzVmssVM -ResourceGroupName "myResourceGroup" -VMScaleSetName "myScaleSet"

To view additional information about a specific VM instance, add the -InstanceId parameter to Get-AzVmssVM 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-AzVmssVM -ResourceGroupName "myResourceGroup" -VMScaleSetName "myScaleSet" -InstanceId "0"

You can also get detailed instanceView information for all instances in one API call, which can help avoid API throttling for large installations.

Get-AzVmssVM -InstanceView -ResourceGroupName "myResourceGroup" -VMScaleSetName "myScaleSet"
GET "https://management.azure.com/subscriptions/<sub-id>/resourceGroups/<resourceGroupName>/providers/Microsoft.Compute/virtualMachineScaleSets/<VMSSName>/virtualMachines?api-version=2019-03-01&%24expand=instanceView"

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-AzVmss, then specify a new value for sku.capacity. To apply the capacity change, use Update-AzVmss. 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-AzVmss -ResourceGroupName "myResourceGroup" -VMScaleSetName "myScaleSet"

# Set and update the capacity of your scale set
$vmss.sku.capacity = 5
Update-AzVmss -ResourceGroupName "myResourceGroup" -Name "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-AzVmss. 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 own values as follows:

Stop-AzVmss -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-AzVmss. 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 own values as follows:

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

Restart VMs in a scale set

To restart one or more VMs in a scale set, use Restart-AzVmss. 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 own values as follows:

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

Remove VMs from a scale set

To remove one or more VMs in a scale set, use Remove-AzVmss. 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 own values as follows:

Remove-AzVmss -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.