fbpx

How To Build Virtual Machine Scale Sets With Azure PowerShell? 1 Best Example

It’s possible that you’ll need to do one or more management tasks at some point throughout the life of a virtual machine scale set. You might also want to write scripts to automate different lifecycle tasks. Some of the popular Azure PowerShell cmdlets that enable you to carry out these activities are described in this article.

Virtual Machine Scale Sets

Using PowerShell to Create Azure Scale Sets

  • The first thing you need to do is use the New-AzVmss cmdlet to build an Azure VM scale set.
  • A scale set is made up of many resources, including a virtual network, load balancer, public IP address, and the VM instances themselves, to manage high availability and redundancy. This is all taken care of by the New-AzVMss cmdlet.
  • When using the New-AzVmss cmdlet to create a VM scale set, you have two choices.
  • You can either supply each configuration option separately or use the basic parameter set, which quickly generates a pre-configured scale set and assumes much of the configuration for you.

How to Build Azure Scale Sets with PowerShell?

If you want to design a straightforward scale set, you’ll focus on defining each resource separately. Only two parameters—Credential and VMScaleSetName—are required to do this. A PSCredential object for the local administrator account of each VM instance serves as the credential parameter.

Below is an example of how to use PowerShell to build each element needed for a set.

$vmssName = 'vmssScaleSet-simple'
$vmPassword = ConvertTo-SecureString '<somepassword>' -AsPlainText -Force
$vmCred = New-Object System.Management.Automation.PSCredential('<somelocaladminaccountname>', $vmPassword)
## Create a VMSS using the default settings
New-AzVmss -Credential $vmCred -VMScaleSetName $vmssName
Build Azure Scale Sets with PowerShell

Example: Create an Azure VM Scale Set

1. View VMs in a scale set

To view a list of VM instances in a scale set, use Azure Get-AzVmssVM. The example below lists every virtual machine instance in the resource group myResourceGroup and myScaleSet. 

Get-AzVmssVM -ResourceGroupName "myResourceGroup" -VMScaleSetName "myScaleSet"
View VMs in a scale set

To access extra information about a single VM instance, add the -InstanceId option to Get-AzVmssVM and choose an instance to inspect. The following example views information about VM example 0 in the set termed myScaleSet and the myResourceGroup resource group.

Get-AzVmssVM -ResourceGroupName "myResourceGroup" -VMScaleSetName "myScaleSet" -InstanceId "0"
Change the capacity of a scale set

You can also retrieve comprehensive instanceView information for all instances in one API call, which can help prevent API throttling for large setups.

Get-AzVmssVM -InstanceView -ResourceGroupName "myResourceGroup" -VMScaleSetName "myScaleSet"
Change the capacity of a scale set
GET "https://management.azure.com/subscriptions//resourceGroups//

2. Change the capacity of a scale set

To raise or decrease the number of instances in the set, you can adjust the capacity. The scale set automatically produces or removes the needed number of VMs, then configures the VMs to receive application traffic.

First, construct a set object with Get-AzVmss, then specify a new value for sku.capacity. To install the capacity update, use Update-AzVmss. The following example upgrades myScaleSet in the myResourceGroup resource group to a limit of 5 instances

# 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
image 117

3. Stop and start VMs in a scale set

Use Stop-AzVmss to stop one or more VMs in a scale set. You can specify a single or several VMs to shutdown using the -InstanceId argument. All VMs in the set are halted if an instance ID is not specified. Use a comma to divide each instance ID in order to terminate multiple VMs.

The example below terminates instance 0 in the resource group myResourceGroup and scale set myScaleSet. the following values, which are your own:

Stop-AzVmss -ResourceGroupName "myResourceGroup" -VMScaleSetName "myScaleSet" -InstanceId "0"
Stop and start VMs in a scale set

Stopped VMs are dealtlocated by default and do not incur compute fees. Add the -StayProvisioned argument to the previous command if you want the VM to keep its provisioned status after being stopped. Regular compute fees apply to provisioned but stopped VMs.

Begin the VMs in a scale set.

Use Start-AzVmss to launch one or more VMs in a set. You can specify one or more virtual machines to start by using the -InstanceId argument. All of the VMs in the scale are started if an instance ID is not specified. Use a comma to divide each instance ID in order to launch several VMs.

The next example begins instance 0 in the resource group myResourceGroup and scale myScaleSet.

Start-AzVmss -ResourceGroupName "myResourceGroup" -VMScaleSetName "myScaleSet" -InstanceId "0
Begin the VMs in a scale set

4. Restart each VM in the scale set.

Use Restart-AzVmss to restart one or more VMs in a set. You can specify one or more virtual machines to restart by using the -InstanceId argument. All of the VMs in the set are restarted if an instance ID is not specified. Use a comma to separate the instance IDs of each VM you want to restart.

The example below restarts instance 0 in the myResourceGroup resource group and the set named myScaleSet. the following values, which are your own:

Restart-AzVmss -ResourceGroupName "myResourceGroup" -VMScaleSetName "myScaleSet" -InstanceId
Restart each VM in the scale set.