Skip to content

Instantly share code, notes, and snippets.

@Xainey
Created January 25, 2018 15:16
Show Gist options
  • Save Xainey/e9b51d3c528d494a59ac5719ec6e3f21 to your computer and use it in GitHub Desktop.
Save Xainey/e9b51d3c528d494a59ac5719ec6e3f21 to your computer and use it in GitHub Desktop.

Revisions

  1. Xainey created this gist Jan 25, 2018.
    31 changes: 31 additions & 0 deletions Get-RDPUsers.ps1
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,31 @@
    function Get-RDPUsers ($computers) {
    process{
    foreach($computer in $computers) {
    try {
    $quser = quser /server:$computer 2>&1 | Select-Object -Skip 1

    foreach($user in $quser) {
    $line = [regex]::split($user.Trim(), '\s{2,}')

    $o = if ($line[2] -eq 'Disc') { 0 } else { 1 }

    [PSCustomObject]@{
    ComputerName = $computer
    UserName = $line[0]
    SessionName = $null + $o
    Id = $line[1 + $o]
    State = $line[2 + $o]
    IdleTime = $line[3 + $o]
    LogonTime = $line[4 + $o]
    }

    }
    } catch {
    [PSCustomObject]@{
    ComputerName = $computer
    Error = $_.Exception.Message
    } | Select-Object -Property UserName,ComputerName,SessionName,Id,State,IdleTime,LogonTime,Error
    }
    }
    }
    }