Get-RDPUserReport.ps1


Description

Purpose

Retrieves RDP session details from specified computers.

Detailed Description

Queries the specified servers for RDP session details and outputs them as objects for further manipulation.

Back to Top

Usage

Example 1

Get-RDPUserReport -ComputerName "DANTOOINE"

Example 2

Get-RDPUserReport -ComputerName "DANTOOINE" | Sort-Object IdleTime | Format-Table -AutoSize

Back to Top

Notes

No additional notes.

Back to Top


Script

# Function: Get-RDPUserReport
Function Get-RDPUserReport {
    <#
    .SYNOPSIS
    Retrieves RDP session details from specified computers.

    .DESCRIPTION
    Queries the specified servers for RDP session details and outputs them as objects for further manipulation.

    .PARAMETER ComputerName
    Name or IP address of the computer(s) to query.

    .EXAMPLE
    Get-RDPUserReport -ComputerName "DANTOOINE"

    .EXAMPLE
    Get-RDPUserReport -ComputerName "DANTOOINE" | Sort-Object IdleTime | Format-Table -AutoSize
    #>

    [CmdletBinding()]
    param (
        [Parameter(Mandatory = $true, ValueFromPipeline = $true, ValueFromPipelineByPropertyName = $true)]
        [string[]]$ComputerName
    )

    # Initialize array to store session details
    $Sessions = @()

    # Query each specified computer
    foreach ($Computer in $ComputerName) {
        try {
            $ConnectionResult = Test-NetConnection -ComputerName $Computer -CommonTCPPort RDP -ErrorAction SilentlyContinue -WarningAction SilentlyContinue
            if ($ConnectionResult.TcpTestSucceeded) {
                Write-Verbose "Connected to $Computer"
                $DirtyOutput = (quser /server:$Computer) -replace '\s{2,}', ',' | ConvertFrom-Csv
                foreach ($session in $DirtyOutput) {
                    if (($session.sessionname -notlike "console") -and ($session.sessionname -notlike "rdp-tcp*")) {
                        $sessionData = [pscustomobject]@{
                            Username    = $session.USERNAME
                            SessionName = ""
                            ID          = $session.SESSIONNAME
                            State       = $session.ID
                            IdleTime    = $session.STATE
                            LogonTime   = $session."IDLE TIME"
                            ServerName  = $Computer
                        }
                    } else {
                        $sessionData = [pscustomobject]@{
                            Username    = $session.USERNAME
                            SessionName = $session.SESSIONNAME
                            ID          = $session.ID
                            State       = $session.STATE
                            IdleTime    = $session."IDLE TIME"
                            LogonTime   = $session."LOGON TIME"
                            ServerName  = $Computer
                        }
                    }
                    $Sessions += $sessionData
                }
            } else {
                Write-Verbose "Failed to connect to $Computer"
                $Sessions += [pscustomobject]@{
                    Username    = 'N/A'
                    SessionName = 'N/A'
                    ID          = 'N/A'
                    State       = 'Unavailable'
                    IdleTime    = 'N/A'
                    LogonTime   = 'N/A'
                    ServerName  = $Computer
                }
            }
        } catch {
            Write-Warning "Failed to query $($Computer): $_"
        }
    }

    # Return session details
    return $Sessions
}

Back to Top

Download

Please feel free to copy parts of the script or if you would like to download the entire script, simply click the download button. You can download the complete repository in a zip file by clicking the Download link in the menu bar on the left hand side of the page.


Report Issues

You can report an issue or contribute to this site on GitHub. Simply click the button below and add any relevant notes. I will attempt to respond to all issues as soon as possible.

Issue


Back to Top