-
Notifications
You must be signed in to change notification settings - Fork 3
/
RDPConnectionParser.ps1
76 lines (56 loc) · 2.44 KB
/
RDPConnectionParser.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
<#
.SYNOPSIS
This script reads the event log "Microsoft-Windows-TerminalServices-LocalSessionManager/Operational" from
multiple servers and outputs the human-readable results to a CSV. This data is not filterable in the native
Windows Event Viewer.
Version: November 9, 2016
.DESCRIPTION
This script reads the event log "Microsoft-Windows-TerminalServices-LocalSessionManager/Operational" from
multiple servers and outputs the human-readable results to a CSV. This data is not filterable in the native
Windows Event Viewer.
NOTE: Despite this log's name, it includes both RDP logins as well as regular console logins too.
Author:
Mike Crowley
https://BaselineTechnologies.com
.EXAMPLE
�
.\RDPConnectionParser.ps1 -ServersToQuery Server1, Server2 -StartTime "November 1"
.LINK
https://MikeCrowley.us/tag/powershell
#>
Param(
[array]$ServersToQuery = (hostname),
[datetime]$StartTime = "January 1, 1970"
)
foreach ($Server in $ServersToQuery) {
$LogFilter = @{
LogName = 'Microsoft-Windows-TerminalServices-LocalSessionManager/Operational'
ID = 21, 23, 24, 25
StartTime = $StartTime
}
$AllEntries = Get-WinEvent -FilterHashtable $LogFilter -ComputerName $Server
$AllEntries | Foreach {
$entry = [xml]$_.ToXml()
[array]$Output += New-Object PSObject -Property @{
TimeCreated = $_.TimeCreated
User = $entry.Event.UserData.EventXML.User
IPAddress = $entry.Event.UserData.EventXML.Address
EventID = $entry.Event.System.EventID
ServerName = $Server
}
}
}
$FilteredOutput += $Output | Select TimeCreated, User, ServerName, IPAddress, @{Name='Action';Expression={
if ($_.EventID -eq '21'){"logon"}
if ($_.EventID -eq '22'){"Shell start"}
if ($_.EventID -eq '23'){"logoff"}
if ($_.EventID -eq '24'){"disconnected"}
if ($_.EventID -eq '25'){"reconnection"}
}
}
$Date = (Get-Date -Format s) -replace ":", "."
$FilePath = "$env:USERPROFILE\Desktop\$Date`_RDP_Report.csv"
$FilteredOutput | Sort TimeCreated | Export-Csv $FilePath -NoTypeInformation -Encoding utf8
Write-host "Writing File: $FilePath" -ForegroundColor Cyan
Write-host "Done!" -ForegroundColor Cyan
#End