Get-xRDS_UsersList.ps1

function Get-xRDS_UsersList ([Parameter(Mandatory=$true)][string]$ConnectionBroker,[PSCredential]$Credential) {

    $Collection = Get-xRDS_CollectionsList -ConnectionBroker $ConnectionBroker -Credential $Credential
    
    Try {

        $ActiveManagementServer =  (Invoke-WmiMethod -ComputerName $ConnectionBroker -Class "Win32_RDMSEnvironment" -Namespace "root\CIMV2\rdms" -Name "GetActiveServer").ServerName

        $OutSessionsCollection = Get-WmiObject -Credential $Credential -ComputerName $ActiveManagementServer -Namespace "root\CIMv2" -Query "SELECT UserName,IdleTime,CreateTime,DisconnectTime,HostServer,DomainName,SessionState,UnifiedSessionID,TSProtocol,ColorDepth,ResolutionHeight,ResolutionWidth,RemoteFxEnabled,SessionEncrypted FROM Win32_SessionDirectorySessionEx" `
        | Select UserName,`
        @{Label="SID";Expression={$_.UnifiedSessionID}},`
        @{Label="Host";Expression={$_.HostServer}},`
        @{Label="Activity";Expression={Convert-xRDS_SessionState($_.SessionState)}},`
        @{Label="Collection";Expression={$Collection[$_.HostServer]}},`
        @{Label="Creation Time";Expression={[datetime]::ParseExact(($_.CreateTime -replace "\..*",""),"yyyyMMddHHmmss", $null)}},`
        @{Label="Disconnect Time";Expression={[datetime]::ParseExact(($_.DisconnectTime -replace "\..*",""),"yyyyMMddHHmmss", $null)}},`
        @{Label="Domain";Expression={$_.DomainName}},`
        @{Label="Idle";Expression={"{0:N0}" -f ([timespan]::frommilliseconds($_.IdleTime)).TotalMinutes}}, `
        @{Label="TSProtocol";Expression={Convert-xRDS_TSProtocol($_.TSProtocol)}},`
        @{Label="ColorDepth";Expression={$_.ColorDepth}},`
        @{Label="ResolutionHeight";Expression={$_.ResolutionHeight}},`
        @{Label="ResolutionWidth";Expression={$_.ResolutionWidth}},`
        @{Label="RemoteFxEnabled";Expression={$_.RemoteFxEnabled}},`
        @{Label="SessionEncrypted";Expression={$_.SessionEncrypted}}`
        | Sort UserName `
        | Out-GridView -PassThru -Title "All Sessions"

    if($OutSessionsCollection.count -eq 0 ) {write-host "No user sessions have been detected on RDS management server $ActiveManagementServer" -ForegroundColor Cyan}
    }
    
    Catch {Write-host $_.Exception.message -ForegroundColor Red} 
    
    return $OutSessionsCollection
}