Let’s save the cost by automating the AVD (Azure Virtual Desktop) VM Start on Connect

If you not opted for the 3 years contract with Microsoft for the reserved instances for your AVD Hostpool and you are using the dedicated session hosts, VM start on connect is an excellent way to save the cost. Today I will show you how you can automate that. This was first announced in MS Ignite in Sept 2020, this feature Enable deallocated VMs to start automatically when a user connects. (Allows administrators to shutdown VMs during off-peak hours to save cost while ensuring that the VMs will start up automatically if a user tries to connect to it.)

Also I can see the VM start on connect is showing NO for this Host Pool.

In the first step you need to create a new script. The below script will create a custom role in Azure RBAC, The role name is “Start VM on connect”. In this custom role I have given the user to the permissions to start the Virtual machine and read the contents of the virtual machines.

$role = Get-AzRoleDefinition -Name "Virtual Machine Contributor"

$role.Id = $null

$role.Name = "Start VM on connect"

$role.Description = "This is start the VM on Connect Role for AVD"

$role.Actions.RemoveRange(0,$role.Actions.Count)

$role.Actions.Add("Microsoft.Compute/virtualMachines/start/action")

$role.Actions.Add("Microsoft.Compute/virtualMachines/read")

$role.AssignableScopes.Clear()

$role.AssignableScopes.Add("/subscriptions/22ab9372-1faf-4756-a709-1af49be58e56")

New-AzRoleDefinition -Role $role

Once you run this script the following role has been created in your Azure Subscription.

In the next step you need assign this role to the user whom you dedicate this session host, so that he can start the VM on connect.

function take_input() {

    param

    (

    [Parameter(Position = 0, ValueFromPipeline = $true)]

    [string]$msg,

    [string]$BackgroundColor = "Black",

    [string]$ForegroundColor = "Yellow"

    )

    

    Write-Host -ForegroundColor $ForegroundColor -NoNewline $msg;

    return Read-Host

    }

    $prompt1 = take_input "Enter the UPN of the user:"

    $upn = $prompt1

    $prompt2 = take_input "Enter the resource group name of the AVD Pool:"

    $rgName = $prompt2

    $prompt3 = take_input "Enter the role definition:"

    $roledefinition = $prompt3

    $prompt4 = take_input "Enter the host pool name:"

    $hostpoolname = $prompt4

    try {

New-AzRoleAssignment -SignInName $upn `

-ResourceGroupName $rgName `

-RoleDefinitionName $roledefinition

        }

        catch {

        Write-Host "A parameter not supplied please try again." -ForegroundColor Red

    

    }

    Start-Sleep -s 15

    Update-AzWvdHostPool -ResourceGroupName $rgName -Name $hostpoolname -StartVMOnConnect:$true 

Also in the last part of the script we have enabled the Hostpool for VM Start on Connect.

From the Azure Portal you can view the VM start on connect is enabled.

That’s all for today. You can implement this script as an Azure Automation runbook  which you can create. Thanks for reading this post, you have a great day ahead.

0.00 avg. rating (0% score) - 0 votes
Tags:,