-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathGet-UserSession.ps1
447 lines (366 loc) · 18.4 KB
/
Get-UserSession.ps1
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
Function Get-UserSession
{
<#
.SYNOPSIS
PowerShell wrapper for "query user" command which also offers resolving the Display Name of users and gathering of user session process information
.DESCRIPTION
PowerShell wrapper for "query user" command which also offers resolving the Display Name of users and gathering of user session process information
.PARAMETER ComputerName
The computer name(s) for which you want to gather user session information
.PARAMETER ResolveDisplayName
Used to determine whether or not to attempt resolving the Display Name for each user
.PARAMETER IncludeProcessInfo
Used to determine whether or not to gather process information for each user session
.PARAMETER Credential
Used to pass into the Invoke-Command function for gathering process information. This is exposed so that you have flexibility for connectivity.
.PARAMETER Port
Used to pass into the Invoke-Command function for gathering process information. This is exposed so that you have flexibility for connectivity.
.PARAMETER UseSSL
Used to pass into the Invoke-Command function for gathering process information. This is exposed so that you have flexibility for connectivity.
.PARAMETER Authentication
Used to pass into the Invoke-Command function for gathering process information. This is exposed so that you have flexibility for connectivity.
.PARAMETER SessionOption
Used to pass into the Invoke-Command function for gathering process information. This is exposed so that you have flexibility for connectivity.
.PARAMETER BatchSize
Used for the runspace pooling that is utilized for more efficient parallel processing. This is exposed so that you have flexibility for runspace behavior.
Default value is calculated from:
[int]$env:NUMBER_OF_PROCESSORS + 1
.PARAMETER ApartmentState
Used for the runspace pooling that is utilized for more efficient parallel processing. This is exposed so that you have flexibility for runspace behavior.
Default value is 'STA'
.PARAMETER ShowProgress
Used to determine whether or not to display a progress bar as the runspace pool is processed
.EXAMPLE
Get-UserSession
Gets the current user sessions from the local machine
.EXAMPLE
Get-UserSession -ComputerName (Get-ADComputer -Filter *).DnsHostname -ShowProgress -Verbose | Format-Table -Autosize
Gets the user sessions from all Active Directory computers while showing a progress bar and displaying verbose information
.EXAMPLE
'ComputerA', 'ComputerB' | Get-UserSession -ResolveDisplayName | Format-Table -Autosize
Gets the user sessions from ComputerA and ComputerB and resolves the Display Name of the users for the output
#>
[cmdletbinding()]
Param(
[Parameter(
ValueFromPipeline = $true
, ValueFromPipelineByPropertyName = $true
)]
[string[]]$ComputerName
,
[switch]$ResolveDisplayName
,
[Parameter(ParameterSetName = 'ProcessInfo')]
[switch]$IncludeProcessInfo
,
[Parameter(ParameterSetName = 'ProcessInfo')]
[pscredential]$Credential
,
[Parameter(ParameterSetName = 'ProcessInfo')]
[int]$Port = -1
,
[Parameter(ParameterSetName = 'ProcessInfo')]
[switch]$UseSSL
,
[Parameter(ParameterSetName = 'ProcessInfo')]
[System.Management.Automation.Runspaces.AuthenticationMechanism]$Authentication
,
[Parameter(ParameterSetName = 'ProcessInfo')]
[System.Management.Automation.Remoting.PSSessionOption]$SessionOption
,
[ValidateScript({
If ($_ -lt 1) { Throw 'Please specify a value of 1 or greater.' }
Else { $true }
})]
[int]$BatchSize = [int]$env:NUMBER_OF_PROCESSORS + 1
,
[ValidateSet(
'STA'
,'MTA'
)]
[string]$ApartmentState = 'STA'
,
[switch]$ShowProgress
)
Begin
{
$Verbose = ($PSBoundParameters.Verbose -eq $true)
Write-Verbose -Message "----------" -Verbose:$Verbose
Write-Verbose -Message "Function '$($MyInvocation.MyCommand)' was called" -Verbose:$Verbose
# Initialize the runspace pool
$Pool = [RunspaceFactory]::CreateRunspacePool(1, $BatchSize)
$Pool.ApartmentState = $ApartmentState
$Pool.Open()
$GetProcessInfo = $IncludeProcessInfo.IsPresent
# Determine if the current console is elevated, which is needed to get process information tied to users
If (-not ([Security.Principal.WindowsPrincipal][Security.Principal.WindowsIdentity]::GetCurrent()).IsInRole([Security.Principal.WindowsBuiltInRole] "Administrator"))
{
If ($GetProcessInfo)
{
Write-Warning -Message "The 'IncludeProcessInfo' parameter requires requires elevated user rights. Try running the command again in a session that has been opened with elevated user rights (that is, Run as Administrator)."
$GetProcessInfo = $false
}
}
If ($GetProcessInfo)
{
$ProcessInfoScriptBlock = {
$CpuCores = (Get-WmiObject -Class Win32_Processor).NumberOfLogicalProcessors
$ProcessPerformanceCounters = Get-WmiObject -Class Win32_PerfFormattedData_PerfProc_Process
# Logic used to calculate CPU usage value
$CpuProperty = @{
Name = 'Cpu'
Expression = {
$Ids = $_.Group.Id
[System.Math]::Round(
($ProcessPerformanceCounters |
Where-Object -FilterScript { $Ids -contains $_.IDProcess } |
Select-Object -Property @{
Name = 'PercentProcessorTime'
Expression = {$_.PercentProcessorTime / $CpuCores}
} |
Measure-Object -Property PercentProcessorTime -Sum).Sum
, 1
)
}
}
# Logic used to calculate memory usage value
$MemoryProperty = @{
Name = 'Memory'
Expression = {
$Ids = $_.Group.Id
[System.Math]::Round(
($ProcessPerformanceCounters |
Where-Object -FilterScript { $Ids -contains $_.IDProcess } |
Measure-Object -Property workingSetPrivate -Sum).Sum / 1MB
, 1
)
}
}
# Get process information and group by UserName to return the UserName, CPU usage, memory usage, and the actual processes
Get-Process -IncludeUserName |
Group-Object -Property UserName |
Select-Object -Property @(
@{
Name = 'Username'
Expression = { $_.Name.Split('\')[-1] }
}
, $CpuProperty
, $MemoryProperty
, @{
Name = 'Processes'
Expression = { $_.Group }
}
)
} # $ProcessInfoScriptBlock
} # If ($GetProcessInfo)
$UserSessionScriptBlock = {
Param(
[string]$ComputerName
,
[bool]$ResolveDisplayName
,
[object[]]$UserProcesses
,
[bool]$Verbose
)
If ([string]::IsNullOrEmpty($ComputerName))
{
$ComputerName = 'localhost'
}
# Check for connectivity over TCP port 135 (WMI) with a 1 second timeout
$TestPort = 135
$TimeoutMilliseconds = 1000
$TCPClient = [System.Net.Sockets.TcpClient]::new()
$Proceed = $TCPClient.BeginConnect($ComputerName, $TestPort, $null, $null).AsyncWaitHandle.WaitOne($TimeoutMilliseconds, $false)
$TCPClient.Close()
If ($Proceed)
{
Write-Verbose -Message "Running query.exe against $($ComputerName)." -Verbose:$Verbose
$Users = query user /server:$ComputerName 2>&1
If ($Users -match "No User exists")
{
Write-Warning -Message "There were no users found on $($ComputerName)." -Verbose:$Verbose
}
ElseIf ($Users -match "Error")
{
Write-Error -Message "There was an error running query against $($ComputerName) : $Users"
}
ElseIf ($Users -eq $null -and $ErrorActionPreference -eq 'SilentlyContinue')
{
# Handdle null output called by -ErrorAction.
Write-Verbose -Message "Error action has supressed output from query.exe. Results were null." -Verbose:$Verbose
}
Else
{
Write-Verbose -Message "Users found on $($ComputerName). Converting output from text." -Verbose:$Verbose
# Conversion logic. Handles the fact that the sessionname column may be populated or not.
$Users = $Users | ForEach-Object {
(($_.trim() -replace ">" -replace "(?m)^([A-Za-z0-9-._]{3,20})\s+(\d+\s+\w+)", '$1 none $2' -replace "\s{2,}", "," -replace "none", $null))
} | ConvertFrom-Csv
$CountString = "$($Users.Count) user$('s' * [int]($Users.Count -ne 1))"
Write-Verbose -Message "Generating output for $($CountString) connected to $($ComputerName)." -Verbose:$Verbose
# Output objects.
foreach ($User in $Users)
{
# Convert the LOGON TIME value to a formatted date/time string
If (-not [string]::IsNullOrEmpty($User.'LOGON TIME'))
{
$SlashSplit = $User.'LOGON TIME'.Split('/')
$SpaceSplit = $SlashSplit[-1].Split(' ')
$ColonSplit = $SpaceSplit[1].Split(':')
$ParsedLogonTime = @{
Month = ([int]$SlashSplit[0]).ToString('00')
Day = ([int]$SlashSplit[1]).ToString('00')
Year = $SpaceSplit[0]
Hour = ([int]$ColonSplit[0]).ToString('00')
Minute = $ColonSplit[-1]
AmPm = $SpaceSplit[-1]
}
$LogonTimeString = '{0}/{1}/{2} {3}:{4} {5}' -f $ParsedLogonTime.Month, $ParsedLogonTime.Day, $ParsedLogonTime.Year, $ParsedLogonTime.Hour, $ParsedLogonTime.Minute, $ParsedLogonTime.AmPm
}
# Build the return object from all of the session information
$HashObject = [Ordered]@{
ComputerName = $ComputerName
Username = $User.USERNAME
DisplayName = ""
SessionId = $User.ID
SessionState = $User.STATE.Replace("Disc", "Disconnected")
SessionType = $($User.SESSIONNAME -Replace '#', '' -Replace "[0-9]+", "")
IdleTime = If ($User.'IDLE TIME' -ne '.' -and $User.'IDLE TIME' -notmatch '^24692+')
{
$IdleTimeValues = $User.'IDLE TIME' -replace '(\d+[+])*(\d+[:])*([0-9]+)', '$1;$2;$3' -replace '[+]' -replace '[:]' -split ';'
[TimeSpan]::new($IdleTimeValues[0], $IdleTimeValues[1], $IdleTimeValues[2], 0)
};
LogonTime = If ($User.'LOGON TIME') { [DateTime]::ParseExact($LogonTimeString, 'MM/dd/yyyy hh:mm tt', [Globalization.CultureInfo]::InvariantCulture) };
}
# Attempt to resolve the Display Name if the switch parameter was specified, otherwise remove the attribute from the return object
If ($ResolveDisplayName)
{
$HashObject['DisplayName'] = (Get-WmiObject -Class Win32_UserAccount -Namespace "root\cimv2" -Filter "Name = '$($HashObject['Username'])'").FullName
}
Else
{
$HashObject.Remove('DisplayName')
}
# Add the CPU, memory, and process information to the return object
If ($UserProcesses.Count -gt 0)
{
If ($ComputerName -eq 'localhost')
{
$ComputerUserProcesses = $UserProcesses.Where( {[string]::IsNullOrEmpty($_.PSComputerName) -and ($_.UserName -eq $HashObject.Username)})[0]
}
Else
{
$ComputerUserProcesses = $UserProcesses.Where( {($_.PSComputerName -eq $ComputerName) -and ($_.UserName -eq $HashObject.Username)})[0]
}
$HashObject['Cpu'] = $ComputerUserProcesses.Cpu
$HashObject['Memory'] = $ComputerUserProcesses.Memory
$HashObject['Processes'] = $ComputerUserProcesses.Processes
}
# Return the object after casting as a PSCustomObject
[PSCustomObject]$HashObject
} # foreach ($User in $Users)
} # Else
} # If ($Proceed)
Else
{
Write-Warning -Message "Computer '$($ComputerName)' is unreachable on port $($TestPort)"
}
} # $UserSessionScriptBlock
}
Process
{
If ($GetProcessInfo)
{
$InvokeCommandSplat = @{
ScriptBlock = $ProcessInfoScriptBlock
ErrorAction = 'Continue'
}
If ($ComputerName.Count -gt 0)
{
$InvokeCommandSplat['ComputerName'] = $ComputerName
}
If ($null -ne $Credential)
{
$InvokeCommandSplat['Credential'] = $Credential
}
If (-1 -ne $Port)
{
$InvokeCommandSplat['Port'] = $Port
}
If ($UseSSL.IsPresent)
{
$InvokeCommandSplat['UseSSL'] = $UseSSL.IsPresent
}
If ($null -ne $Authentication)
{
$InvokeCommandSplat['Authentication'] = $Authentication
}
If ($null -ne $SessionOption)
{
$InvokeCommandSplat['SessionOption'] = $SessionOption
}
# Run Invoke-Command to get the process information after building a splat with all necessary parameters
Write-Verbose -Message "Running Invoke-Command with specified parameters to gather process information" -Verbose:$verbose
$UserProcesses = Invoke-Command @InvokeCommandSplat
}
Else
{
$UserProcesses = @()
}
If ($ComputerName.Count -eq 0)
{
$ComputerName = @('')
}
If ($ShowProgress.IsPresent)
{
$ProgressActivity = 'Receiving Results'
$TotalCompleted = 0
}
# Build a runspace for each computer and execute it
$RunSpaces = foreach ($computerEntry in $ComputerName)
{
$Pipeline = [PowerShell]::Create()
$null = $Pipeline.AddScript($UserSessionScriptBlock)
$ScriptBlockParams = @(
$computerEntry
,$ResolveDisplayName.IsPresent
,$UserProcesses
,$Verbose
)
foreach ($parameter in $ScriptBlockParams)
{
$null = $Pipeline.AddArgument($parameter)
}
$Pipeline.RunspacePool = $Pool
[PSCustomObject]@{
Pipeline = $Pipeline
Status = $Pipeline.BeginInvoke()
}
} # foreach ($computerEntry in $ComputerName)
# Process the various runspace streams and output, while displaying progress if desired
While ($RunSpaces.Status -ne $null)
{
$Completed = $RunSpaces | Where-Object -FilterScript { $_.Status.IsCompleted -eq $true }
If ($ShowProgress.IsPresent)
{
$TotalCompleted += $Completed.Count
[int]$PercentComplete = ($TotalCompleted / $RunSpaces.Count) * 100
Write-Progress -Id 1 -Activity $ProgressActivity -PercentComplete $PercentComplete -Status "Percent Complete: $($PercentComplete)%"
}
foreach ($RunSpace in $Completed)
{
# EndInvoke method retrieves the results of the asynchronous call
$RunSpace.Pipeline.Streams.Warning | ForEach-Object -Process { Write-Warning -Message $_ }
$RunSpace.Pipeline.Streams.Verbose | ForEach-Object -Process { Write-Verbose -Message $_ -Verbose:$Verbose }
$RunSpace.Pipeline.EndInvoke($RunSpace.Status)
$RunSpace.Status = $null
$RunSpace.Pipeline.Dispose()
}
}
If ($ShowProgress.IsPresent)
{
Write-Progress -Id 1 -Activity $ProgressActivity -Completed
}
} # Process
} # Function Get-UserSession