-
Notifications
You must be signed in to change notification settings - Fork 58
/
MasterParser.ps1
261 lines (192 loc) · 7.68 KB
/
MasterParser.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
param(
# Options
[Parameter(Mandatory = $true)]
[ValidateSet('Start','Menu','Update','Purge')]
[string]$O,
# Mode
[Parameter(Mandatory = $false)]
[ValidateSet('Developer')]
[string]$Mode
)
# current script version
$CurrentVersion = "v2.5"
# tool running path.
$RunningPath = Get-Location
# Dot Sourcing -> 00-Banner.ps1
. "$RunningPath\03-Options\00-Banner.ps1"
# Dot Sourcing -> 05-functions.ps1
. "$RunningPath\03-Options\05-functions.ps1"
# space
Write-Output ""
switch ($Mode) {
'Developer' {
$Mode = "Developer"
}
}
switch ($O) {
'Start' {
# HashTable to store all the $Log names that was analysed by the ParserMaster
$AnalysedLog = @{}
# HashTable to store all the $Log names that are not supported by ParserMaster
$UnsupportedLog = @{}
#List to contain all the $Log files that found a machtch
$VerifiedLogList = @()
# Log Types HashTable.
$LogTypeList = @{
'Auth.Log' = 'Empty'
}
# variable to store all the files under 01-Logs folder.
$Logs = Get-Item -Path "$RunningPath\01-Logs\*" | Select-Object -ExpandProperty Name
# variable to store 0 in it.
$LogFileCount = 0
# iterate file names under 01-Logs.
foreach ($Log in $Logs) {
# clean the flag in each foreach iteration
$WasExtracted = $null
# each $Log add 1 to $LogFileCount flag.
$LogFileCount++
# foreach statment to iterate on each key under $LogTypeList hashtable.
foreach ($Key in $LogTypeList.Keys) {
# clean this flag each iteration
$WasExtracted = $null
# statment to execute if a log file under 01-Logs was found uner $LogTypeList hashtable.
if ($Log -match $Key) {
# list for match found logs
$VerifiedLogList += $Log
# execute this on .gz files.
if ($Log -like "*.gz") {
# flag to state that the log was extracted from a GZip
$WasExtracted = "True"
# Save GZip file name in a variable
$GZipName = $Log
# Specify the .gz file paths
$archivePath = "$RunningPath\01-Logs\$Log"
$destinationPath = "$RunningPath\01-Logs\"
# Create a FileStream to read the .gz file
$fileStream = [System.IO.File]::OpenRead($archivePath)
# Create a GZipStream to decompress the file
$gzipStream = New-Object IO.Compression.GZipStream $fileStream,([IO.Compression.CompressionMode]::Decompress)
# Create a FileStream to write the decompressed data to
$destFilePath = Join-Path $destinationPath (Get-Item $archivePath).BaseName
$destFileStream = [System.IO.File]::Create($destFilePath)
# Copy data from the compressed stream to the destination file
$gzipStream.CopyTo($destFileStream)
# Close the streams
$gzipStream.Close()
$fileStream.Close()
$destFileStream.Close()
# End of GZip Extraction
# execute MasterParser on the GZip output file.
$Log = $Log -replace '\.gz',''
# list for match found logs
$VerifiedLogList += $Log
. "$RunningPath\02-LogModules\$Key\$Key.ps1"
$LogMatchFlag = "True"
$Results = $Log+$RunningTime
$AnalysedLog[$Results] = $Result = "| Log Name: $Log | Archived From: $GZipName | Running Time: $RunningTime |"
}
# execute this on non .gz files.
else {
. "$RunningPath\02-LogModules\$Key\$Key.ps1"
$LogMatchFlag = "True"
# add log names to this hashtable list
$Results = $Log+$RunningTime
$AnalysedLog[$Results] = $Result = "| Log Name: $Log | Archived From: Not Archived | Running Time: $RunningTime |"
#$Results = $null
}
}
# execute if the the $Log name is not found under $LogTypeList hashtable list
else {
$UnsupportedLog[$Log] = "- $Log"
}
}
}
# if statment to write message if 01-Logs is empty.
if ($LogFileCount -eq 0) {
Write-Output "Logs Folder is Empty"
Write-Output "+------------------+"
Write-Output "[!] Error: Folder -> $RunningPath\01-Logs is empty!"
Write-Output "[!] Insert logs into the '01-Logs' folder for the MasterParser to function properly."
Write-Output ""
}
else {
# print the hashtable of logs that been analyzed by MasterParser
Write-Output "List of Successfully Analyzed Logs"
# Initialize variables to store maximum lengths
$MaxChar_LogNameCut = 0
$MaxChar_ArchiveCut = 0
$MaxChar_RunningTimeCut = 0
foreach ($Key in $AnalysedLog.Keys) {
# Get the fiel name
$RemoveStart = $AnalysedLog[$Key] -replace '.*Log Name\: ',''
$LogNameCut = $RemoveStart -replace ' \| Archived From\:.*',''
# Get the archive status
$RemoveStart = $AnalysedLog[$Key] -replace '.*Archived From\: ',''
$ArchiveCut = $RemoveStart -replace ' \| Running Time\:.*',''
# Get the running time
$RemoveStart = $AnalysedLog[$Key] -replace '.*Running Time\: ',''
$RunningTimeCut = $RemoveStart -replace ' \|.*',''
# Update max lengths if necessary
$MaxChar_LogNameCut = [math]::Max($MaxChar_LogNameCut,$LogNameCut.Length)
$MaxChar_ArchiveCut = [math]::Max($MaxChar_ArchiveCut,$ArchiveCut.Length)
$MaxChar_RunningTimeCut = [math]::Max($MaxChar_RunningTimeCut,$RunningTimeCut.Length)
}
# flag to stop $Border iteration after first iteration
$TheFlag = "Enable"
foreach ($Key in $AnalysedLog.Keys) {
# Get the fiel name
$RemoveStart = $AnalysedLog[$Key] -replace '.*Log Name\: ',''
$LogNameCut = $RemoveStart -replace ' \| Archived From\:.*',''
# Get the archive status
$RemoveStart = $AnalysedLog[$Key] -replace '.*Archived From\: ',''
$ArchiveCut = $RemoveStart -replace ' \| Running Time\:.*',''
# Get the running time
$RemoveStart = $AnalysedLog[$Key] -replace '.*Running Time\: ',''
$RunningTimeCut = $RemoveStart -replace ' \|.*',''
$LogNameCut = $LogNameCut.PadRight($MaxChar_LogNameCut)
$ArchiveCut = $ArchiveCut.PadRight($MaxChar_ArchiveCut)
$RunningTimeCut = $RunningTimeCut.PadRight($MaxChar_RunningTimeCut)
$TheResult = "| Log Name: $LogNameCut | Extracted From: $ArchiveCut | Run Time: $auth_log_run_time |"
# multiply $Result.Length with "-" hyfen symbol to get the boarder
$Border = '-' * ($TheResult.Length - 2)
# print the result in a table
if ($TheFlag -match "Enable") {
Write-Output "+$Border+"
$TheFlag = "Disable"
}
$TheResult
}
Write-Output "+$Border+"
Write-Output ""
}
# iteration to remove all the logs that found at least 1 time from the $UnsupportedLog hashtable
foreach ($VLog in $VerifiedLogList) {
$UnsupportedLog.Remove($VLog)
}
# statment to execute if a log file under 01-Logs NOT found uner $LogTypeList hashtable.
if ($UnsupportedLog.Values.Count -ge 1) {
Write-Output "List of Unsupported Logs"
Write-Output "+----------------------+"
$UnsupportedLog.Values
Write-Output ""
}
}
'Update' {
# Dot Sourcing -> 01-Update.ps1
. "$RunningPath\03-Options\01-Update.ps1"
# stop the script here
exit
}
'Menu' {
# Dot Sourcing -> 03-Menu.ps1
. "$RunningPath\03-Options\03-Menu.ps1"
# stop the script here
exit
}
'Purge' {
# Dot Sourcing -> 04-Purge.ps1
. "$RunningPath\03-Options\04-Purge.ps1"
# stop the script here
exit
}
}