Re-Size multiple Azure VM - Powershell

It happens many time that we need to re-size the Azure VM and its not a big deal to do it , we can easily do it from Portal. However if we need to do it for multiple VM's it becomes tiresome hence Powershell for rescue. moreover if we need to determine what all machine doesnt have the standard size even then little tweak in the below code would help us , lets check the code -

First figure out what all machine in entire subscription doesn't belong to particular size for e.g. if we need to figure out what all VM size is not "Standard_D2s_v3" . We can easily figure out by this code

$vms = get-azvm
foreach ( $vm in $vms ) {
        if ( $vm.hardwareprofile.vmsize -ne "Standard_D2s_v3") {
        write-host "need to change the size + "$($VM.name)""
      }
 }

Now if you do not want list or just want to change all the VM which doesnt belong to particular VM size you can add one more line in above code and voila !


$vms = get-azvm
$NewVMSize = "Standard_B2ms"
foreach ( $vm in $vms ) {
        if ( $vm.hardwareprofile.vmsize -ne "Standard_D2s_v3") {

        write-host "changing size + "$($VM.name)""
        $vm.HardwareProfile.vmSize = $NewVMSize
        Update-AzVM -ResourceGroupName $vm.ResourceGroupName -VM $vm

        }
        }

you can copy this code from here and use it as per your need, also you can download this from git hub below is the link -

https://github.com/ps-world/Azure-VM/blob/master/resizevm.ps1



No comments:

Post a Comment

Azure DevOps

Azure DevOps is a suite of development tools provided by Microsoft, designed to support the entire development lifecycle of a software proje...