Invoke-xRDS_LogoffUser.ps1

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}
}