Find Azure Virtual Desktop User Session

Here’s a quick script that can be used to find a user session which you can integrate with other scripts to perform actions.

# Log in to Azure (Skip this step if using Azure Cloud Shell or already logged in)
az login

# Define subscription and tenant IDs
$subscriptionId = "1234"
$tenantId = "56789"

# Get an Azure AD token for the Azure Management API, specifying the tenant
$azureRmToken = az account get-access-token --resource https://management.azure.com --tenant $tenantId --query accessToken -o tsv

# Define the REST API URL for listing all host pools
$restApiUrl = "https://management.azure.com/subscriptions/$subscriptionId/providers/Microsoft.DesktopVirtualization/hostPools?api-version=2021-01-14-preview"

# Invoke the REST API to get all host pools
$hostPools = Invoke-RestMethod -Uri $restApiUrl -Headers @{Authorization = "Bearer $azureRmToken" } -Method Get

# Specify the User UPN you are looking for
$userUpn = "kyle@domain.com"

# Iterate through each host pool and check for user sessions
foreach ($hostPool in $hostPools.value) {
    $hostPoolId = $hostPool.id
    $userSessionsUrl = "https://management.azure.com$hostPoolId/userSessions?api-version=2021-01-14-preview&`$filter=userPrincipalName eq '$userUpn'"

    # Invoke the REST API with the Bearer token for authorization to get user sessions
    $userSessionsResponse = Invoke-RestMethod -Uri $userSessionsUrl -Headers @{Authorization = "Bearer $azureRmToken" } -Method Get

    if ($userSessionsResponse.value.Count -gt 0) {
        # Output the host pool and user sessions response if sessions exist
        Write-Host "Host Pool: $($hostPool.name)"
        $userSessionsResponse.value | ConvertTo-Json | Write-Output
    }
}

Find a User’s Azure Virtual Desktop Assigned Personal Desktops

If you want to find the Personal Desktop that a user is assigned to you can modify the subscription ID and assigned user. Sander Rozemuller provided the initial idea and details for getting an assigned user using Azure Resource Graph Find specific Azure Virutal Desktop assigned user (rozemuller.com) and I used that information to create that script below that can be used for automation.

# Requires the Az.Accounts and Az.ResourceGraph modules
# Install them using Install-Module -Name Az.Accounts, Az.ResourceGraph

# Connect to Azure
Connect-AzAccount

# Define the subscription IDs you want to search across
$subscriptions = @('123-456-789-1011-12')

# Define the query for Azure Resource Graph
$query = @"
desktopvirtualizationresources 
| where type == "microsoft.desktopvirtualization/hostpools/sessionhosts" 
    and properties.assignedUser == "Kyle.Wise@domain.com"
"@

# Execute the query
$results = Search-AzGraph -Query $query -Subscription $subscriptions

# Output the results
$results

This will clean up the results and display only Name, Resource Group, UPN and Host Pool.

# Requires the Az.Accounts and Az.ResourceGraph modules
# Install them using Install-Module -Name Az.Accounts, Az.ResourceGraph

# Connect to Azure
#Connect-AzAccount

# Define the subscription IDs you want to search across
$subscriptions = @('1234-56789-00000000000')

# Define the query for Azure Resource Graph
$query = @"
desktopvirtualizationresources 
| where type == 'microsoft.desktopvirtualization/hostpools/sessionhosts' 
    and properties.assignedUser == 'Kyle.Wise@domain.com'
"@

# Execute the query
$results = Search-AzGraph -Query $query -Subscription $subscriptions

# Select the relevant properties and create a table
$table = $results | Select-Object -Property @{Name='Name'; Expression={
# Extract the session host name after the slash
                            ($_.name -split '/', 2)[-1]
                            }},
                            @{Name='ResourceGroupName'; Expression={$_.resourceGroup}},
                            @{Name='UserPrincipalName'; Expression={$_.properties.assignedUser}},
                            @{Name='PoolName'; Expression={
                            # Extract the pool name before the slash
                            ($_.name -split '/', 2)[0]
                            }}

# Output the results in a table format
$table | Format-Table -AutoSize
Share or Save this:
Share