Created
April 2, 2015 16:51
-
-
Save vScripter/9d48d1efad16f3a44119 to your computer and use it in GitHub Desktop.
This file contains 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
<# | |
.SYNOPSIS | |
Retrieves all logged on users from a local or remote system | |
.DESCRIPTION | |
RRetrieves all logged on users from a local or remote system. | |
This script/function parses the output from the 'qwinsta' command, and formats it into a proper PSObject | |
.INPUTS | |
System.String | |
.OUTPUTS | |
System.Management.Automation.PSCustomObject | |
.PARAMETER ComputerName | |
Name of computer/s | |
.EXAMPLE | |
.\Get-LoggedOnUsers -ComputerName "server1" | |
.EXAMPLE | |
.\Get-LoggedOnUsers -ComputerName (Get-Content C:\ComputerList.txt) | |
#> | |
[cmdletbinding( | |
DefaultParameterSetName = 'session', | |
ConfirmImpact = 'low')] | |
Param ( | |
[Parameter( | |
Mandatory = $True, | |
Position = 0, | |
ValueFromPipeline = $True)] | |
[string[]]$ComputerName | |
) | |
BEGIN { | |
$report = @() | |
} # end BEGIN block | |
PROCESS { | |
foreach ($c in $ComputerName) { | |
Write-Verbose -Message "[$c] Gathering logged on users" | |
$sessions = $null | |
$sessions = query session /server:$c | |
# skip the first session; it's essentially the header when running qwinsta; we want don't care about parsing this and using it | |
foreach ($session in ($sessions | Select-Object -Skip 1)) { | |
$objSession = @() | |
$objSession = [PSCustomObject] @{ | |
ComputerName = $c | |
SessionName = $session.Substring(1, 18).Trim() | |
UserName = $session.Substring(19, 20).Trim() | |
ID = $session.Substring(39, 9).Trim() | |
State = $session.Substring(48, 8).Trim() | |
Type = $session.Substring(56, 12).Trim() | |
Device = $session.Substring(68).Trim() | |
} # end $objSession | |
$objSession | |
} # end foreach $session | |
} # end foreach $c | |
} # end PROCESS block | |
END { | |
} # end END block |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment