Powershell script to change the InstanceSize for a bunch of Azure Virtual Machines


Hi All

Lately I have been living the Azure dream, madly trying to build every possible Skype and Lync environment my customers have because I can, and because it is fun. However, you like me and you don’t keep an eye on the utilisation it is very easily to get carried away and have way too many medium, large and extralarge virtual machines out there that are burning through your hours in Azure *embarrassed grin*.

As I have a stack of VMs, I thought it would be handy to write a script to cycle through all my VMs that meet some criteria and to swap them over to a particular InstanceSize. I also wanted to leave certain VMs alone so I have also included an ExcludeFilter. This is just a quick and dirty script so please be careful and test it before running against any production Azure workload.

Enjoy and Happy Skype’ing/Azure’ing

Steve

 

#I have decided to convert all VMs I find to a Small instancesize. You can change this to suit your needs.
$strNewSize=”Small”

$strServiceName=”YOUR-CLOUD-SERVICE-1″
#get Vms matching this string
$strFilter=”lab1″
#Exclude VMs matching this string (notice has multiple filters that are matched, the | is a OR operator)
$strExcludeFilter=”SVR1|SVR2|-SFB”

cls

$i=0

get-azurevm -ServiceName $strServiceName | where {$_.name -match $strFilter -and $_.name -notmatch $strExcludeFilter} | sort -property name |%{

    $strServiceName=$_.ServiceName
    $strVM=$_.Name
    $strSize=$_.InstanceSize
    $vm=$_

    $strDateTime=get-date -UFormat “%d-%m-%Y %H:%M”
    write-host “$strDateTime$i – Checking $strVM to see size change is required!”

    switch ($strNewSize) {
        “ExtraSmall” {
            If ($strSize -ne $strNewSize) {
                write-host “$strDateTime $i – Updating $strVM to $strNewSize from $strSize”           
                $vm | Set-AzureVMSize ExtraSmall | Update-AzureVM
            }
        }
        “Small” {
            If ($strSize -ne $strNewSize) {
                write-host “$strDateTime $i – Updating $strVM to $strNewSize from $strSize”           
                $vm | Set-AzureVMSize Small | Update-AzureVM
            }
        }
        “Medium” {
            If ($strSize -ne $strNewSize) {
                write-host “$strDateTime  $i – Updating $strVM to $strNewSize from $strSize”           
                $vm | Set-AzureVMSize Medium | Update-AzureVM
            }
        }
        “Large” {
            If ($strSize -ne $strNewSize) {
                write-host “$strDateTime  $i – Updating $strVM to $strNewSize from $strSize”           
                $vm | Set-AzureVMSize Large | Update-AzureVM
            }
        }     
        “ExtraLarge” {
            If ($strSize -ne $strNewSize) {
                write-host “$strDateTime  $i – Updating $strVM to $strNewSize from $strSize”           
                $vm | Set-AzureVMSize ExtraLarge | Update-AzureVM
            }
        }

    }
    $i++
   
}

This is what the output of the script looks like.

Comments (0)

Skip to main content