Resize Azure VM w/PowerShell

Cover

This is anniversary 50-th article in my blog.

If you had selected wrong VMSize while VM deployment or your workload eventually changed – you have to resize your VM.

To make decision on selecting well-becoming VMSize for your workload you can read my previous article.

The Microsoft’s way

  • The Microsoft suggests semi-manual workaround, you have to make a number of «preflight» checks and take into account several conditions. Is your VM standalone or Availability Set member? If yes, who are other members? Is it deallocated or powered on? Is desired VMSize available in the current cluster?

  • I suggest you fully-automated solution – a ready-made PowerShell function.

Set-AzVmSize

  • The Set-AzVmSize is a part of Azure Automation Az-Module. It can resize a single VM, either group of VM or all VM under any Resource Group or Subscription. The function supports pipeline.

$RG = Select-AzResourceGroup
Get-AzureRmVM -ResourceGroupName $RG -Name azvm1 | Set-AzVmSize -Size Standard_A2
(Get-AzureRmVM -ResourceGroupName $RG).Where{$_.Name -like azvm*} | Set-AzVmSize -Size Standard_A2
(Get-AzureRmVM -ResourceGroupName $RG).Where{$_.Name -like azvm*} | Set-AzVmSize -Size Standard_A2
(Get-AzureRmVM -ResourceGroupName $RG).Where{'azvm1', 'azvm2' -contains $_.Name} | Set-AzVmSize -Size Standard_A2
Get-AzureRmVM -ResourceGroupName $RG | Set-AzVmSize -Size Standard_A2
Get-AzureRmVM | Set-AzVmSize -Size Standard_A2
  • Let’s take for example a single Basic_A0 VM and upsize it to the Standard_A0.
(Get-AzureRmVM -ResourceGroupName $RG -Name azvm1).HardwareProfile.VmSize
Get-AzureRmVM -ResourceGroupName $RG -Name azvm1 | Set-AzVmSize -Size Standard_A0
  • There is an option also to downsize VM. Note the -Size parameter is positional and may be omitted.
(Get-AzureRmVM -ResourceGroupName $RG -Name azvm1).HardwareProfile.VmSize
Get-AzureRmVM -ResourceGroupName $RG -Name azvm1 | Set-AzVmSize Basic_A1

04.Set-AzVmSize_StandaloneVM

  • Please note all above resizes we did on running VM! But may be situations when the Azure must move your VM to another hardware cluster to apply the desired VMSize. The Set-AzVmSize can identify this situation and deallocate VM prior to resizing.

05.Set-AzVmSize_StandaloneVM

Availability Set VM

  • If target VM is Availability Set member and it has to be deallocated to resize, we will have to deallocate all Availability Set members! Despite the Set-AzVmSize will do that for you, I recommend to deallocate all AS VM manually before resizing. This will save multiple deallocate-start cycles and dramatically decrease the resizing time.
(Get-AzureRmVM -ResourceGroupName $RG).Where{$_.Name -match '^azvm'} | Stop-AzureRmVM -Confirm:$false –Force
(Get-AzureRmVM -ResourceGroupName $RG).Where{$_.Name -match '^azvm'} | Set-AzVmSize Basic_A1 –Verbose
  • To suppress confirmation dialogs, use -Confirm:$false parameter.
(Get-AzureRmVM -ResourceGroupName $RG).Where{$_.Name -match '^azvm'} | Set-AzVmSize Basic_A2 –Confirm:$false

08.Set-AzVmSize_AS

Start-AzureRmVM

  • If Set-AzVmSize did need to deallocate some VM before resizing, it will power them on after resize is complete. The power on is executed by embedded Start-AzureRmVM cmdlet from AzureRM module.
  • From recent times the Start-AzureRmVM at last Emoj supports -AsJob parameter. It depends on your AzureRM module version. If your version does not support parallel VM processing, the VM will be powered on in sequence, otherwise a client side PSJob will be created for every VM. To get the job status, use Get-Job cmdlet.

(Get-AzureRmVM -ResourceGroupName $RG).Where{$_.Name -match '^azvm'} | Get-AzVmPowerState
(Get-AzureRmVM -ResourceGroupName $RG).Where{$_.Name -match '^azvm'} | Start-AzureRmVM –AsJob
Get-Job -Id 258,259

09.Set-AzVmSize_AS

  • The above example about Start-AzureRmVM and its -AsJob parameter is proof of concept and intended for your reference only. All this logic is done automatically by the Set-AzVmSize function!

Summary

  • The Set-AzVmSize is advanced function and supports set of standard advanced parameters such as -Confirm or -Verbose.
Get-AzureRmVM -ResourceGroupName $RG -Name azvm1 | Set-AzVmSize -Size Basic_A1 -Confirm:$false –Verbose
  • For more details about the functions, please take a look at the content based help and examples.
Get-Help Set-AzVmSize -Full
Get-Help Set-AzVmSize -Examples
Get-Help Set-AzVmSize -Parameter Size

You might also like

Get-AzVmSize – Select right Azure VM Size
Azure Automation – How to stop/start Azure VM on schedule
Azure VM Tag automation
How to login to the Azure automatically

Leave a Reply

Fill in your details below or click an icon to log in:

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 )

w

Connecting to %s