Change size for Virtual Machines in availability set

  • October 27, 2017
  •  
  • November 25, 2018

To change VM Size when your virtual machines are part of an availability set can be a pain in the Azure Portal. Especially if the new size you want is not available on your current cluster and the resources have to be moved as well.

The entire process is a lot easier to do with powershell

Login-AzureRmAccount
 
$rg_name = "RG-ResizeDemo"
$as_name = "AS-ResizeDemo"
$new_vm_size = "Standard_B2s"
$as = Get-AzureRmAvailabilitySet -ResourceGroupName $rg_name
$vm_ids = $as.VirtualMachinesReferences
 
foreach ($vm_id in $vm_ids) {
    $s = $vm_id.Id.Split("/")
    $vm_name = $s[8]
    Stop-AzureRmVM -ResourceGroupName $rg -Name $vm_name -Force
}
 
foreach ($vm_id in $vm_ids) {
    $s = $vm_id.Id.Split("/")
    $vm_name = $s[8]
    $vm = Get-AzureRmVM -ResourceGroupName $rg -name $vm_name
    $vm.HardwareProfile.VmSize = $new_vm_size
    Update-AzureRmVM -ResourceGroupName $rg -VM $vm
    Start-AzureRmVM -ResourceGroupName $rg -Name $vm_name
}