-
Notifications
You must be signed in to change notification settings - Fork 0
/
PSWinEventLogTool.ps1
435 lines (377 loc) · 13.2 KB
/
PSWinEventLogTool.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
#PowerShell Windows Event Log Export And Compression Tool Script by Julian McConnell
#https://julianmcconnell.com
#Version 20230809a
#Does require admin privileges due to the security log option
#Requires -RunAsAdministrator
#Experimental parameters support (by request)
#$param_evt_log = which event log (follows the integers in the selection menu)
#$param_time_unit = which time unit (follows the integers in the selection menu)
#$param_time_how_much = how much of the specific time unit (except if all time was selected for $param_time_unit)
param ([int] $param_evt_log, $param_time_unit, $param_time_how_much)
#Date and time setup
[String]$day = (get-date).day
[String]$month = (get-date).month
[String]$year = (get-date).year
[String]$hour = (get-date).hour
[String]$minute = (get-date).minute
[String]$second = (get-date).second
[String]$ms = (get-date).millisecond
#Let's make a timestamp format
[String]$timestampformat = $year + $month + $day + '-' + $hour + $minute + $second + $ms
#Event log selection menu
function EventLogSelectionMenu
{
#Basic menu for selections
Clear-Host
Write-Host 'Which event log would you like to export and compress?'
Write-Host 'Press 1 For Application Log'
Write-Host 'Press 2 For System Log'
Write-Host 'Press 3 For Security Log'
Write-Host 'Press 4 For All Logs'
Write-Host 'Press 5 To Exit Script'
#User input prompt for the selection
[int]$selectionmenuselection = Read-Host 'Please type a selection...'
#Selections
#1 = Application
If ($selectionmenuselection -eq '1')
{
[String]$selectedlog = 'Application'
TimeFrameSelectionMenu
}
#2 = System
ElseIf ($selectionmenuselection -eq '2')
{
$selectedlog = 'System'
TimeFrameSelectionMenu
}
#3 = Security
ElseIf ($selectionmenuselection -eq '3')
{
$selectedlog = 'Security'
TimeFrameSelectionMenu
}
#4 = All
ElseIf ($selectionmenuselection -eq '4')
{
$selectedlog = 'All'
TimeFrameSelectionMenu
}
#If the selection was 5, let's quit
ElseIf ($selectionmenuselection -eq '5')
{
exit
}
#Catch-all for if there's an invalid choice entered (return to top of menu)
else
{
#Warn user about invalid entry
Write-Host 'Invalid entry. Please try again!'
#Pause
Pause
#Return to previous menu to try again
EventLogSelectionMenu
}
}
#Time unit selection menu
function TimeFrameSelectionMenu
{
#Basic menu for timeframe selection
Clear-Host
Write-Host 'What is the desired timeframe/unit for this export?'
Write-Host 'Press 1 For Minutes'
Write-Host 'Press 2 For Hours'
Write-Host 'Press 3 For Days'
Write-Host 'Press 4 For Weeks'
Write-Host 'Press 5 For All Time'
Write-Host 'Press 6 To Exit Script'
#User input prompt for the timeframe selection
[int]$timeframemenuselection = Read-Host 'Please type a selection...'
#Selections
#1 = Minutes
If ($timeframemenuselection -eq '1')
{
[String]$unitoftime = 'Minutes'
SpecificTimeSelection
}
#2 = Hours
ElseIf ($timeframemenuselection -eq '2')
{
[String]$unitoftime = 'Hours'
SpecificTimeSelection
}
#3 = Days
ElseIf ($timeframemenuselection -eq '3')
{
[String]$unitoftime = 'Days'
SpecificTimeSelection
}
#4 = Weeks
ElseIf ($timeframemenuselection -eq '4')
{
[String]$unitoftime = 'Weeks'
SpecificTimeSelection
}
#5 = All Time
ElseIf ($timeframemenuselection -eq '5')
{
#
[String]$unitoftime = 'All Time'
CheckOptionsToProceed
}
#If the selection was 6, let's quit
ElseIf ($timeframemenuselection -eq '6')
{
exit
}
#Otherwise, proceed
else
{
#Proceed to time selection menu
TimeFrameSelectionMenu
}
}
#Specific time input menu
function SpecificTimeSelection
{
#Basic input for specific time selection
Clear-Host
Write-Host 'How much of the specific time unit chosen would you like to export?'
#Previously-selected options drive the write-host text
If ($timeframemenuselection -eq '1')
{
Write-Host '(In Minutes)'
}
ElseIf ($timeframemenuselection -eq '2')
{
Write-Host '(In Hours)'
}
ElseIf ($timeframemenuselection -eq '3')
{
Write-Host '(In Days)'
}
ElseIf ($timeframemenuselection -eq '4')
{
Write-Host '(In Weeks)'
}
#User input prompt for the amount (how much time)
[int]$howmuchtime = Read-Host 'Please enter amount...'
#If nothing was entered, go down this path
if ($null -eq $howmuchtime)
{
#Warn user that there was no value entered
Write-Host 'No value entered!'
#Pause
Pause
#Return to menu
SpecificTimeSelection
}
#Otherwise, proceed
else
{
#Move along to calculate the time differential
CalculateTimeDiff
}
}
#Calculate the time differential
function CalculateTimeDiff
{
#Driven from previous timeframe menu selections in TimeFrameSelectionMenu
If ($timeframemenuselection -eq '1')
{
#Calculate total time differential and then proceed to check options
[Int32]$totaltime = 60000 * $howmuchtime
CheckOptionsToProceed
}
ElseIf ($timeframemenuselection -eq '2')
{
#Calculate total time differential and then proceed to check options
[Int32]$totaltime = 3600000 * $howmuchtime
CheckOptionsToProceed
}
ElseIf ($timeframemenuselection -eq '3')
{
#Calculate total time differential and then proceed to check options
[Int32]$totaltime = 86400000 * $howmuchtime
CheckOptionsToProceed
}
ElseIf ($timeframemenuselection -eq '4')
{
#Calculate total time differential and then proceed to check options
[Int32]$totaltime = 604800000 * $howmuchtime
CheckOptionsToProceed
}
else
{
#Just in case!
CheckOptionsToProceed
}
}
function CheckOptionsToProceed
{
#We need to check what we're doing to go forward
#If selection is all logs and all time
If ($selectionmenuselection -eq '4' -and $timeframemenuselection -eq '5')
{
#Proceed
ExportAllLogsForAllTime
}
#If selection is all logs and specific time
ElseIf ($selectionmenuselection -eq '4' -and $timeframemenuselection -ne '5')
{
#Proceed
ExportAllLogsForSpecificTime
}
#If selection is specific log and all time
ElseIf ($selectionmenuselection -ne '4' -and $timeframemenuselection -eq '5')
{
#Proceed
ExportSpecificLogForAllTime
}
#If selection is specific log and specific time
ElseIf ($selectionmenuselection -ne '4' -and $timeframemenuselection -ne '5')
{
#Proceed
ExportSpecificLogForSpecificTime
}
}
#Export all logs for all time
function ExportAllLogsForAllTime
{
#Export all logs for all time
wevtutil epl Application "${PSScriptRoot}\Application_All.evtx"
wevtutil epl System "${PSScriptRoot}\System_All.evtx"
wevtutil epl Security "${PSScriptRoot}\Security_All.evtx"
#Proceed to check if we're ready to compress
ReadyToZip
}
#Export all logs for a specific timeframe
function ExportAllLogsForSpecificTime
{
#Export all logs for a specific amount of time
#Setup file names for the logs
[String]$applicationlogfilename = "${PSScriptRoot}\" + "Application_Last_" + [String]$howmuchtime + "_" + $unitoftime + ".evtx"
[String]$systemlogfilename = "${PSScriptRoot}\" + "System_Last_" + [String]$howmuchtime + "_" + $unitoftime + ".evtx"
[String]$securitylogfilename = "${PSScriptRoot}\" + "Security_Last_" + [String]$howmuchtime + "_" + $unitoftime + ".evtx"
#Setup queries for the logs
[String]$applicationquery = "*[System[TimeCreated[timediff(@SystemTime) <=" + [String]$totaltime + "]]]"
[String]$systemquery = "*[System[TimeCreated[timediff(@SystemTime) <=" + [String]$totaltime + "]]]"
[String]$securityquery = "*[System[TimeCreated[timediff(@SystemTime) <=" + [String]$totaltime + "]]]"
#Export the logs with queries and filenames
wevtutil epl Application /q:$applicationquery "$applicationlogfilename"
wevtutil epl System /q:$systemquery "$systemlogfilename"
wevtutil epl Security /q:$securityquery "$securitylogfilename"
#Proceed to check if we're ready to compress
ReadyToZip
}
#Export specific logs for all time
function ExportSpecificLogForAllTime
{
#Export the specific log for all time
If ($selectionmenuselection -eq '1')
{
#Export the application log
wevtutil epl Application "${PSScriptRoot}\Application_All.evtx"
}
ElseIf ($selectionmenuselection -eq '2')
{
#Export the system log
wevtutil epl System "${PSScriptRoot}\System_All.evtx"
}
ElseIf ($selectionmenuselection -eq '2')
{
#Export the security log
wevtutil epl Security "${PSScriptRoot}\Security_All.evtx"
}
#Proceed to check if we're ready to compress
ReadyToZip
}
#Export specific logs for specific time
function ExportSpecificLogForSpecificTime
{
#Export specific log for a specific amount of time
If ($selectionmenuselection -eq '1' -and $timeframemenuselection -ne '5')
{
#Export the application log for the specific time
[String]$applicationlogfilename = "${PSScriptRoot}\" + "Application_Last_" + [String]$howmuchtime + "_" + $unitoftime + ".evtx"
[String]$applicationquery = "*[System[TimeCreated[timediff(@SystemTime) <=" + [String]$totaltime + "]]]"
wevtutil epl Application /q:$applicationquery "$applicationlogfilename"
}
ElseIf ($selectionmenuselection -eq '2' -and $timeframemenuselection -ne '5')
{
#Export the system log for the specific amount of time
[String]$systemlogfilename = "${PSScriptRoot}\" + "System_Last_" + [String]$howmuchtime + "_" + $unitoftime + ".evtx"
[String]$systemquery = "*[System[TimeCreated[timediff(@SystemTime) <=" + [String]$totaltime + "]]]"
wevtutil epl System /q:$systemquery "$systemlogfilename"
}
ElseIf ($selectionmenuselection -eq '2' -and $timeframemenuselection -ne '5')
{
#Export the security log for the specific amount of time
[String]$securitylogfilename = "${PSScriptRoot}\" + "Security_Last_" + [String]$howmuchtime + "_" + $unitoftime + ".evtx"
[String]$securityquery = "*[System[TimeCreated[timediff(@SystemTime) <=" + [String]$totaltime + "]]]"
wevtutil epl Security /q:$securityquery "$securitylogfilename"
}
#Proceed to check if we're ready to compress
ReadyToZip
}
#Check if we are ready to zip the files
function ReadyToZip
{
#Check if the process is running
$check=Get-Process wevtutil -ErrorAction SilentlyContinue
if ($null -eq $check)
{
#If not running, proceed to compress logs
CompressLogs
}
else
{
#If still running, wait and then go back again
Start-Sleep -s 2
ReadyToZip
}
}
#Compress the logs
function CompressLogs
{
#Time to compress what we exported
Write-Host 'Compressing log files...'
Compress-Archive -Path "${PSScriptRoot}\*.evtx" -DestinationPath "${PSScriptRoot}\Windows_Event_Log_Export_$timestampformat.zip"
Remove-Item "${PSScriptRoot}\*.evtx" -Force
#Quit when finished
quitscript
}
#Quit the script
function quitscript
{
#Cleanup
#Clear-Host
Write-Host 'Complete!'
Exit
}
#bypass menus if a proper combo of parameters were passed along
#if $param_evt_log was greater than or equal to 1 or less than or equal to 4, and $param_time_unit was greater than or equal to 1 or less than or equal to 4, and $param_time_how_much was greater than or equal to 1
If ((($param_evt_log -ge '1') -and ($param_evt_log -le '4')) -and (($param_time_unit -ge '1') -and ($param_time_unit -le '4')) -and ($param_time_how_much -ge '1'))
{
#set three things via this path - the log selection, the time unit, and how much time to export
#set $selectionmenuselection from $param_evt_log
$selectionmenuselection = $param_evt_log
#set $timeframemenuselection from $param_time_unit
$timeframemenuselection = $param_time_unit
#set $howmuchtime from $param_time_how_much
$howmuchtime = $param_time_how_much
#skip ahead with the new stuff, just as we would via a menu walk-through
CalculateTimeDiff
}
#elseif $param_evt_log is greater than or equal to 1, and less than or equal to 4, and $param_time_unit is equal to 5 (all events in the selected log/all time equivalent)
ElseIf ((($param_evt_log -ge '1') -and ($param_evt_log -le '4')) -and ($param_time_unit -eq '5'))
{
#set two things via this path - the log selection and the time unit, and how much time to export
#set $selectionmenuselection from $param_evt_log
$selectionmenuselection = $param_evt_log
#set $timeframemenuselection from $param_time_unit
$timeframemenuselection = $param_time_unit
#skip ahead with the new stuff, just as we would via a menu walk-through
CheckOptionsToProceed
}
#Call the first menu
EventLogSelectionMenu