Last active
October 21, 2024 14:41
-
-
Save khazeamo/0f7ccc163da6bcffd6440daa926cb767 to your computer and use it in GitHub Desktop.
Output of qwinsta.exe varies depending on OS language. Column length seems to vary between OS versions. This is a try to reliably convert the text output of qwinsta.exe.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# Get text output of qwinsta | |
$qwinstaOutput = &qwinsta.exe | |
# Column definitions (in the order as they appear in the output of qwinsta) | |
# Name --> Name of the property in the output object | |
# Start --> Should always be 0 and is detected automatically later in the script (varies depending on the output language) | |
# Length -eq 0 --> Column is left aligned with variable length (detected later in the script) | |
# Length -gt 0 --> Column is right aligned with fixed length | |
$columnInfo = @( | |
[PSCustomObject]@{ | |
Name = 'SessionName' | |
Start = 0 | |
Length = 0 | |
}, | |
[PSCustomObject]@{ | |
Name = 'UserName' | |
Start = 0 | |
Length = 0 | |
}, | |
[PSCustomObject]@{ | |
Name = 'ID' | |
Start = 0 | |
Length = 5 | |
}, | |
[PSCustomObject]@{ | |
Name = 'State' | |
Start = 0 | |
Length = 0 | |
}, | |
[PSCustomObject]@{ | |
Name = 'Type' | |
Start = 0 | |
Length = 0 | |
}, | |
[PSCustomObject]@{ | |
Name = 'ComputerName' | |
Start = 0 | |
Length = 0 | |
} | |
) | |
# Detect position of first character of each column ($columnInfo.Start) and the length of the column ($columnInfo.Length) by analysing the header row | |
$currentCharNumber = 0 # current character position | |
$currentColNumber = 0 # current table column (i.e. array index of $columnInfo) | |
$isLastCharWhitespace = $true # helper to detect if new column starts | |
$headerCharArray = [char[]]$qwinstaOutput[0] # convert header row to char array | |
foreach ($character in $headerCharArray) { | |
# cannot be a new line if its a whitespace or the "current session indicator" (>) in the first char column | |
if ($character -ne ' ' -and ($character -ne '>' -or $currentCharNumber -gt 0)) { | |
# if last char is non-whitespace, it's still the same column | |
if ($isLastCharWhitespace) { | |
# left aligned column with variable length | |
if ($columnInfo[$currentColNumber].Length -eq 0) { | |
$columnInfo[$currentColNumber].Start = $currentCharNumber | |
} | |
# right aligned column with fixed length | |
else { | |
$columnInfo[$currentColNumber].Start = $currentCharNumber - $columnInfo[$currentColNumber].Length | |
} | |
# set length of previous column if we are currently not in the first column | |
if ($currentColNumber -gt 0) { | |
$columnInfo[$currentColNumber - 1].Length = $columnInfo[$currentColNumber].Start - $columnInfo[$currentColNumber - 1].Start | |
} | |
$currentColNumber++ | |
} | |
$isLastCharWhitespace = $false | |
} | |
else { | |
$isLastCharWhitespace = $true | |
} | |
$currentCharNumber++ | |
} | |
# set the length of the last column | |
$columnInfo[-1].Length = $headerCharArray.Length - $columnInfo[-1].Start | |
# create output object | |
$sessions = [System.Collections.Generic.List[PSCustomObject]]::new() | |
# process each row except the header row | |
foreach ($line in ($qwinstaOutput | Select-Object -Skip 1)) { | |
$session = [ordered]@{} | |
# process each column | |
foreach ($column in $columnInfo) { | |
$session[$column.Name] = $line.Substring($column.Start, $column.Length).Trim() | |
} | |
$sessions.Add([PSCustomObject]$session) | |
} | |
$sessions |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment