How to deal with AVD session hosts version management

 How to deal with AVD session hosts version management

Last updated 2 months ago by Sander Rozemuller

A Shared Image Gallery is a great way to distribute 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 lose sight fast. At this moment there is no good overview of 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 your environment clean and save costs.

We all were in a situation where image versioning wasn’t that necessary. But at this time we all know better and create 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 with 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 site). 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, it 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

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

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 manage your AVD environments.

Because 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 let’s see the command in action. I’m searching in an AVD hostpool for all the session hosts with their image versions.

$hostpoolInfo = @{
    HostpoolName      = "avd-hostpool"
    ResourceGroupName = "rg-avd-01"
}
Get-AvdImageVersionStatus @hostpoolInfo -Verbose | Select-Object 

The result of the command is shown in the picture below. If there are different versions active in one hostpool, you see differences between the latestVersion object and the currentImageVersion object. The latest version is picked from the Azure Compute Gallery from where the sessionhost got his image. The currentImageVersion is the version that has the sessionhost now.

When using the -Latest flag in the command, the command searches at the isLatestVersion object.

Drain mode

To clean up the mess it is recommended to get 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 of 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 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 session hosts are removed.

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

11 Comments

  • Thank for this. The was working perfectly, however, over the last couple of months the output is now significantly different to what it was. Most importantly I no longer get any info related to the actual image version. Any ideas?

    • Hi AZ, thank you for reading my article.
      Do you have an example of what you are missing?

      Sander

  • Hi Sander, thank you for your reply- no image version details are presented when using Get-AvdImageVersionStatus. I get the below warning with some details about the pool, host, etc.

    WARNING: Someting went wrong when crawling for info, {“error”:{“code”:”MissingSubscription”,”message”:”The request did not have a subscription or a valid tenant level resource provider.”}}

    Interestingly the following WVD command works fine:
    Get-WvdImageVersionStatus -HostpoolName wvd-hostpool-001 -ResourceGroupName rg-wvd-001

    May be an issue with the module. Anyhow if is all working fine for you then it is probably an issue on my end 🙂

    Thanks again.

  • Hi,
    I’ve the same problem when using “Get-AvdImageVersionStatus”

    WARNING: Someting went wrong when crawling for info at url https://management.azure.com/versions?api-version=2022-01-03, {“error”:{“code”:”MissingSubscription”,”message”:”The request did not have a subscription or a valid tenant level resource provider.”}}

  • Hi Sander,
    thanks for the quick response; , I’m not so experienced with PowerShell module installation
    If I enter the following command

    Verzeichnis: C:\temp\AzAvd_v3.0.1-beta.1\Az.Avd

    Mode LastWriteTime Length Name
    —- ————- —— —-
    d—– 14.04.2022 16:07 Aliasses
    d—– 14.04.2022 16:07 en-US
    d—– 14.04.2022 16:07 Private
    d—– 14.04.2022 16:07 Public
    -a—- 14.04.2022 16:07 6334 Az.Avd.psd1
    -a—- 14.04.2022 16:07 938 Az.Avd.psm1

    PS C:\temp\AzAvd_v3.0.1-beta.1\Az.Avd> Install-Module .\Az.Avd.psm1 -Force

    I get the following error message

    “PackageManagement\Install-Package : No match was found for the specified search criteria and module name ‘.\Az.Avd.psm1’. Try Get-PSRepository
    to see all available registered module repositories.
    In C:\Program Files\WindowsPowerShell\Modules\PowerShellGet\2.2.5\PSModule.psm1:9711 Zeichen:34
    + … talledPackages = PackageManagement\Install-Package @PSBoundParameters
    + ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo : ObjectNotFound: (Microsoft.Power….InstallPackage:InstallPackage) [Install-Package], Exception
    + FullyQualifiedErrorId : NoMatchFoundForCriteria,Microsoft.PowerShell.PackageManagement.Cmdlets.InstallPackage”

    Do you have any idea what Im doing wrong ?

    Regards

    Jens

    • Hi Jens, sorry I provided you a wrong command. Use Import-Module .\Az.Avd.psm1

  • Hi Sander,
    thanks for the answer, no I was able to import the beta module 3.0.1-beta1, the warning is gone but the output is still empty

    if I use the following command which you mentioned in this blog article

    “Get-AvdImageVersionStatus -HostpoolName adspool3 -ResourceGroupName rg-avd-005-dev | Select vmLatestVersion, hostpoolName, sessionhostName, currentImageVersion, lastVersion”

    gives

    vmLatestVersion :
    hostpoolName :
    sessionhostName :
    currentImageVersion :
    lastVersion :

    vmLatestVersion :
    hostpoolName :
    sessionhostName :
    currentImageVersion :
    lastVersion :

    vmLatestVersion :
    hostpoolName :
    sessionhostName :
    currentImageVersion :
    lastVersion :

    It seems to me that there are some changes in the output of this command, all the infos are now in the “imageInfo” property ?

    • Hi Jens, thank you for testing it out. Could you please run the command below (without the select part)

      Get-AvdImageVersionStatus -HostpoolName adspool3 -ResourceGroupName rg-avd-005-dev
      The output should be stored in the imageInfo object.

      While writing, I realized the imageInfo is too complex. So, I decided to rewrite this command to get a more clear output. I updated the module which is now available in the new beta release.
      https://github.com/srozemuller/AzAvd/releases/tag/v3.0.1-beta.2

      Could you please test again with the command below.
      Get-AvdImageVersionStatus -HostpoolName adspool3 -ResourceGroupName rg-avd-005-dev

      Hope this will help

  • Hi Jens,

    did another try with the new 3.0.1-beta2 release and now it looks better
    When running this command

    Get-AvdImageVersionStatus -HostpoolName adspool3 -ResourceGroupName rg-avd-005-dev | fl sessionhostname,currentImageVersion,latestVersion,islatestVersion,hostpoolname

    i get the following output:

    ———————————————————-

    sessionHostName : adspool3/adspool3-1.az140.tk
    currentImageVersion : 2022.04.0301
    latestVersion : 2022.04.0301
    isLatestVersion : True
    hostpoolName : adspool3

    sessionHostName : adspool3/adspool3-2.az140.tk
    currentImageVersion : 2022.04.0301
    latestVersion : 2022.04.0301
    isLatestVersion : True
    hostpoolName : adspool3

    sessionHostName : adspool3/adspool3-3.az140.tk
    currentImageVersion : 2022.04.0301
    latestVersion : 2022.04.0301
    isLatestVersion : True
    hostpoolName : adspool3

    —————————————————

    Looks good now…

    Thanks

    • Hi Jens! That is great to hear. I will publish a new version in the gallery soon.
      Again, thank you so much for your feedback.

      Sander

Leave a Reply

Your email address will not be published.