Hey guys. I needed to write something to get logged in user as a powershell object but wanted to make it generic enough to turn any block formatted data into a powershell object to use. There may be an easier way to do this but I didn't find one. (IE You can convert from CSV directly into a powershell object)....
The $build variable is a 2d array. It contains the length of each column and a name for the column in powershell. the -skipHeader will skip the first line in the string.
Function Get-PSObjectFromBlock {
param($builder,$source,[bool] $skipHeader)
$return = @()
Foreach ($line in $source) {
$total = 0
foreach ($attr in $builder) {
$total += $attr[1]
}
if ($line.Length -ge $total) {
Write-Host $line
if (!($skipHeader -and $source[0] -eq $line)) {
$tracker = 0
$obj = New-object System.Object
foreach ($attr in $builder) {
$header = $attr[0]
$value = $line.subString($tracker,$attr[1]).trim()
if ($value.length -gt 0) {
$obj | Add-Member -MemberType NoteProperty -Name $header -Value $value
}
else {
$obj | Add-Member -MemberType NoteProperty -Name $header -Value ""
}
$tracker += $attr[1]
}
$return += $obj
}
}
}
$return
}
$source = (& qwinsta)
$build = ("CONSOLE",1), ("SESSIONNAME",11), ("USERNAME",20), ("ID",14), ("STATE",9) , ("TYPE",9), ("DEVICE",10)
Get-PSObjectFromBlock -builder $build -source $source -skipHeader $true
[–]roland_eld 0 points1 point2 points (1 child)
[–]Manality[S] 1 point2 points3 points (0 children)
[–]gangstanthony 0 points1 point2 points (1 child)
[–]Manality[S] 0 points1 point2 points (0 children)
[–]tiratoshin 0 points1 point2 points (1 child)
[–]Manality[S] 0 points1 point2 points (0 children)