-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathRemoteServerUserSessions.ps1
61 lines (50 loc) · 1.93 KB
/
RemoteServerUserSessions.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
<# ***** User Sessions on a Terminal Server *****
05/26/2020 - Jason Franklin
#Script to check all users on Terminal Servers
05/28/2020 - Jason Franklin
#Added SessionID to $Output table
******** Add revisions above ******** #>
#Enter variable for $Servers you want to see user sessions for
$Servers = @(
#AddServerNamesHere
)
#Initialize $Sessions which will contain all sessions
[System.Collections.ArrayList]$Sessions = New-Object System.Collections.ArrayList($null)
#Go through each server
Foreach ($Server in $Servers) {
#Get the current sessions on $Server and also format the output
$Ouput = (quser /server:$Server) -replace '\s{2,}', ',' | ConvertFrom-Csv
#Go through each session in $Ouput
Foreach ($session in $Ouput) {
#Initialize a temporary hash where we will store the data
$tmpHash = @{}
#Check if SESSIONNAME isn't like "console" and isn't like "rdp-tcp*"
If (($session.sessionname -notlike "console") -AND ($session.sessionname -notlike "rdp-tcp*")) {
#If the script is in here, the values are shifted and we need to match them correctly
$tmpHash = @{
Username = $session.USERNAME
SessionName = "" #Session name is empty in this case
SessionID = $session.SESSIONNAME
State = $session.ID
IdleTime = $session.STATE
LogonTime = $session."IDLE TIME"
ServerName = $Server
}
}Else {
#If the script is in here, it means that the values are correct
$tmpHash = @{
Username = $session.USERNAME
SessionName = $session.SESSIONNAME
SessionID = $session.ID
State = $session.STATE
IdleTime = $session."IDLE TIME"
LogonTime = $session."LOGON TIME"
ServerName = $Server
}
}
#Add the hash to $Sessions
$Sessions.Add((New-Object PSObject -Property $tmpHash)) | Out-Null
}
}
#Display the sessions, sort by name, and just show Username, ID and Server
$sessions | Sort Username | select Username, SessionID, ServerName | FT