Skip to content

Instantly share code, notes, and snippets.

@figueroadavid
Last active February 18, 2021 22:05
Show Gist options
  • Select an option

  • Save figueroadavid/068640d6f3bedba76af9149f7c59979b to your computer and use it in GitHub Desktop.

Select an option

Save figueroadavid/068640d6f3bedba76af9149f7c59979b to your computer and use it in GitHub Desktop.
Converts QUser data into PSCustomObjects
function Get-QuserInfo {
<#
.SYNOPSIS
Gets QUser information as objects
.DESCRIPTION
Parses quser.exe output for a given computer and outputs it as objects
.EXAMPLE
PS C:\> Get-QuserInfo -ComputerName Server1
UserName : user2
SessionName : ica-cgp#54
SessionID : 3
SessionState : Active
LogonTime : 2/18/2021 6:52 AM
IdleTime : .
UserName : user1
SessionName : ica-cgp#130
SessionID : 5
SessionState : Active
LogonTime : 2/18/2021 8:06 AM
IdleTime : 41
UserName : user3
SessionName : ica-cgp#72
SessionID : 6
SessionState : Active
LogonTime : 2/18/2021 12:29 PM
IdleTime : 56
.INPUTS
[string]
.OUTPUTS
[pscustomobject]
.PARAMETER ComputerName
The name of the computer to run quser against.
.NOTES
By default, quser.exe does not require any special permissions to run.
#>
[cmdletbinding()]
param(
[parameter()]
[string]$ComputerName = $env:computername
)
$Pattern = '^[> ](?<UserName>\w+?)?\s+(?<SessionName>[-a-z#0-9]+)?\s+(?<SessionID>\d+?)?\s+(?<SessionState>\w+?)?\s+(?<IdleTime>[0-9+#.]+?)?\s+(?<LogonTime>.*)$'
$QUserInfo = quser /server:$ComputerName
for ($i = 1 ; $i -lt $QUserInfo.Count; $i++) {
$null = $QUserInfo[$i] -match $Pattern
[pscustomobject]@{
UserName = $Matches.UserName
SessionName = $Matches.SessionName
SessionID = $Matches.SessionID
SessionState = $Matches.SessionState
LogonTime = $Matches.LogonTime
IdleTime = $Matches.IdleTime
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment