Public/Get-CitrixDeliveryGroupLoadStatus.ps1

<#
.SYNOPSIS
    Retrieves and visualizes load and session count information for a specified Citrix Delivery Group, supporting both SingleSession and MultiSession types.
 
.DESCRIPTION
    This function queries Citrix Delivery Groups for load and session information, visualizing these metrics for both SingleSession and MultiSession groups. It generates graphical representations for MultiSession groups and console output for all groups.
 
.PARAMETER AdminAddress
    The address of the Citrix administration server.
 
.PARAMETER DeliveryGroupName
    The name of the Delivery Group to query.
 
.EXAMPLE
    Get-CitrixDeliveryGroupLoadStatus -AdminAddress "ctx-admin.domain.com" -DeliveryGroupName "OfficeApps"
    Queries and visualizes load and session data for the 'OfficeApps' delivery group.
 
.NOTES
    Requires administrative rights on the Citrix environment for execution.
#>


Function Get-CitrixDeliveryGroupLoadStatus {
    [CmdletBinding()]
    Param(
        [Parameter(Mandatory = $true)]
        [string]$AdminAddress,

        [Parameter(Mandatory = $true)]
        [string]$DeliveryGroupName
    )

    Begin {

        $CitrixParams = @{
            AdminAddress   = $AdminAddress
            MaxRecordCount = ([Int32]::MaxValue)
            Filter         = "Name -eq '$DeliveryGroupName'"
            ErrorAction    = 'Stop'
        }
    }

    Process {
        Try {
            $DeliveryGroup = Get-BrokerDesktopGroup @CitrixParams

            if ($DeliveryGroup) {
                if ($DeliveryGroup.SessionSupport -eq 'MultiSession') {
                    # Handling MultiSession group visualization
                    $machines = Get-BrokerMachine -AdminAddress $AdminAddress -DesktopGroupName $DeliveryGroupName

                    foreach ($machine in $machines) {
                        Write-Output "Hosted Machine: $($machine.HostedMachineName) | Load Index: $($machine.LoadIndex) | Session Count: $($machine.SessionCount)"
                    }


                } elseif ($DeliveryGroup.SessionSupport -eq 'SingleSession') {
                    # Handling SingleSession group calculation
                    $loadPercentage = [math]::Round(($DeliveryGroup.Sessions * 100) / $DeliveryGroup.TotalDesktops)

                    $output = [PSCustomObject]@{
                        Name          = $DeliveryGroupName
                        LoadStatus    = "$loadPercentage%"
                        SessionCount  = $DeliveryGroup.Sessions
                        TotalDesktops = $DeliveryGroup.TotalDesktops
                    }

                    Write-Output $output
                }
            } else {
                Write-Output "Delivery Group '$DeliveryGroupName' not found or does not support sessions."
            }
        } Catch {
            Write-Error "Failed to retrieve load status: $_"
        }
    }

    End {
        Write-Verbose "Load status query completed for $DeliveryGroupName."
    }
}