Private/ADLookups/_GetLoggedInUser.ps1
Function _GetLoggedInUser { [CmdletBinding()] [OutputType([System.Object[]])] param( [Parameter(Mandatory = $true)] $Identity ) $Object = @() If (-not (Test-Connection -ComputerName $Identity -Quiet -Count 1 -ErrorAction SilentlyContinue)) { Return "$Identity is Offline" } $stringOutput = quser /server:$Identity 2>$null If (!$stringOutput) { Return "Unable to retrieve quser info for `"$Identity`"" } ForEach ($line in $stringOutput) { If ($line -match 'logon time') { Continue } $Object += [PSCustomObject]@{ ComputerName = $Identity 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 = $line.SubString(54, 9).Trim().Replace('+', '.') LogonTime = [datetime]$line.SubString(65) } } Return $Object.Username } |