Home > Software design >  Objectify the results of Qwinsta
Objectify the results of Qwinsta

Time:06-22

I am trying to write a script that will give me back as an object the results from qwinsta. The code of my script is this:

function Get-QWinsta {
    $queryResults = (qwinsta /Server:$ENV:COMPUTERNAME | ForEach-Object { ( ( $_.Trim() -replace "\s ",",") ) } | ConvertFrom-Csv)

    $queryResults | ForEach-Object { if ( $_.SESSIONNAME -match 'da-' ) { 
                                        $_.USERNAME = $_.SESSIONNAME; $_.SESSIONNAME = $null; $_.STATE = $_.ID; $_.ID = $null 
                                        }
                                    
    }
    
    $RDPSESSIONS = $queryResults | ForEach-Object {
        [PSCustomObject]@{
            UserName = if ( $_.USERNAME -match '\d' ) { $_.USERNAME = $null } else { $_.USERNAME }
            SessionName = if ( $_.SESSIONNAME -match 'services|console' ) { $_.SESSIONNAME = $null } else { $_.SESSIONNAME } 
            SessionID = $_.ID
            SessionState = if ( $_.ID -match 'Disc' ) { $_.STATE = 'Disconnected' } else { $_.STATE }
        }
    } 
    return $RDPSESSIONS 
}

and the output is this:

UserName    SessionName    SessionID    SessionState
--------    -----------    ---------    ------------                                
                           Disc 
                           Conn
Admin01                                 Disc                                        
Admin02     rdp-tcp#41     4            Active
            rdp-tcp                     Listen

However the above is not a real object and what I would really want to have as output is something like this:

UserName     SessionName     SessionID     SessionState                                
--------     -----------     ---------     ------------                                
Admin01                                    Disc
Admin02      rdp-tcp#41      4             Active

Plus if I could something like this:

> $user1 = (Get-Qwinsta).UserName
> Write-Output $user1
> Admin01

that would be a bonus.

I have read all the similar post here and everywhere on the internet and none of them worked perfectly fine or did what I want to achieve.

CodePudding user response:

You can get the qwinsta results as custom objects like this:

function Get-QWinsta
{
    param ($ComputerName = "$env:COMPUTERNAME")

    qwinsta /server:$ComputerName |
        ForEach-Object {$_ -replace "\s{2,18}",","} | 
            ConvertFrom-Csv
}

> Get-QWinsta -ComputerName 'Server1'

This gives a custom object for each entry in the output:

SESSIONNAME : services
USERNAME    : 
ID          : 0
STATE       : Disc
TYPE        : 
DEVICE      : 

SESSIONNAME : console
USERNAME    : 
ID          : 1
STATE       : Conn
TYPE        : 
DEVICE      : 

SESSIONNAME : session1
USERNAME    : User1
ID          : 23
STATE       : Active
TYPE        : wdica
DEVICE      : 

SESSIONNAME : session2
USERNAME    : User2
ID          : 25
STATE       : Active
TYPE        : wdica
DEVICE      : 

Which can be manipulated like other objects. For example, shown in a table:

> Get-QWinsta -ComputerName 'Server1' | Format-Table -Autosize

SESSIONNAME      USERNAME    ID    STATE  TYPE  DEVICE 
-----------      --------    --    -----  ----  -------
services                     0     Disc                
console                      1     Conn                
session1         User1       23    Active wdica        
session2         User2       25    Active wdica        
session3         User3       41    Active wdica

Or, output just specific properties:

> (Get-QWinsta -ComputerName 'Server1').UserName

User1
User2
User3
  • Related