Enable or Disable autoscaling on AVD hostpool with Powershell

I got a question from a customer if I could write a script that can enable or disable autoscaling for an AVD hostpool with PowerShell. So, I wrote the script and want to share it.

They needed to disable autoscaling before the deployment of new session hosts and the removal of old ones.

Prerequisites

You need the Az module for Powershell. You can install that by using this command in an administrative PowerShell window:

Install-Module Az

The script

Save this script as a .ps1 file:

Function Set-AVDHostPoolAutoScaling {
    [CmdletBinding()]
    Param(
        [Parameter(Mandatory = $true)]
        [string]$ResourceGroupName,
        [Parameter(Mandatory = $true)]
        [string]$ScalingPlanName,
        [Parameter(Mandatory = $true)]
        [string]$HostPoolResourceId,
        [Parameter(Mandatory = $true)]
        [bool]$EnableAutoScale

    )
    Begin {

        #To be filled later if needed

    }

    Process {

        Update-AzWvdScalingPlan `
            -ResourceGroupName $ResourceGroupName `
            -Name $ScalingPlanName `
            -HostPoolReference @(
            @{
                'hostPoolArmPath'    = $HostPoolResourceId;
                'scalingPlanEnabled' = $EnableAutoScale;
            }
        ) `
    
    }
}

After that, connect to Azure with this command:

Connect-AzAccount

*Optional* select the appropriate subscription:

Select-AzSubscription *Subscription ID* | Set-AzContext

Import the PowerShell function by using: (Path to file is the file we saved at the beginning of this blog)

. .\Pathto\File.ps1

Lastly, run the PowerShell function by using:

$ResourceGroupName = "RG_WE_AVD_HostPools"
$ScalingPlanName = "Test-ScalingPlan"
$HostPoolResourceId = "/subscriptions/SubscriptionID/resourceGroups/RG_WE_AVD_HostPools/providers/Microsoft.DesktopVirtualization/hostpools/AVD-Production"
$EnableAutoScale = $true

Set-AVDHostPoolAutoScaling -ResourceGroupName $ResourceGroupName -ScalingPlanName $ScalingPlanName -HostPoolResourceId $HostPoolResourceId -EnableAutoScale $EnableAutoScale

Output before:

Output after:

References

Microsoft Docs

Other Posts:
Set Page File via Bicep

Leave a Comment