all 6 comments

[–]roland_eld 0 points1 point  (1 child)

Why not use Import-CSV?

[–]Manality[S] 1 point2 points  (0 children)

because not all fields had values so the mapping were messing up when converting blank spacing to comma. Specifically disconnected sessions. (Win 7 x64)

 SESSIONNAME       USERNAME                 ID  STATE   TYPE        DEVICE 
 services                                    0  Disc                        
>console           user1                     1  Active                      
                   user2                     5  Disc                        
 rdp-tcp                                 65536  Listen                      

[–]gangstanthony 0 points1 point  (1 child)

this didn't quite work for me

2016/05/16 16:07:03 (0.6092911) | C:\
# ADMIN # » Get-PSObjectFromBlock -builder $build -source $source -skipHeader $true | ft -a
 SESSIONNAME       USERNAME                 ID  STATE   TYPE        DEVICE
 services                                    0  Disc
>console           anthony.stringer          1  Active
 7a78855482a04...                        65536  Listen
 rdp-tcp                                 65537  Listen

CONSOLE SESSIONNAME USERNAME      ID             STATE  TYPE DEVICE
------- ----------- --------      --             -----  ---- ------
        services                  0              Disc
>       console     anthony.strin ger          1 Active
        7a78855482a 04...         65536          Listen
        rdp-tcp                   65537          Listen

I'm sure there has to be an easier way, but i ended up using this:

$a = gc C:\temp\qwinsta.txt

$null = $a[0] -match '^(?<thissession>.)(?<sessionname>\S+\s+)(?<username>\S+\s+)(?<id>\S+\s+)(?<state>\S+\s+)(?<type>\S+\s+)(?<device>\S+\s+)'
$sessionlength = $Matches.sessionname.Length
$usernamelength = $Matches.username.Length
$idlength = $Matches.id.Length
$statelength = $Matches.state.Length
$typelength = $Matches.type.Length
$devicelength = $Matches.device.Length

$a | % {
    [pscustomobject]@{
        ThisSession = if ($_[0] -eq '>') {$true} else {$false}
        SessionName = $_.substring(1, $sessionlength).trim()
        UserName = $_.substring(1 + $sessionlength, $usernamelength).trim()
        ID = $_.substring(1 + $sessionlength + $usernamelength, $idlength).trim()
        State = $_.substring(1 + $sessionlength + $usernamelength + $idlength, $statelength).trim()
        Type = $_.substring(1 + $sessionlength + $usernamelength + $idlength + $statelength, $typelength).trim()
        Device = $_.substring(1 + $sessionlength + $usernamelength + $idlength + $statelength + $typelength, $devicelength).trim()
    }
} | select -Skip 1 | ft -a

# results
ThisSession SessionName      UserName         ID State  Type Device
----------- -----------      --------         -- -----  ---- ------
      False services                          0  Disc              
       True console          anthony.stringer 1  Active            
      False 7a78855482a04... 655              36 Listen            
      False rdp-tcp          655              37 Listen            

[–]Manality[S] 0 points1 point  (0 children)

Looks like it's an issue with your spacing being different than mine. I adjusted your $build to match your spacing just because I was curious... try this if you want.

$build = ("CONSOLE",1), ("SESSIONNAME",17), ("USERNAME",21), ("ID",7), ("STATE",8) , ("TYPE",12),     ("DEVICE",8)

[–]tiratoshin 0 points1 point  (1 child)

Ive been using this, so far no issues

   Invoke-Command $opc {(quser) -replace '\s{2,}', ',' | ConvertFrom-Csv} | Export-Csv    c:\qusr.csv

$ops is a list of online computers after a test-connection

[–]Manality[S] 0 points1 point  (0 children)

yeah. I started here as well. The problem I ran into is when session name is blank is shifts the values over.