Simple way for retrieving all AVD sessions in an Azure Subscription

Simple way for retrieving all AVD sessions in an Azure Subscription

Hello again everyone :)

When working with Azure Virtual Desktop sometimes you require to view all active/disconnected sessions to be able to make a decision on whatever action.

The AVD portal is very nice and intuitive, but sometimes you need to act fast and collect information that is not that easily displayed in the portal itself.

It is so nice to have PowerShell modules available for such tasks. And for AVD we have the Az.DesktopVirtualization module.

Tip: To view all cmdlets related to that specific module you can run:

Get-Command *-AzWv

or

Get-Command -Module Az.DesktopVirtualization*d        

Let us continue...

This time I want to share with you a very simple and fast script that retrieves all sessions that are registered to any of the Host Pools in an Azure Subscription.

The only thing that is required is to connect to Azure and set the context to an active subscription, like this:

Connect-AzAccount
Set-AzContext -SubscriptionId <your subscription id>        

Now you are ready to run the script:

$AllPools = Get-AzWvdHostPool
$AllSessions = @()
foreach ($pool in $AllPools){
? ? $name = $pool.Name
? ? $id = $pool.Id.ToLower()
? ? $rg = $id -replace '(:?\/subscriptions\/.*\/resourcegroups\/)(?<pt1>.*)(:?\/providers\/.*)', '${pt1}'
? ? $sessions = Get-AzWvdUserSession -HostPoolName $name -ResourceGroupName $rg
? ? foreach ($session in $sessions){
? ? ? ? $fullId = $session.Id.ToLower()
? ? ? ? $shFullName = $fullId -replace '(:?\/subscriptions\/.*\/sessionhosts\/)(?<pt1>.*)(:?\/usersessions\/.*)', '${pt1}'
? ? ? ? $shShortName = $shFullName -replace '\..*' ,''
? ? ? ? $shid = $fullId -replace '(:?\/subscriptions\/.*\/usersessions\/)(?<pt1>)', '${pt1}'
? ? ? ? $entry = New-Object psobject
? ? ? ? $entry | Add-Member -MemberType NoteProperty -Name UserPrincipalName -Value $($session.UserPrincipalName)
? ? ? ? $entry | Add-Member -MemberType NoteProperty -Name ResourceGroup -Value $rg
? ? ? ? $entry | Add-Member -MemberType NoteProperty -Name HostPool -Value $name
? ? ? ? $entry | Add-Member -MemberType NoteProperty -Name VM -Value $shShortName
? ? ? ? $entry | Add-Member -MemberType NoteProperty -Name SessionHost -Value $shFullName
? ? ? ? $entry | Add-Member -MemberType NoteProperty -Name Id -Value $shid
? ? ? ? $entry | Add-Member -MemberType NoteProperty -Name Type -Value $($session.ApplicationType)
? ? ? ? $entry | Add-Member -MemberType NoteProperty -Name State -Value $($session.SessionState)
? ? ? ? $AllSessions += $entry
? ? }
}        

The output is stored inside this variable: $AllSession

No alt text provided for this image

What is nice here is that I haven't specified any 'Resource Group' or 'Host Pool' names to be able to retrieve that information.

This now can help you to disconnect/remove unwanted sessions, and more.

Enjoy :)

Oliver Hüppe

Senior System Engineer

7 个月

Nice script. But when I try to remove all sessions from the Array "Allsessions" it simply doesn't remove any session. There are no locks in place etc. Does anyone has an advice for me to troubleshoot?

回复

要查看或添加评论,请登录

Pavel Rosenberg的更多文章

社区洞察

其他会员也浏览了