Get-xRDS_SessionHostList.ps1

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
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 }   

}