Advertisement
guyrleech

Parse quser.exe output to objects

Jul 18th, 2024 (edited)
518
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PowerShell 1.28 KB | Software | 0 0
  1. ## convert quser.exe output to objects. deals with empty sessionname for disconnected sessions and > prefixing username for current session plus time formats that use AM/PM
  2. ## can also remote with /server:
  3.  
  4. ##  USERNAME              SESSIONNAME        ID  STATE   IDLE TIME  LOGON TIME
  5. ## admingle                                  2  Disc         1:22  18/07/2024 10:57
  6. ##>billybob              rdp-tcp#1           4  Active          .  18/07/2024 15:33
  7.  
  8. (quser.exe /server:cuxenvda19-03) -replace '\bLOGON TIME\b' , 'LOGONDATE LOGONTIME' -replace '\bIDLE TIME\b' , 'IDLETIME' -replace '\s+' , ' ' -replace '^\s+'  | % { if( $_ -cmatch '^(.+) (\d+):(\d+) ([AP]M)$' ) { [int]$shift = 0;if( $matches[4] -eq 'PM' ) { $shift = 12 } ; "{0} {1}:{2}" -f $matches[1] , (([int]$matches[2] + $shift ) % 24 ), $matches[3] } else { $_ } } | ConvertFrom-Csv -Delimiter ' ' -PipelineVariable line | % { if( $null -eq $line.LOGONTIME ) { $line.LOGONTIME = $line.LOGONDATE ; $line.LOGONDATE = $line.IDLETIME ; $line.IDLETIME = $line.STATE ; $line.STATE = $line.ID ; $line.ID = $line.SESSIONNAME ; $line.SESSIONNAME = $null } ; if( $thisSession = ($line.USERNAME -match '^>(.*)$' )) { $line.USERNAME = $matches[1] } ; Add-Member -InputObject $line -MemberType NoteProperty -Name THISSESSION -Value $thisSession -PassThru }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement