-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy patharchiveEventlogs.ps1
417 lines (391 loc) · 11.6 KB
/
archiveEventlogs.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
##########################################################
# Powershell Documentation
##########################################################
# .SYNOPSIS
# This is a script that will archive the event logs from remote computers to a local directory
# .DESCRIPTION
# This is a script that will archive the event logs from remote computers to a local directory. It can accept a single or multiple hosts via AD calls, CSV files and command line parameters
# .PARAMETER hostCsvPath
# The path the a CSV File containing hosts
# .PARAMETER computers
# A comma separated list of hostnames
# .PARAMETER OU
# An Organizational Unit in Active Directory to pull host names from
# .PARAMETER archiveLocation
# The location the event logs should be stored
# .PARAMETER onlyFull
# If present, the log files will only be archived if they are above 90% of the log file capacity
# .PARAMETER clearLogs
# If present, the log files will be cleared after being archived
# .PARAMETER manualExec
# Whether of not to auto-execute
# .EXAMPLE
# C:\PS>.\archiveEventlogs.ps1 -computers "hostname1,hostname2" -archiveLocation "c:\logarchive\"
# This example will gather the events from the computers entered into the command line
# .INPUTS
# There are no inputs that can be directed to this script
# .OUTPUTS
# All outputs are sent to the console and logged in the log folder
# .NOTES
# Author: Robert Weber
# Date: Dec 30, 2014
##########################################################
# API Documentation
##########################################################
##########################################################
# About: Script Information
#
# Filename - archiveEventLogs.ps1
# Synopsis - This is a script that will archive the event logs from remote computers to a local directory
# Input - There are no inputs that can be directed to this script
# Output - All output is sent to the console and logged in the log folder
#
# Requirements:
# - Powershell V2
# - Admin Privileges
#
# Deprecated:
# FALSE
#
# Command Line Arguments:
# OU - An Organizational Unit in Active Directory to pull host names from
# hostCsvPath -The path the a CSV File containing hosts
# computers - A comma separated list of hostnames
# archiveLocation -The location the event logs should be stored
# onlyFull - If present, the log files will only be archived if they are above 90% of the log file capacity
# clearLogs -If present, the log files will be cleared after being archived
# manualExec - Whether or not to auto execute
#
# Examples:
# .\archiveEventlogs.ps1 -computers "hostname1,hostname2" -archiveLocation "c:\logarchive\" - This example will gather the events from the computers entered into the command line
#
# Links:
# https://software.forge.mil/sf/projects/diacap_tools
#
# Authors:
# Robert Weber
#
# Version History:
# Dec 30, 2014 - Initial Script Creation
# Dec 2, 2015 - Added code documentation
#
# Notes:
#
# Todo:
#
##########################################################
[CmdletBinding()]
param( $hostCsvPath = "", $computers = @(), $OU = "", $archiveLocation, [switch] $onlyFull, [switch] $clearLogs, [switch] $manualExec )
clear
$error.clear()
. .\lib\PSClass.ps1
(gci .\lib) | % { . "$($_.FullName)" }
##########################################################
# Class: ArchiveEventLogClass
# This is a script that will archive the event logs from remote computers to a local directory. It can accept a single or multiple hosts via AD calls, CSV files and command line parameters
#
# Dependencies:
# - <PSClass>
#
# Provides:
# - <ArchiveEventLogClass>
#
# Deprecated:
# FALSE
#
# See Also:
#
# Links:
#
# Notes:
#
# Todo:
#
##########################################################
$archiveEventLogClass = new-PSClass ArchiveEventLog{
##########################################################
# Variables: Static Properties
# PsScriptName - The name of the executing script
# Description - A description of what this script does
##########################################################
note -static PsScriptName "archiveEventlogs"
note -static Description ( ($(((get-help .\archiveEventlogs.ps1).Description)) | select Text).Text)
###########################################################
# Variables: Private Properties
# HostObj - An object containing all the hosts to be scanned
# mainProgressBar - A Container for the main script progress bar
# gui - The gui object for the script
# archiveLocation - the path for where the archives will be stored
# onlyFull - A flag to only archive logs closed to full
# clearLogs - A flag to clear the logs once archived
###########################################################
note -private HostObj @{}
note -private gui
note -private archiveLocation
note -private onlyFull
note -private clearLogs
note -private mainProgressBar
##########################################################
# Method: Constructor
# Creates a new object for the <archiveEventLogClass> Script
#
# Parameters:
# NA
#
# Scope:
# Public
#
# Dependencies:
# - <HostsClass>
# - <utilities>
# - <uiClass>
# - <guiClass>
#
# Returns:
# New <applyPoliciesClass> Object
#
# Deprecated:
# False
#
# See Also:
#
# Links:
#
# Notes:
#
# Todo:
#
##########################################################
constructor{
param()
$private.HostObj = $HostsClass.New($hostCsvPath, $computers, $OU)
$private.archiveLocation = $archiveLocation
$private.onlyFull = $onlyFull
$private.clearLogs = $clearLogs
while($utilities.isBlank($private.archiveLocation) -eq $true){
$private.gui = $null
$private.gui = $guiClass.New("archiveEventlogs.xml")
$private.gui.generateForm();
$private.gui.Controls.btnOpenFolderBrowser.add_Click({ $private.gui.Controls.txtArchiveLocation.Text = $private.gui.actInvokeFolderBrowser() })
$private.gui.Controls.btnExecute.add_Click({ $private.gui.Form.close() })
$private.gui.Form.ShowDialog()| Out-Null
$private.archiveLocation = $private.gui.Controls.txtArchiveLocation.Text
$private.onlyFull = $private.gui.Controls.chkOnlyFull.Checked
$private.clearLogs = $private.gui.Controls.chkClearLogs.Checked
}
}
##########################################################
# Method: Execute
# Executes the body of this script
#
# Parameters:
# NA
#
# Scope:
# Public
#
# Dependencies:
# - <progressBarClass>
# - <uiClass>
#
# Deprecated:
# False
#
# Returns:
# N/A
#
# See Also:
#
# Links:
#
# Notes:
#
# Todo:
#
##########################################################
method Execute{
$currentComputer = 0
if($private.HostObj.Count -gt 0){
$private.mainProgressBar = $progressBarClass.New( @{ "parentId" = 0; "Activity" = "0 / 0 :"; "Status" = ("0% complete" ); "PercentComplete" = 0; "Completed" = $false; "id" = 1 }).Render()
$private.HostObj.Hosts.keys | % {
$currentComputer = $currentComputer + 1
$i = (100*($currentComputer / $private.HostObj.Hosts.count))
$private.mainProgressBar.Activity("$currentComputer / $($private.HostObj.Hosts.count): Processing system $_").Status(("{0:N2}% complete" -f $i)).Percent($i).Render()
$Result = Get-WmiObject -Class win32_pingstatus -Filter "address='$($_.Trim())'"
if( ($Result.StatusCode -eq 0 -or $Result.StatusCode -eq $null ) -and $utilities.isBlank($Result.IPV4Address) -eq $false ) {
if($_.length -ge 1) {
$uiClass.writeColor( "$($uiClass.STAT_OK) Processing $_" )
$this.getBackUpFolder($_)
}
} else {
$uiClass.writeColor( "$($uiClass.STAT_ERROR) Skipping $_ .. not accessible" )
}
}
$private.mainProgressBar.Completed($true).Render()
}
$uiClass.errorLog()
}
##########################################################
# Method: getBackUpFolder
# Creates the archive folder path and a temporary folder
# on the remote host
#
# Parameters:
# NA
#
# Scope:
# Public
#
# Dependencies:
# - <progressBarClass>
# - <uiClass>
#
# Deprecated:
# False
#
# Returns:
# N/A
#
# See Also:
#
# Links:
#
# Notes:
#
# Todo:
#
##########################################################
method getBackUpFolder{
param(
[string] $computer
)
#create a folder on log server for event logs
$folder = "{0:yyyyMMdd-HHmmss}" -f [DateTime]::now
New-Item "$($private.ArchiveLocation)\$computer\$folder" -type Directory -force
# create folder on remote system to temporarily hold logs
If(!(Test-Path "\\$computer\c$\LogFolder\$folder")){
New-Item "\\$computer\c$\LogFolder\$folder" -type Directory -force | out-Null
}
$this.backupEventLogs($computer, $folder)
}
##########################################################
# Method: backupEventLogs
# Copies the event logs from the remote host to the
# temporary folder on the remote host
#
# Parameters:
# computer - The host being archived
# folder - The folder on the remote host to temporarily store the event logs
#
# Scope:
# Public
#
# Dependencies:
# - <uiClass>
#
# Deprecated:
# False
#
# Returns:
# N/A
#
# See Also:
#
# Links:
#
# Notes:
#
# Todo:
#
##########################################################
method backupEventLogs{
param(
$computer,
$folder
)
$Eventlogs = Get-WmiObject -Class Win32_NTEventLogFile -ComputerName $computer
Foreach($log in $EventLogs){
if($utilities.isblank($log) -eq $false){
if($private.onlyFull){
if($log.FileSize / $log.MaxFileSize -gt .9){
$uiClass.writeColor("$($uiClass.STAT_OK) Archiving #green#$($log.LogFileName)# Log")
$path = "\\$computer\c$\LogFolder\$folder\{0}.evt" -f $log.LogFileName
try{
$log.BackupEventLog($path) | out-null
}catch{
$uiClass.writeColor("$($uiClass.STAT_ERROR) Error Archiving #yellow#$($log.LogFileName)# Log on #green#$($computer)#")
}
}else{
$uiClass.writeColor("$($uiClass.STAT_WARN) Skipping #green#$($log.LogFileName)# Log, log file not nearing capacity.")
}
}else{
$uiClass.writeColor("$($uiClass.STAT_OK) Archiving #green#$($log.LogFileName)# Log")
$path = "\\$computer\c$\LogFolder\$folder\{0}.evt" -f $log.LogFileName
try{
$log.BackupEventLog($path) | out-null
}catch{
$uiClass.writeColor("$($uiClass.STAT_ERROR) Error Archiving #yellow#$($log.LogFileName)# Log on #green#$($computer)#")
}
}
if($private.clearLogs){
$log.ClearEventLog()
}
}
}
$this.copyEventLogsToArchive($computer,$folder)
remove-item "\\$computer\c$\LogFolder\" -recurse
}
##########################################################
# Method: copyEventLogsToArchive
# Copies the event logs from the active host to the
# archiveLog location
#
# Parameters:
# computer - The host being archived
# folder - The folder on the remote host to temporarily store the event logs
#
# Scope:
# Public
#
# Dependencies:
# - <utilities>
#
# Deprecated:
# False
#
# Returns:
# N/A
#
# See Also:
#
# Links:
#
# Notes:
#
# Todo:
#
##########################################################
method copyEventLogsToArchive{
param(
$computer,
$folder
)
Copy-Item "\\$computer\c$\LogFolder\$folder\*" "$($private.ArchiveLocation)\$computer\$folder" -force
$utilities.zipFolder("$($private.ArchiveLocation)\$computer\$folder")
remove-item "$($private.ArchiveLocation)\$computer\$folder" -recurse
}
}
##########################################################
# Script: Execution
# This is where the script determines if it should
# auto execute
#
# (start code)
# if($manualExec -ne $true){
# $ArchiveEventLogClass.New().Execute() | out-null
# }
# (end code)
##########################################################
if($manualExec -ne $true){
$ArchiveEventLogClass.New().Execute() | out-null
}