Partager via


Resize Azure ARM virtual machine with PowerShell

I recently worked on a project where we needed to resize VMs in Azure to be able to scale up or down during different periods of the month. Since these VMs where created using Azure Resource Manager (ARM) we needed to use the new AzureRM PowerShell cmdlets. So naturally I tried Set-AzureRmVMSize, but to my surprise there was no such cmdlet. It turns out that it’s actually not supposed to work with the current version of AzureRM modules: https://social.msdn.microsoft.com/Forums/azure/en-US/0c16a10a-c4df-4d34-97c5-f0778cdef182/how-to-change-a-v2-arm-virtual-machine-size-using-powershell?forum=windowsazuremanagement

But then I found the following blog post by Igor Pagliai: https://blogs.msdn.com/b/igorpag/archive/2015/11/04/azure-vm-resizing-and-sku-change-in-asm-and-arm.aspx

So with just a little modification of the example he provides its really quite simple to achieve.

Note that it’s only necessary to stop the VM if you're also changing SKU when doing the resize, read more about that on Igor's post above. It also describes what you need to think about regarding storage etc. Note that if changing size within the same SKU the VM will still reboot.

So below is a simple script to resize a VM with the AzureRM modules.


Login-AzureRmAccount

$ResourceGroupName = "CMLAB"

$VMName = "2007CMCEN"

$NewVMSize = "Standard_A5"

 

$vm = Get-AzureRmVM -ResourceGroupName $ResourceGroupName -Name $VMName

$vm.HardwareProfile.vmSize = $NewVMSize

Update-AzureRmVM -ResourceGroupName $ResourceGroupName -VM $vm