Resize Azure virtual machine

Changing the VM size is straight forward, you can change the size of a Azure virtual machine by using either the Azure Management Portal or PowerShell commands. However we often come across a strange yet descriptive error message. i.e. ” Storage account of type “Premium_LRS” is not support for VM size “Standard”.

Let’s have a look on how can we automate the process using PowerShell Command-lets.

The above error occurs when you have selected Premium Storage (SSD) during provisioning and you try to change it to a plan that supports Standard Storage (HDD).

The Solution is very straight Forward, you need to change the OS disk first and then the VMSize. Complete Powershell script below.

param(
[String]$VMname,
[String]$ResourceGroupName,
[String]$VmSize,
[String]$diskname,
[ValidateSet("StandardLRS","PremiumLRS")]
[String]$acctype
)

function Resize-vm($VMname,$ResourceGroupName,$VmSize,$diskname,$acctype)
{
#Stop the VM for which you need to change the size for.
Stop-AzureRmVM -ResourceGroupName $ResourceGroupName -Name $VMname
#Store the Disk in the Variable
$disk =  Get-AzureRmDisk -ResourceGroupName $ResourceGroupName -DiskName $diskname
#Change the Disk to Standard Storage
$disk.AccountType = $acctype
#Update the Azure Disk
Update-AzureRmDisk -ResourceGroupName $ResourceGroupName -DiskName $diskname -Disk $disk
#Now it's time to change the VM size.
$vm = Get-AzureRmVM -ResourceGroupName $ResourceGroupName -Name $VMname
$vm.HardwareProfile.VmSize = $VmSize
Update-AzureRmVM -ResourceGroupName $ResourceGroupName -VM $vm
#Start your Azure VM
Start-AzureRmVM -ResourceGroupName $ResourceGroupName  -Name $VMname
}
Resize-vm -VMname "$VMname" -ResourceGroupName $ResourceGroupName -VmSize $VmSize -diskname $diskname -acctype $acctype

Congrats you have successfully resized your VM.

Download the script at : Resize AzureRm Virtual Machine

Leave a comment