Created
February 18, 2021 22:08
-
-
Save figueroadavid/79c5009876c016cb52120cabbfadd8c1 to your computer and use it in GitHub Desktop.
Get-QWinstaInfo converts the output into objects
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
| function Get-QwinstaInfo { | |
| <# | |
| .SYNOPSIS | |
| Gets QWinsta information as objects | |
| .DESCRIPTION | |
| Parses qwinsta.exe output for a given computer and outputs it as objects | |
| .EXAMPLE | |
| PS C:\> Get-QwinstaInfo -ComputerName Server1 | |
| SessionName : services | |
| UserName : | |
| SessionID : 0 | |
| SessionState : Disc | |
| SessionType : | |
| Device : | |
| SessionName : console | |
| UserName : | |
| SessionID : 1 | |
| SessionState : Conn | |
| SessionType : | |
| Device : | |
| SessionName : ica-cgp#85 | |
| UserName : user1 | |
| SessionID : 516 | |
| SessionState : Active | |
| SessionType : wdica | |
| Device : | |
| SessionName : ica-cgp#109 | |
| UserName : user2 | |
| SessionID : 520 | |
| SessionState : Active | |
| SessionType : wdica | |
| Device : | |
| SessionName : ica-cgp | |
| UserName : | |
| SessionID : 65536 | |
| SessionState : Listen | |
| SessionType : | |
| Device : | |
| SessionName : ica-ssl | |
| UserName : | |
| SessionID : 65537 | |
| SessionState : Listen | |
| SessionType : | |
| Device : | |
| SessionName : ica-tcp | |
| UserName : | |
| SessionID : 65538 | |
| SessionState : Listen | |
| SessionType : | |
| Device : | |
| SessionName : rdp-tcp | |
| UserName : | |
| SessionID : 65539 | |
| SessionState : Listen | |
| SessionType : | |
| Device : | |
| .INPUTS | |
| [string] | |
| .OUTPUTS | |
| [pscustomobject] | |
| .PARAMETER ComputerName | |
| The name of the computer to run quser against. | |
| .NOTES | |
| By default, qwinsta.exe does not require any special permissions to run. | |
| #> | |
| [cmdletbinding()] | |
| param( | |
| [parameter()] | |
| [string]$ComputerName = $env:computername | |
| ) | |
| $Pattern = '^[> ](?<SessionName>[-a-zA-Z#0-9]+)\s+(?<UserName>\w+)?\s+(?<SessionID>\d+)\s+(?<SessionState>\w+)\s+(?<SessionType>\w+)?\s+(?<Device>\w+)?$' | |
| $QWinstaInfo = qwinsta /server:$ComputerName | |
| for ($i = 1 ; $i -lt $QWinstaInfo.Count; $i++) { | |
| $null = $QWinstaInfo[$i] -match $Pattern | |
| [pscustomobject]@{ | |
| SessionName = $Matches.SessionName | |
| UserName = $Matches.UserName | |
| SessionID = $Matches.SessionID | |
| SessionState = $Matches.SessionState | |
| SessionType = $Matches.SessionType | |
| Device = $Matches.Device | |
| } | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment