Get-xRDS_SessionHostList.ps1

function Get-xRDS_SessionHostList {

<#
    .DESCRIPTION
    Shows RDS Session Hosts Maintenance mode details.
  
    .PARAMETER Broker
    -ConnectionBroker - FQDN of RDS ConnectionBroker.
 
    .PARAMETER Credential
    -Credential [Optional] - Query RDS Connection Broker resources under provided credentials, the same credentials will be used to query RDS session hosts.
 
    .PARAMETER UI
    -UI [Optional] - Displays records in GridView for output selection.
 
    .PARAMETER WINRMPort
    -WINRMPort [Optional] - WINRM Port to test for host connectivity validation. Default port is 5985.
 
    .EXAMPLE
    # Invokes RDS user's session logoff:
    Get-xRDS_SessionHostList -ConnectionBroker ardscbl01.adatum.labnet
#>



    [CmdletBinding()]
    Param(
        [Parameter(Mandatory=$true)][string]$ConnectionBroker,
        [switch]$UI,
        [Int]$WinRMPort = "5985",    
        [PSCredential]$Credential
    )      

    $Collection = Get-xRDS_CollectionsList -ConnectionBroker $ConnectionBroker -Credential $Credential
    $ObjectsList=@()

    Try {  

    #List session host maintenance mode

             foreach ($key in $Collection.Keys) 
                {
                $TempObject = New-Object PSObject
                $connection = $null;

                #Add host details
                $TempObject | add-member Noteproperty Host $key

                #Add host DrainMode details
                $connection =  Invoke-xRDS_TestPort -hostname $key -port $WinRMPort
                
                if ($connection.open) {$TempObject | add-member Noteproperty Status "UP" ; $value = (Get-WmiObject -Credential $Credential -Class "Win32_TerminalServiceSetting" -Namespace "root\CIMV2\terminalservices" -ComputerName $key).SessionBrokerDrainMode ; $TempObject | add-member Noteproperty DrainMode $(Convert-xRDS_DrainMode($value))}
                else {$TempObject | add-member Noteproperty Status "DOWN" ; $value = "99" ; $TempObject | add-member Noteproperty DrainMode $(Convert-xRDS_DrainMode($value))}
                
                #Add host collection details
                $TempObject | add-member Noteproperty Collection $Collection[$key]               
                 
                $ObjectsList += $TempObject 
          
                } 

             #Output
             If($UI) {$ObjectsList | Sort "Host" | Out-GridView -PassThru -Title "RDS Session Hosts Drain Mode Status"}
             ELSE {$ObjectsList | Sort "Host"} 

    } Catch {Write-host $_.Exception.message }   

}