Functions/Get-Quser.ps1
function Get-Quser { [CmdletBinding()] param ( [Parameter()] [string] $UserName, [Parameter()] [string] $Identity, [Parameter()] [string] [ValidateSet("Active", "Disc")] $State, [Parameter()] [int] $IdleTimeHours, [Parameter()] [int] $IdleTimeMinutes # [Parameter()] [string] $LogonTime ) $SessionInfo = @() $quser = quser 2>$null ForEach ($line in $quser) { If ($line -match "logon time") { Continue } # Parse idle time $Idle = $line.SubString(54, 9).Trim().Replace('+', '.') $IdleTimeRaw = $Idle if ($IdleTimeRaw -like "*.*") { $IdleSplit1 = $Idle.split(".") $IdleDays = $IdleSplit1[0] $IdleTimeRaw = $IdleSplit1[1] # $IdleDays } if ($IdleTimeRaw -like "*:*") { $IdleSplit2 = $IdleTimeRaw.split(":") $IdleHours = $IdleSplit2[0] $IdleMinutes = $IdleSplit2[1] } elseif ($IdleTimeRaw -eq "none") { $IdleDays = 0 $IdleHours = 0 $IdleMinutes = 0 } else { $IdleDays = 0 $IdleHours = 0 $IdleMinutes = $IdleTimeRaw } $SessionInfo += [PSCustomObject]@{ Username = $line.SubString(1, 20).Trim() SessionName = $line.SubString(23, 17).Trim() ID = $line.SubString(42, 2).Trim() State = $line.SubString(46, 6).Trim() Idle = $Idle IdleDays = [int]$IdleDays IdleHours = [int]$IdleHours IdleMinutes = [int]$IdleMinutes LogonTime = [datetime]::Parse($line.SubString(65)) } } if ($State) { $SessionInfo = $SessionInfo | Where-Object State -EQ $State } if ($Identity) { $SessionInfo = $SessionInfo | Where-Object ID -EQ $Identity } if ($UserName) { $SessionInfo = $SessionInfo | Where-Object Username -EQ $UserName } if ($IdleTimeHours) { $SessionInfo = $SessionInfo | Where-Object IdleHours -GE $IdleTimeHours } if ($IdleTimeMinutes) { $SessionInfo = $SessionInfo | Where-Object IdleMinutes -GE $IdleTimeMinutes } return $SessionInfo } |