Resize Azure Virtual Machines in Parallel

Imagine you had to resize a whole bunch of VMs in Azure, rather than doing a resize one after the other in serial, you can do this in parallel with the new ForEach-Object Parallel feature in PowerShell v7. More details on this feature here – https://devblogs.microsoft.com/powershell/powershell-foreach-object-parallel-feature/

Doing this in parallel will greatly speed up the process by resizing any number of VMs all at once. To start with, you will need a CSV file with two columns, vmname,targetsize. Enter all the VM names of the VMs that you want to resize, also the correct VM size. 

Sample CSV file:

vmname,targetsize
VM01,Standard_DS14_v2
VM02,Standard_DS14_v2

To get a list of Azure VM sizes and the correct format for the name, run the following:

(Get-AzVMSize -Location 'Australia East').Name

This script will loop through all the VMs in the CSV file, shutting down the VM first before the resize happens. A full shutdown of the VM ensures that all target VM sizes are on offer across all Azure clusters, not just VM sizes of the current cluster in which your VM is a member of. First the script will get the status of the Private IP Allocation Method for the VM (Static or Dynamic) and if the private IP address is Dynamic, it will change it to a Static private address temporarily to make sure that the VM’s private address stays the same while it’s in a shut down state. Then after the resize happens, the VM is powered back up and if the Private IP Allocation Method was changed to Static part of this process, then it changed back to Dynamic, to keep the VM the same as it was originally. 

The full script can be found below and here, be sure to read the lines at the top to make sure you gave the correct version of PowerShell v7 and the correct version of the Az (Azure) PowerShell module. 

Leave a Reply

Please log in using one of these methods to post your comment:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s