Invoke-xRDS_LogoffUser.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
function Invoke-xRDS_LogoffUser {

<#
    .DESCRIPTION
    Logs off selected RDS users from a sessions list.
  
    .PARAMETER Broker
    -ConnectionBroker - FQDN of RDS ConnectionBroker.
 
    .PARAMETER Force
    -Force - Force logoff of users session. Use it only if logoff does not work. It can corrupt user's profile.
 
    .PARAMETER Credential
    -Credential [Optional] - Query RDS Connection Broker resources under provided credentials.
 
    .EXAMPLE
    # Invokes RDS user's session logoff:
    Invoke-xRDS_LogoffUser -ConnectionBroker ardscbl01.adatum.labnet
#>



    [CmdletBinding()]
    Param(
        [Parameter(Mandatory=$true)][string]$ConnectionBroker,    
        [switch]$Force,
        [PSCredential]$Credential
    )      

    $SelectedUsers = Get-xRDS_UsersList -ConnectionBroker $ConnectionBroker -Credential $Credential

    If ($Force) {$cmd = "rwinsta"} else {$cmd = "logoff"}

    Try {    

    If ($SelectedUsers.UserName.Count -le 0) {Write-Host "No users have been selected." -ForegroundColor Cyan }
    else { Write-Host "Following sessions have been initiated for disconnect (Force:$Force):" -ForegroundColor Cyan ; $SelectedUsers | foreach {Write-Host "Username: $($_.UserName) SID: $($_.SID) Collection: $($_.Collection) HOST: $($_.Host) " -ForegroundColor Cyan ; start-process $cmd "$($_.SID) /SERVER:$($_.Host)" -WindowStyle Hidden}}


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