Menu Close

How to deal with AVD session hosts version management

Last updated 3 months ago by Sander Rozemuller

A Shared Image Gallery is a great way distributing images over multiple environments. This great feature is also its disadvantage. When using a Shared Image Gallery in combination with several environments with even more host pools you will loose sight fast. At this moment there is no good overview which host in which environment is using which version.
In this article I will explain how to deal with AVD session hosts and version management. This will help to keep you environment clean and save costs.

We all where in a situation where image versioning wasn’t that necessary. But at this time we all know better and creating versions over and over. And we all agree that versioning reduces downtime and gives us options to revert quickly.
Also Microsoft thought it was a good idea to provide us an image versioning option. Where Citrix is using Citrix Provisioning Microsoft has the Azure Shared Image gallery.

I have worked a lot with both and still do (mostly on the Microsoft side). During the years I experienced it could be hard to manage all those environments with all of their versions. It really becomes hard when you have multiple tenants with environments. And at last I becomes almost impossible when you have set some AVD session hosts in drain mode (not accepting new connections) and deployed new session hosts with a newer version in the same host pool. It is just a matter of time becoming a mess.

In this article I will show how to deal with AVD session hosts and version management. I will show you how to get all the session host information with the image versions, how to set drain mode and how to clean up old session hosts.

Table of contents

Getting sight

Lets clear the air and make sure we are getting sight on our environment. The main idea is to get all the session host information in combination with the underlying image image source. To get sight without a writing a lot of code every time I will recommend using the Az.Avd PowerShell module. This module will you managing AVD environments.

Install module Az.Avd

If you haven’t already installed the Az.Avd module then is time to do it right now. As I said it really helps you managing your AVD environments.

Because of the module is available in the official PowerShell gallery it is very easy to install the module by the following command. After installing don’t forget to import the module as well.

Install-Module Az.Avd
Import-Module Az.Avd

So far so good.

Get AVD image version status

After the module is installed a whole new set of AVD PowerShell command become available. One of them is Get-AvdImageVersionStatus. This command accepts a few parameters and will look into the environment for all the needed information.

SYNTAX
     Get-AvdImageVersionStatus -HostpoolName  -ResourceGroupName  [-NotLatest] []
     Get-AvdImageVersionStatus -HostpoolName <String> -ResourceGroupName <String> [-SessionHostName <String>] [-NotLatest] [<CommonParameters>]

For all help information use the command Get-Help Get-AvdImageVersionStatus -Full

Now lets see the command in action. I’m searching in a AVD hostpool for all the session hosts with their image version.

Get-AvdImageVersionStatus -HostpoolName avd-hostpool -ResourceGroupName rg-avd-001 | Select vmLatestVersion, hostpoolName, sessionhostName, currentImageVersion, lastVersion

The result of the command is shown in the picture below. As you can see I have three different image version in one hostpool. This is a situation which can result in very strange user experience which can take a lot of research time.
The red and orange hosts aren’t at the last version where they also have a different version in relation to eachother.

The green host if fine.

Drain mode

To clean up the mess it is recommended to getting the users to correct session hosts. Putting the old session hosts into drain mode is the first step. Drain mode avoids users logging in at one the old hosts.

Putting hosts into drain mode is simple with the Az.Avd PowerShell module. In the first step I’m only requesting the old session hosts with the -NotLatest switch parameter. The results will be piped to an another command Update-AvdSessionhostDrainMode.

Get-AvdImageVersionStatus -HostpoolName avd-hostpool -ResourceGroupName rg-avd-001 -NotLatest | foreach {Update-AvdSessionhostDrainMode -HostpoolName $_.Hostpoolname -ResourceGroupName rg-avd-001 -SessionHostName $_.sessionHostName -AllowNewSession $true}

Clean up old resources

Now the session hosts are in drain mode it is a matter of time till the hosts are empty. At the moment the hosts are empty it is time to clean up the resources. First we will delete the session host from the host pool. Later we are going to remove the virtual machine, the disk and the network card.


Get-AvdImageVersionStatus -HostpoolName avd-hostpool -ResourceGroupName rg-avd-001 -NotLatest | foreach {
    $sessionHostInfo = @{
        HostpoolName = $_.HostpoolName
        ResourceGroupName = "rg-avd-001" 
        SessionHostName = $_.SessionHostName
    }
    
    $sessionHost = Get-AvdSessionHostResources @sessionHostInfo
    Remove-AzResource -ResourceId $sessionHost.Id -Force
    Remove-AzResource -ResourceId $sessionHost.StorageProfile.OsDisk.ManagedDisk.id -Force
    Remove-AzResource -ResourceId (Get-AvdNetworkInfo @sessionHostInfo).nicId -Force
}

The next step is removing the session hosts from the hostpool.

With the command below we are deleting the AVD session host from the host pool. This command is also in the Az.Avd PowerShell module.

Get-AvdImageVersionStatus -HostpoolName avd-hostpool -ResourceGroupName rg-avd-001 -NotLatest | foreach {Remove-AvdSessionhost -HostpoolName $_.Hostpoolname -ResourceGroupName rg-avd-001 -SessionHostName $_.sessionHostName}

And now the old sessionhosts are removed.

Thank you for reading my post about AVD session hosts and version management.

Leave a Reply

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