Send AVD Host Pool Session Load Info to Teams

Used for all Multi-Session Host Pools then the script gets Active Sessions, Disconnected Sessions, Session Host Count, Maximum Session Limit, Total Sessions and Session Usage Percentage which is based on the Max session limit defined on the host pool. Additionally, it sends this info to a Microsoft Teams webhook so you can be alerted. You can also expand on this script to automatically deploy (Auto scale) session hosts when needed.

#Webhook URL to send the teams message on
                $webhookUrl = "https://YourTeamsWebhookHere"

                # Notification message to send into the teams
                $messageBody = @{
                    "themeColor" = "FF3105"
                    "title" = "Multi-Session- Check for Host Pools with Session Count Over 90%"
                    "summary" = "Multi-Session- Check for Host Pools with Session Count Over 90%"
                    "sections" = @()
                }

                # Get all resource groups in the subscription
                $resourceGroups = Get-AzResourceGroup

                # Loop through each resource group
                foreach ($resourceGroup in $resourceGroups) {

                    # Get all host pools in the resource group
                    $hostPools = Get-AzWvdHostPool -ResourceGroupName $resourceGroup.ResourceGroupName

                    # Loop through each host pool
                    foreach ($hostPool in $hostPools) {

                        # If host pool is Pooled, calculate session percentage
                        if ($hostPool.HostPoolType -eq 'Pooled') {

                            $activeSessionCount = (Get-AzWvdUserSession -ResourceGroupName $resourceGroup.ResourceGroupName -HostPoolName $hostPool.Name | where {$_.SessionState -eq "Active"}).count
                            $disconnectedSessionCount = (Get-AzWvdUserSession -ResourceGroupName $resourceGroup.ResourceGroupName -HostPoolName $hostPool.Name | where {$_.SessionState -eq "Disconnected"}).count
                            $sessionHostCount = (Get-AzWvdSessionHost -ResourceGroupName $resourceGroup.ResourceGroupName -HostPoolName $hostPool.Name).count

                            $maxSessionLimit = $hostPool.MaxSessionLimit * $sessionHostCount
                            $totalSessions = $activeSessionCount + $disconnectedSessionCount
                            $sessionPercentage = ($totalSessions / $maxSessionLimit) * 100

                            # Organized output
                            Write-Output "--------------------------------------------------"
                            Write-Output "Resource Group: $($resourceGroup.ResourceGroupName)"
                            Write-Output "Pooled Host Pool: $($hostPool.Name)"
                            Write-Output "--------------------------------------------------"
                            Write-Output "Active Sessions: $activeSessionCount"
                            Write-Output "Disconnected Sessions: $disconnectedSessionCount"
                            Write-Output "Session Host Count: $sessionHostCount"
                            Write-Output "Maximum Session Limit: $maxSessionLimit"
                            Write-Output "Total Sessions: $totalSessions"
                            Write-Output "Session Usage Percentage: ${sessionPercentage}%"
                            Write-Output "--------------------------------------------------"
                              
                            $hostPoolArray = @()
                              $hostPoolArray += $hostPool
                              $messageBody['sections'] += @{
                                "activityTitle" = "Resource Group: $($resourceGroup.ResourceGroupName)"
                                "activitySubtitle" = "Pooled Host Pool: $($hostPool.Name)"
                                "facts" = @(
                                    @{
                                        "name" = "Active Sessions:"
                                        "value" = "$activeSessionCount"
                                    },
                                    @{
                                        "name" = "Disconnected Sessions:"
                                        "value" = "$disconnectedSessionCount"
                                    },
                                    @{
                                        "name" = "Session Host Count:"
                                        "value" = "$sessionHostCount"
                                    },
                                    @{
                                        "name" = "Maximum Session Limit:"
                                        "value" = "$maxSessionLimit"
                                    },
                                    @{
                                        "name" = "Total Sessions:"
                                        "value" = "$totalSessions"
                                    },
                                    @{
                                        "name" = "Session Usage Percentage:"
                                        "value" = "${sessionPercentage}%"
                                    }
                                  )
                              }
                            }
                        }
                    }
                }
                if($messageBody['sections'].Count -gt 0){
                  #Converting Message body into JSON to send it as a payload into teams.
                  $jsonPayload = $messageBody | ConvertTo-Json -Depth 10
                  Invoke-RestMethod -Uri $webhookUrl -Method Post -Body $jsonPayload -ContentType 'application/json'
                }
Share or Save this:
Share