Invoke-xRDS_ShadowUser.ps1

function Invoke-xRDS_ShadowUser {

<#
    .DESCRIPTION
    Shadows selected RDS user from a sessions list.
  
    .PARAMETER Broker
    -ConnectionBroker - FQDN of RDS ConnectionBroker.
 
    .PARAMETER Control
    -Control - Starts RDS user shadowing session with remote control access.
 
    .PARAMETER Credential
    -Credential [Optional] - Query RDS Connection Broker resources under provided credentials.
 
    .EXAMPLE
    # Starts RDS user's shadowing session with remote control access:
    Invoke-xRDS_ShadowUser -ConnectionBroker ardscbl01.adatum.labnet -Control
#>



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

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

    Try {    

    If ($SelectedUsers.UserName.Count -ne 1) {Write-Host "No users have been selected or you have selected multiple users which is not allowed." -ForegroundColor Cyan }
    else { Write-Host "Shadowing user (with remote control:$Control): $($SelectedUsers.UserName) on hosts: $($SelectedUsers.Host)" -ForegroundColor Cyan ; 
        #Shadow with control
        if($Control) {Start-Process -FilePath "$env:windir\system32\mstsc.exe" -ArgumentList "/control /shadow:$($SelectedUsers.SID) /v:$($SelectedUsers.Host)"}
        #Shadow view only
        else {Start-Process -FilePath "$env:windir\system32\mstsc.exe" -ArgumentList "/shadow:$($SelectedUsers.SID) /v:$($SelectedUsers.Host)"} }

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