Azure Virtual Desktop – Scaling Automation

Table of Contents

Introduction

Lorem ipsum dolor sit amet, consectetur adipiscing elit. Ut elit tellus, luctus nec ullamcorper mattis, pulvinar dapibus leo.

Requirements

Lorem ipsum dolor sit amet, consectetur adipiscing elit. Ut elit tellus, luctus nec ullamcorper mattis, pulvinar dapibus leo.

Services
Permissions

Automation Account

Lorem ipsum dolor sit amet, consectetur adipiscing elit. Ut elit tellus, luctus nec ullamcorper mattis, pulvinar dapibus leo.

PowerShell Runbook

Connect to Azure Subscription

This part of the script executes a Sign-In using the Service Principal that was created by adding the Azure RunAsConnection.

				
					## Connect to Azure using Run As Connection
$connectionName = "AzureRunAsConnection"
$connection = Get-AutomationConnection -Name $connectionName
try
{
    $connectionResult = Connect-AzAccount `
    -ServicePrincipal `
    -Tenant $connection.TenantID `
    -ApplicationId $connection.ApplicationID `
    -CertificateThumbprint $connection.CertificateThumbprint
    write-output "Login successful.."
    write-output $connectionResult
}
catch
{
    write-output "Login with $connectionName failed..."
    write-output $connectionResult
}
				

Auto Scaling Tag

This part of the script executes a Sign-In using the Service Principal that was created by adding the Azure RunAsConnection.

				
					## Declare local variables
$AutoScaleTag = "WVDAutoScale"
## Start processing of host pools
write-output "Getting HostPools with AutoScaling enabled..."
$varHostPoolsRscs = Get-AzResource -ResourceType Microsoft.DesktopVirtualization/hostpools | Where-Object {$_.Tags."$AutoScaleTag" -eq $true}
				

Get Azure Virtual Desktop Hostpool

This part of the script executes a Sign-In using the Service Principal that was created by adding the Azure RunAsConnection.

				
					if($varHostPoolsRscs.Count -gt 0)
{
    foreach ($varHostPoolsRsc in $varHostPoolsRscs)
    {
        Write-Output " "
        Write-Output "==============================================================="
        $varWvdCoreRscGroup = $varHostPoolsRsc.ResourceGroupName
        $Hostpool = Get-AzWvdHostPool -Name $varHostPoolsRsc.Name -ResourceGroupName $varWvdCoreRscGroup
        $HostPoolName = $Hostpool.Name 
        Write-Output "Found HostPool to AutoScale. [$HostPoolName] "
        ...
    }
}
				

Evaluate Hostpool Tags

This part of the script executes a Sign-In using the Service Principal that was created by adding the Azure RunAsConnection.

				
					if($varHostPoolsRscs.Count -gt 0)
{
    foreach ($varHostPoolsRsc in $varHostPoolsRscs)
    {
        ...
        Write-Output "Getting AutoScale Config from Hostpool tags..."
        $HostpoolTags = Get-AzResource -ResourceType Microsoft.DesktopVirtualization/hostpools | Where-Object {$_.Name -eq $varHostPoolsRsc.Name} | Select-Object -ExpandProperty Tags
        Write-Output "--------------------------------------------------------------"
        Write-Output "Check if all configuration values are set..."
        if($HostpoolTags."WVDUserBuffer")
        {
            [int32]$buffer = $HostpoolTags."WVDUserBuffer"
            Write-Output "Off-Peak User Buffer set to (Tag): $buffer"
        }
        else
        {
            [int32]$buffer = Get-AutomationVariable -Name 'wvduserbuffer'
            Write-Output "Off-Peak User Buffer set to (Default): $buffer"
        }
        if($HostpoolTags."WVDMinHosts")
        {
            [int32]$minhosts = $HostpoolTags."WVDMinHosts"
            Write-Output "Minimum Session Hosts set to (Tag): $minhosts"
        }
        else
        {
            [int32]$minhosts = Get-AutomationVariable -Name 'wvdminhosts'
            Write-Output "Minimum Session Hosts set to (Default): $minhosts"
        }
        if($HostpoolTags."WVDPeakUserBuffer")
        {
            [int32]$peakbuffer = $HostpoolTags."WVDPeakUserBuffer"
            Write-Output "Peak User Buffer set to (Tag): $peakbuffer"
        }
        else
        {
            [int32]$peakbuffer = Get-AutomationVariable -Name 'wvdpeakuserbuffer'
            Write-Output "Peak User Buffer set to (Default): $peakbuffer"
        }
        if($HostpoolTags."WVDPeakHours")
        {
            [string]$peakhour = $HostpoolTags."WVDPeakHours"
            Write-Output "Peak Hours set to (Tag): $peakhour"
        }
        else
        {
            [string]$peakhour = Get-AutomationVariable -Name 'wvdpeakhour'
            Write-Output "Peak Hours set to (Default): $peakhour"
        } 
        ...
    }
}
				

Get Azure Virtual Desktop Hostpool

This part of the script executes a Sign-In using the Service Principal that was created by adding the Azure RunAsConnection.

				
					if($varHostPoolsRscs.Count -gt 0)
{
    foreach ($varHostPoolsRsc in $varHostPoolsRscs)
    {
        Write-Output " "
        Write-Output "==============================================================="
        $varWvdCoreRscGroup = $varHostPoolsRsc.ResourceGroupName
        $Hostpool = Get-AzWvdHostPool -Name $varHostPoolsRsc.Name -ResourceGroupName $varWvdCoreRscGroup
        $HostPoolName = $Hostpool.Name 
        Write-Output "Found HostPool to AutoScale. [$HostPoolName] "
        ...
    }
}
				

Hostpool Configuration

Lorem ipsum dolor sit amet, consectetur adipiscing elit. Ut elit tellus, luctus nec ullamcorper mattis, pulvinar dapibus leo.

Resource Tags
Name Expected value Description
WVDAutoScale

True or False

Enables or disables scaling automation for the specific hostpool resource.
Content Content Content

Leave a Comment

Your email address will not be published. Required fields are marked *