Resizing a virtual machine (VM) in Azure is a simple feat if you know your PowerShell. Luckily, in this article, I'll be showing you how to create a PowerShell script to resize your Azure VMs that are either in an availability set or not.

First, to manage anything in Azure with PowerShell, you'll need to get your hands on the Az PowerShell module. To do this, run Install-Module Az. This will download the module and make it available in your PowerShell session. I'm also going to assume you have an Azure VM either in or not in an availability set. With that out of the way, let's look at how to resize Azure VMs with PowerShell!

Listing available sizes ^

Before we can resize a VM, we must first know which sizes are available. To do this, we can run the Get-AzVmSize command against the VM in question. I'll be working with a VM called CLIENT in this example. CLIENT is in the LAB resource group.

Get-AzVMSize -ResourceGroupName 'LAB' -VMName CLIENT
Listing VM sizes

Listing VM sizes

Resizing a VM not in an availability set ^

You can see we've got a lot of sizes to choose from. If you see a size you'd like, first create an instance of the VM using Get-AzVm.

$vm = Get-AzVM -ResourceGroupName 'LAB' -VMName 'CLIENT'

Next, change the VmSize property on that VM instance to the new size. In my case, I'll randomly pick the Standard_D3 size.

$vm.HardwareProfile.VmSize = 'Standard_D3'

Once I've saved the VM instance variable with the new size in my PowerShell session, it's then time to send that change up to Azure by running Update-AzVM and providing the VM instance and the resource group name the VM is in.

Update-AzVM -VM $vm -ResourceGroupName 'LAB'

This may take a bit to run as it does not work in the background. If you wanted to run this command in the background, you have the option of using the AsJob parameter.

Once this is complete, you should see an output like the one below:

RequestId IsSuccessStatusCode StatusCode ReasonPhrase
--------- ------------------- ---------- ------------
                         True         OK OK

At this point, we can now check the VM size again by inspecting the VmSize property on the HardwareProfile object that's part of the VM instance object.

PS> (Get-AzVM -ResourceGroupName 'LAB' -VMName 'CLIENT').HardwareProfile.VmSize

Standard_D3

Resizing a VM in an availability set ^

Finally, we can also resize VMs in an availability set using a similar approach. If a VM is in an availability set and the size you need is not available when running Get-AzVmSize, you can still change the size if you shut down all VMs in the availability set, update the sizes of all VMs, and then start up the VMs again.

We'll first find all of the VMs in the availability set and stop them all.

$as = Get-AzAvailabilitySet -ResourceGroupName 'LAB'
$vmIds = $as.VirtualMachinesReferences
foreach ($vmId in $vmIDs){
    $string = $vmID.Id.Split("/")
    $vmName = $string[8]
    Stop-AzVM -ResourceGroupName 'LAB' -Name $vmName -Force
}

After stopping all the VMs, we can now use the same method we used earlier for updating the VMs—but this time we update all the VMs in the availability set.

Subscribe to 4sysops newsletter!

$as = Get-AzAvailabilitySet -ResourceGroupName 'LAB'
$vmIds = $as.VirtualMachinesReferences
foreach ($vmId in $vmIDs){
    $string = $vmID.Id.Split("/")
    $vmName = $string[8]
    $vm = Get-AzVM -ResourceGroupName 'LAB' -Name $vmName
    $vm.HardwareProfile.VmSize = 'Standard_D3'
    Update-AzVM -ResourceGroupName 'LAB' -VM $vm
Start-AzVM -ResourceGroupName 'LAB' -Name $vmName
}

 

avatar