Public/Get-RDSession.ps1

function Get-RDSession {
    <#
    .SYNOPSIS
        Lists Remote Desktop sessions on a given server.
    .DESCRIPTION
        The Get-RDSession cmdlet retrieves a list of Remote Desktop sessions from a local or remote computer.
    .PARAMETER ComputerName
        Specifies the name of the computer to query. Default value is the local computer.
    .PARAMETER UserName
 
    .EXAMPLE
    .EXAMPLE
    .NOTES
    .LINK
        http://psservermanagement.readthedocs.io/en/latest/functions/Get-RDSession
    .LINK
        http://code.msdn.microsoft.com/PSTerminalServices
    .LINK
        http://code.google.com/p/cassia/
    #>


    [CmdletBinding()]
    [OutputType('Cassia.Impl.TerminalServicesSession')]

    param(
        [Parameter(
            ValueFromPipelineByPropertyName = $true
        )]
        [Alias('DSNHostName', 'Name', 'Computer')]
        [string]$ComputerName = 'localhost',

        [Parameter()]
        [ValidateSet('Active', 'Connected', 'ConnectQuery', 'Disconnected', 'Down', 'Idle', 'Initializing', 'Listening', 'Reset', 'Shadowing')]
        [Alias('ConnectionState')]
        [string]$State = '*',

        [Parameter()]
        [string]$ClientName = '*',

        [Parameter()]
        [string]$UserName = '*'
    )

    begin {
        try {
            Write-Verbose -Message 'Creating instance of the Cassia TSManager.'
            $TSManager = New-Object -TypeName Cassia.TerminalServicesManager
        }
        catch {
            throw
        }
    }

    process {
        Write-Verbose -Message ($LocalizedData.RemoteConnect -f $ComputerName)
        if (!(Test-Connection -ComputerName $ComputerName -Count 1 -Quiet)) {
            Write-Warning -Message ($LocalizedData.ComputerOffline -f $ComputerName)
            return
        }
        try {
            $TSRemoteServer = $TSManager.GetRemoteServer($ComputerName)
            $TSRemoteServer.Open()
            if (!($TSRemoteServer.IsOpen)) {
                throw ($LocalizedData.RemoteConnectError -f $ComputerName)
            }

            $Session = $TSRemoteServer.GetSessions()
            if ($Session) {
                $Session | Where-Object { $_.ConnectionState -like $State -and $_.UserName -like $UserName -and $_.ClientName -like $ClientName } |
                    Add-Member -MemberType AliasProperty -Name IPAddress -Value ClientIPAddress -PassThru |
                    Add-Member -MemberType AliasProperty State -Value ConnectionState -PassThru
            }
        }
        catch {
            throw
        }
        finally {
            $TSRemoteServer.Close()
            $TSRemoteServer.Dispose()
        }
    }

    end {}
}