Monitoring Azure Windows Virtual Desktop using PowerShell

Azure Windows Virtual Desktop service provided by Microsoft offers application virtualization services. Note that most of the management of WVD needs to be done using PowerShell modules. Microsoft recently released WVD 2.0, and it is important to note that the architecture of WVD 2.0 has completely been changed from its previous version. The WVD 2.0 follows the Azure Resource Manager model in which you are required to create a WVD workspace, a resource group, and then deploy WVD components such as Host Pool, session hosts, application groups, etc. under the resource groups. While Microsoft Azure Portal provides a limited set of functionalities through which you can manage some aspects of the Windows Virtual Desktop environment, but you are still required to use PowerShell modules to perform certain tasks. For example, you cannot remove or disconnect a session from the Azure Portal. However, when it comes to monitoring Windows Virtual Desktop, you are required to pay attention to two important tasks: monitoring the overall health of session hosts and active users. This article explains which PowerShell modules to install in your environment and some PowerShell commands to help you monitor your Windows Virtual Desktop environment, particularly session hosts status. Please note that this article explains modules used for the Windows Virtual Desktop Spring update.

Az.DesktopVirtualization PowerShell Module

For the earlier version of WVD, you had to use RDS PowerShell modules to manage the Azure Windows Virtual Desktop 1.0 environment. For WVD 2.0, you are required to use Az.DesktopVirtualization PowerShell module. To install Az.DesktopVirtualization PowerShell modules, you need to do it by executing this command:

Install-Module -Name Az.DesktopVirtualization

After executing the command, you should have Az.DesktopVirtualization PowerShell modules installed on your computer.

Connecting to Windows Virtual Desktop 2.0 environment

Once you have installed the PowerShell modules, you will use the Connect-AzConnect PowerShell module to connect to the WVD 2.0 environment. You can either connect using an Azure username and password or use a service principal.

Using PowerShell commands for WVD 2.0

There are several PowerShell commands available as part of Az.DesktopVirtualization to manage WVD 2.0. For example, you can use Get-AzWVDHostPool PowerShell cmdlet to get a list of WVD host pool hosted in the WVD environment. You can use Get-AzWVDSessionHost to get a list of session hosts attached to each host pool, and so on.

For example, to get a list of WVD host pools from the WVD 2.0 environment, execute Get-AzWVDHostPool. The Get-AzWVDHostPool command lists the host pools but note that the command does not return all the properties by default. To make sure the command returns all the properties, execute the Get-AzWVDHostPool | Select-Object *” command as shown in the screenshot below:

Azure Windows Virtual Desktop

As you can see in the above screenshot, it lists the host pools that I have created in my environment. The output shows the configuration for each host pool, such as LoadBalancerType, MaxSessionLimit, location of the pool, and so on.

Important: Most of the PowerShell cmdlets for Az.DesktopVirtualization module requires you to provide necessary parameters before you can see the desired output. While retrieving the list of host pools, you don’t need to add any parameters as it shows in the screenshot above, but for some PowerShell commands such as when retrieving the list of session hosts assigned to a host pool, you are required to provide “resource group name” and “host pool name” as indicated in this PowerShell command:

$FinalResGroup = “ThisResourceGroup”
$PoolName = $PoolName
Get-AzWvdSessionHost -ResourceGroupName $FinalResGroup -HostPoolName $PoolName

As you can see in the commands above, you need to provide resource group name and a pool name to get you a list of session hosts, but as stated earlier the command does not return all properties unless you specify “Select-Object *” parameter, as shown in the command and screenshot below:

$FinalResGroup = “ThisResourceGroup”
$PoolName = $PoolName
Get-AzWvdSessionHost -ResourceGroupName $FinalResGroup -HostPoolName $PoolName | Select-Object *

Getting a list of session hosts for each host pool

Let’s assume you would like to get a list of session hosts for each pool you have hosted in your environment. To do this, use Get-AzWVDHostPool to collect all host pools and PowerShell ForEach loop to process all host pools as shown in the PowerShell script below:

$ResultFile = "C:\Temp\AllHosts.CSV"
Remove-item $ResultFile -ErrorAction SilentlyContinue
$STR = "Pool Name, Host Name"
Add-Content $ResultFile $STR
$AllPools = Get-AzWVDHostPool | Select-Object *
foreach ($Item in $AllPools)
{
$PoolName = $Item.Name
$ThisLine = $Item.ApplicationGroupReference
$R = $ThisLine
$A, $B, $C, $D, $E, $F = $R.Split("/")
$FinalResGroup = $E
$AllHosts = Get-AzWvdSessionHost -ResourceGroupName $FinalResGroup -HostPoolName $PoolName
foreach ($Item in $AllHosts)
{
$HostName = $Item.Name
$STR = $PoolName + "," + $HostName
Add-Content $ResultFile $STR
}
}

Note that in the script above, I am retrieving the resource group name from ApplicationGroupReference property that is assigned to each host pool. Since the Get-AzWVDSessionHost cmdlet requires you to specify resource group Name, you are required to fetch the resource group name from the ApplicationGroupReference property. I am using the split () PowerShell method to retrieve the resource group Name. When you execute the above PowerShell script, it collects all host pools in the WVD environment, store outputs to $AllPools variable and then collects session hostname in each pool by executing “Get-AzWVDSessionHost” PowerShell command. Finally, the output is stored in the C:\Temp\AllHosts.CSV file.

The above PowerShell script is retrieved from the Wintellisys WVD Manager Application, which is designed to manage and monitor all aspects of a WVD 2.0 environment.

Monitoring session hosts status

Note that when you have thousands of session hosts running in your Azure Windows Virtual Desktop environment, you are not sure whether all session hosts are enabled for connections and whether any of the session hosts are idle during the day. When it comes to monitoring session hosts, you would like to know:

  • How many session hosts are receiving heartbeat?
  • How many session hosts are enabled for servicing client requests? Or How many session hosts are in drain mode?
  • How many session hosts are turned off, and how many session hosts are idle during the day?

While you can use Get-AzWVDSessionHost PowerShell commands to get a list of session hosts and check “AllowNewSession” property and “Status” property to check availability status of session hosts, but to check the VM status of session host, such as whether the VM is turned on or off, you will need to use Get-AzVM. The Get-AzVM PowerShell cmdlet can help you get the VM status of each session host. For example, below PowerShell Script can help you collect session hosts VM Status for each host pool:

$ResultFile = "C:\Temp\AllHosts.CSV"
Remove-item $ResultFile -ErrorAction SilentlyContinue
$STR = "Pool Name, Host Name, VM Status"
Add-Content $ResultFile $STR
$AllPools = Get-AzWVDHostPool | Select-Object *
foreach ($Item in $AllPools)
{
$PoolName = $Item.Name
$ThisLine = $Item.ApplicationGroupReference
$R = $ThisLine
$A, $B, $C, $D, $E, $F = $R.Split("/")
$FinalResGroup = $E
$AllHosts = Get-AzWvdSessionHost -ResourceGroupName $FinalResGroup -HostPoolName $PoolName
foreach ($Item in $AllHosts)
{
$HostName = $Item.Name
$R, $Z = $HostName.Split("/")
$C, $D = $Z.Split(".")
$FinalVMName = $C
$VMStatus = Get-AzVM -ResourceGroupName $FinalResGroup -name $FinalVMName -Status
$FinalVMStatus = $VMStatus.Statuses[1].DisplayStatus
$STR = $PoolName + "," + $FinalVMName+","+$FinalVMStatus
Add-Content $ResultFile $STR
}
}

When the script is finished executing, a CSV file is generated under C:\Temp\AllHosts.CSV, which contains the host pool name, session hostname in the pool, and VM status of session host. As you can see in the script, we are using the Get-AzVM PowerShell cmdlet to get the status of the session host VM.

While the script can give you the VM status of each session host, that is not the only information you are looking for. For example, you may also need to collect other attributes such as the number of active users on each host and whether the session host is receiving heartbeat or not. It also becomes cumbersome to collect all information by using custom PowerShell scripts, and managing those PowerShell scripts is again a tedious task. The above script, which is fetched from a WVD Pack called “session hosts health” in Wintellisys WVD Manager, can be expanded to show a complete diagram of session hosts availability and their other statuses we spoke about in the earlier section of this article. For example, when you execute “session hosts health” WVD Pack in Wintellisys WVD Manager, you will be able to see a view of current session hosts health as shown in the screenshot below:

WVD

As you can see in the screenshot, there are 379 session hosts deployed in the WVD environment, 303 session hosts are on, 61 session hosts are in drain mode, and so on. By just looking at the dashboard, you can tell the current status of session hosts in your environment.

Featured image: Shutterstock

About The Author

Leave a Comment

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

This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.

Scroll to Top