Invoke-xRDS_SessionHostMaintenance.ps1
function Invoke-xRDS_SessionHostMaintenance { <# .DESCRIPTION Sets RDS Session Hosts Maintenance mode. .PARAMETER Broker -ConnectionBroker - FQDN of RDS ConnectionBroker. .PARAMETER Maintenance -Maintenance - Enable (no new logons), Disable (Allow new logons), Reboot (no new logons until reboot). .PARAMETER Credential -Credential [Optional] - Query RDS Connection Broker resources under provided credentials, the same credentials will be used to set maintenance mode too. .EXAMPLE # Sets RDS Session host for selected servers not allow new connections until the next reboot of the hosts: Invoke-xRDS_SessionHostMaintenance -ConnectionBroker ardscbl01.adatum.labnet -Maintenance Reboot #> [CmdletBinding()] Param( [Parameter(Mandatory=$true)][string]$ConnectionBroker, [Parameter(Mandatory=$false)] [ValidateSet('Enable','Disable','Reboot')] [string]$Maintenance = 'Disable', [PSCredential]$Credential ) $Collection = Get-xRDS_CollectionsList -ConnectionBroker $ConnectionBroker -Credential $Credential #Attribute values for maintenance mode switch ($Maintenance) { 'Enable' {$attributeSyntax = 2} 'Disable' {$attributeSyntax = 0} 'Reboot' {$attributeSyntax = 1} Default {} } Try { $ActiveManagementServer = (Invoke-WmiMethod -ComputerName $ConnectionBroker -Class "Win32_RDMSEnvironment" -Namespace "root\CIMV2\rdms" -Name "GetActiveServer").ServerName $SelectedServers = Get-WmiObject -ComputerName $ActiveManagementServer -Credential $Credential -Query "SELECT ServerName,LoadIndicator,NumberOfSessions,NumPendRedir,ServerWeight,SingleSessionMode FROM Win32_SessionDirectoryServer" | select ` @{Label="Host";Expression={$_.ServerName}}, @{Label="Collections";Expression={$Collection[$_.ServerName]}},@{Label="Sessions";Expression={$_.NumberOfSessions}}, ` @{Label="DrainMode";Expression={Convert-xRDS_DrainMode((Get-WmiObject -Credential $Credential -Class "Win32_TerminalServiceSetting" -Namespace "root\CIMV2\terminalservices" -ComputerName $_.ServerName).SessionBrokerDrainMode)}},` @{Label="LoadIndicator";Expression={$_.LoadIndicator}},@{Label="ServerWeight";Expression={$_.ServerWeight}},` @{Label="NumPendRedir";Expression={$_.NumPendRedir}},@{Label="SingleSessionMode";Expression={$_.SingleSessionMode}} | sort Host | Out-GridView -PassThru -Title "Host List (Select Servers)" If ($SelectedServers.Host.Count -le 0) {Write-Host "No hosts have been selected." -ForegroundColor Cyan } else { Write-Host "Maintenance has been set: $Maintenance for the following hosts" -ForegroundColor Cyan ; $SelectedServers.Host | foreach {Write-Host "$_" -ForegroundColor Cyan ; Set-WmiInstance -Credential $Credential -Class "Win32_TerminalServiceSetting" -Namespace "root\CIMV2\terminalservices" -Arguments @{SessionBrokerDrainMode=$attributeSyntax} -ComputerName $_ | Out-Null }} } Catch {Write-host $_.Exception.message -ForegroundColor Red} } |