-
Notifications
You must be signed in to change notification settings - Fork 17
/
Copy pathparse-etl-files.ps1
375 lines (321 loc) · 13 KB
/
parse-etl-files.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
<#
.SYNOPSIS
Parses ETL files into csv format using pktmon or netsh
.DESCRIPTION
This script reads Windows ETL files.
The script uses pktmon or netsh to convert the ETL files to CSV format.
.NOTES
File Name : parse-etl-files.ps1
Author : jagilber
.EXAMPLE
.\parse-etl-files.ps1 -etlFilesPath "C:\Windows\Logs\WindowsUpdate" -outputDir "C:\Windows\Logs\WindowsUpdate\csv" -useNetsh -etlMaxProcessorInstance 4 -tmfPath "C:\users\Public\TMF" -sort -force -sleepSeconds 1
.PARAMETER etlFilesPath
Path to the directory containing the WindowsUpdate ETL files
.PARAMETER etlFileFilter
Filter for the ETL files. Default is "*.etl"
.PARAMETER useNetsh
Using pktmon to parse the ETL files is about 33% faster than netsh but not available on all systems
.PARAMETER outputFile
If specified, the script will save the parsed entries to a CSV file or JSON file
.PARAMETER outputDir
Directory to save the parsed CSV files
.PARAMETER etlMaxProcessorInstance
Maximum number of ETL files to process simultaneously
.PARAMETER tmfPath
Path to the TMF files
.PARAMETER sort
Sort the entries by level
.PARAMETER force
Overwrite existing files
.PARAMETER sleepSeconds
Number of seconds to wait between checking for completed jobs
.PARAMETER loadFunctions
Load the script functions
#>
[cmdletbinding()]
param(
# [Parameter(Mandatory = $true)]
[string]$etlFilesPath, # Path to the directory containing the WindowsUpdate ETL files
[string]$etlFileFilter = "*.etl",
[switch]$useNetsh, # Using pktmon to parse the ETL files is about 33% faster than netsh but not available on all systems
# [string]$outputFile, #= "$pwd\WindowsUpdate.csv" # if specified, the script will save the parsed entries to a CSV file or JSON file
[string]$outputDir,
[int]$etlMaxProcessorInstance = 4,
[string]$tmfPath = 'C:\users\Public\TMF',
[switch]$sort,
[switch]$force,
[int]$sleepSeconds = 1,
[switch]$loadFunctions
)
$global:startTime = get-date
$global:etlEventEntries = [System.Collections.ArrayList]::New()
$global:outputFiles = @{}
$global:scriptName = $null #$MyInvocation.ScriptName #"$psscriptroot\$($MyInvocation.MyCommand.Name)"
#$commandLine = $global:myinvocation.myCommand.definition
$scriptParams = $PSBoundParameters
function main() {
try {
$global:scriptName = $MyInvocation.ScriptName
if (!(test-path $etlFilesPath)) {
get-help $global:scriptName -Examples
write-error "The specified path does not exist: $etlFilesPath"
return $null
}
$process = if ($usepktmon) { 'pktmon' } else { 'netsh' }
$etlFiles = @(get-childItem -Path $etlFilesPath -Filter $etlFileFilter -Recurse)
$usepktmon = !$useNetsh -and ($null -ne (Get-Command -Name pktmon -ErrorAction SilentlyContinue))
$totalFiles = $etlFiles.Count
$count = 0
remove-jobs
if ($totalFiles -gt 0) {
foreach ($etlFile in $etlFiles) {
monitor-processes -maxCount $etlMaxProcessorInstance -process $process
$count++
write-log -data "file $count of $totalFiles"
$outputFile = "$outputDir\$([io.path]::GetFileNameWithoutExtension($etlFile)).csv"
# write-log -data "netsh trace convert input=`"$etlFile`" output=`"$outputFile`" report=no overwrite=yes"
# start-process -FilePath $process -ArgumentList "trace convert input=`"$etlFile`" output=`"$outputFile`" report=no overwrite=yes" -NoNewWindow
write-progress -Activity "Processing ETL files" -Status "Processing $etlFile" -PercentComplete (($etlFiles.IndexOf($etlFile) / $totalFiles) * 100)
[void]$global:outputFiles.Add($outputFile, (format-etlFile -fileName $etlFile.FullName -outputFileName $outputFile -usepktmon $usepktmon))
}
wait-jobs
# wait for all processes to finish
# monitor-processes -maxCount 0 -process $process
# foreach ($outputFile in $global:outputFiles.GetEnumerator()) {
# if ($usepktmon) {
# # $eventEntries = read-pktmonCsvFile -fileName $outputFile.key
# $outputFileName = "$($outputFile.key).json"
# # $job = (read-pktmonCsvFile -fileName $outputFile.key -outputFile $outputFile) &
# # $job = start-job -ScriptBlock { param($fileName, $outputFile) read-pktmonCsvFile -fileName $fileName -outputFile $outputFile } -ArgumentList $outputFile.key, $outputFile
# $job = start-job -ScriptBlock {
# param($global:scriptName, $scriptParams, $fileName, $outputFile)
# Start-Sleep -Seconds 5
# # Wait-Debugger
# [void]$scriptParams.Add("loadFunctions", $true)
# write-host ". $global:scriptName @scriptParams"
# . $global:scriptName @scriptParams
# write-host "read-pktmonCsvFile -fileName $fileName -outputFile $outputFile "
# read-pktmonCsvFile -fileName $fileName -outputFile $outputFile
# } -ArgumentList ($MyInvocation.ScriptName, $scriptParams, $outputFile.key, $outputFileName) -Debug
# if ($DebugPreference -ine "SilentlyContinue") {
# write-log -data 'debugging job' -ForegroundColor Cyan
# #Wait-Debugger
# #Start-Sleep -Seconds 5
# debug-job -Job $job.childjobs[0] -BreakAll -Debug
# # debug-job -Job $job -BreakAll -Debug
# pause
# }
# }
# else {
# $eventEntries = read-netshCsvFile -fileName $outputFile.key
# }
# [void]$global:etlEventEntries.AddRange(@($eventEntries))
# }
wait-jobs
}
else {
write-error "No ETL files found in the specified directory: $etlFilesPath"
return $null
}
# save-toFile -outputFile $outputFile
$levelGroups = $global:etlEventEntries.Level | Group-Object | Sort-Object | Select-Object Count, Name
write-log -data "Level Counts:$($levelGroups| out-string)" -ForegroundColor Cyan
write-log -data "Total entries: $($global:etlEventEntries.Count)"
write-log -data "Entries saved to `$global:etlEventEntries"
return $global:etlEventEntries
}
catch {
write-log -data "exception::$($psitem.Exception.Message)`r`n$($psitem.scriptStackTrace)" -ForegroundColor Red
return $null
}
}
function format-etlFile([string]$fileName, [string]$outputFileName, [bool]$usepktmon) {
write-verbose "format-etlFile:$fileName"
$error.clear()
$result = $false
$parentDir = split-path -Path $outputFileName
if (!(test-path $parentDir)) {
write-log -data "md (split-path -Path $outputFileName)" -ForegroundColor Cyan
mkdir $parentDir
}
if (!$outputFileName) {
$outputFileName = "$fileName.csv"
}
if ((test-path -PathType Leaf -Path $outputFileName)) {
write-warning "file exists: $outputFileName"
if ($force) {
write-warning "remove-item -Path $outputFileName -Force"
remove-item -Path $outputFileName -Force
}
else {
return $null
}
}
if ($usepktmon) {
write-log -data "pktmon etl2txt $fileName -o $outputFileName -m -v 5 -p $tmfPath" -ForegroundColor Cyan
$job = pktmon etl2txt $fileName -o $outputFileName -m -v 5 -p $tmfPath &
# $eventEntries = read-pktmonCsvFile -fileName $outputFileName
}
else {
write-log -data "netsh trace convert input=$fileName output=$outputFileName" -ForegroundColor Cyan
$job = netsh trace convert input=$fileName output=$outputFileName &
# $eventEntries = read-netshCsvFile -fileName $outputFileName
}
write-log -data $job -ForegroundColor Green
if ($errror -or $LASTEXITCODE -ne 0) {
write-log -data "Failed to parse ETL file: $fileName" -ForegroundColor Red
return $null
}
# remove-item -Path $outputFileName -Force
write-log -data "format-etlFile: $fileName - $($eventEntries.Count) entries"
return $eventEntries
}
function monitor-processes([int]$maxCount, [string]$process) {
while ($true) {
if ((get-process) -match $process) {
$instanceCount = (get-process -Name ($process)).Length
write-log -data "instance count:$($instanceCount)"
if ($instanceCount -ge $maxCount) {
write-log -data "waiting for $($process) instances to finish."
write-log -data " current instance count: $($instanceCount) seconds waiting: $($count++)"
start-sleep -Seconds 1
continue
}
}
break
}
}
function remove-jobs() {
try {
foreach ($job in get-job) {
write-log -data "removing job $($job.Name)" -report $global:scriptName
write-log -data $job -report $global:scriptName
$job.StopJob()
Remove-Job $job -Force
}
}
catch {
write-log -data "error:$($Error | out-string)"
$error.Clear()
}
}
function save-toFile([string]$outputFile) {
if (!$outputFile) { return }
write-log -data "Saving entries to $outputFile" -ForegroundColor Cyan
if ((test-path -PathType Leaf -Path $outputFile)) {
write-warning "remove-item -Path $outputFile -Force"
remove-item -Path $outputFile -Force
}
if ($outputFile.ToLower().EndsWith(".json")) {
$global:etlEventEntries | ConvertTo-Json | Out-File -FilePath $outputFile
}
else {
# export-csv does not format datetime correctly
$streamWriter = [IO.StreamWriter]::New($outputFile, $false)
$streamWriter.WriteLine("Time,PID,TID,Level,Keyword,Provider,Event,Info")
foreach ($entry in $global:etlEventEntries) {
$line = "$($entry.time.ToString('o')),$($entry.pid),$($entry.tid),$($entry.level),$($entry.keyword),$($entry.provider),$($entry.event),$($entry.info),"
$streamWriter.WriteLine($line)
}
$streamWriter.Close()
}
}
function wait-jobs() {
write-log "monitoring jobs"
while (get-job) {
foreach ($job in get-job) {
$jobInfo = (receive-job -Id $job.id)
if ($jobInfo) {
write-log -data $jobInfo
}
else {
write-log -data ($job | Format-List *)
}
if ($job.state -ine "running") {
write-log -data ($job | Format-List *)
if ($job.state -imatch "fail" -or $job.statusmessage -imatch "fail") {
write-log -data $job
}
write-log -data $job
remove-job -Id $job.Id -Force
}
write-progressInfo
start-sleep -Seconds $sleepSeconds
}
}
write-log "finished jobs"
}
function write-log($data, [consoleColor]$foregroundColor = 'White') {
if (!$data) { return }
[text.stringbuilder]$stringData = New-Object text.stringbuilder
if ($data.GetType().Name -eq "PSRemotingJob") {
foreach ($job in $data.childjobs) {
if ($job.Information) {
$stringData.appendline(@($job.Information.ReadAll()) -join "`r`n")
}
if ($job.Verbose) {
$stringData.appendline(@($job.Verbose.ReadAll()) -join "`r`n")
}
if ($job.Debug) {
$stringData.appendline(@($job.Debug.ReadAll()) -join "`r`n")
}
if ($job.Output) {
$stringData.appendline(@($job.Output.ReadAll()) -join "`r`n")
}
if ($job.Warning) {
write-warning (@($job.Warning.ReadAll()) -join "`r`n")
$stringData.appendline(@($job.Warning.ReadAll()) -join "`r`n")
$stringData.appendline(($job | format-list * | out-string))
$global:resourceWarnings++
}
if ($job.Error) {
write-error (@($job.Error.ReadAll()) -join "`r`n")
$stringData.appendline(@($job.Error.ReadAll()) -join "`r`n")
$stringData.appendline(($job | format-list * | out-string))
$global:resourceErrors++
}
if ($stringData.tostring().Trim().Length -lt 1) {
return
}
}
}
else {
$stringData = "$(get-date):$($data | format-list * | out-string)"
}
write-host $stringData
}
function write-progressInfo() {
$ErrorActionPreference = $VerbosePreference = 'silentlycontinue'
$errorCount = $error.Count
# write-verbose "Get-AzResourceGroupDeploymentOperation -ResourceGroupName $resourceGroupName -DeploymentName $deploymentName -ErrorAction silentlycontinue"
# $deploymentOperations = Get-AzResourceGroupDeploymentOperation -ResourceGroupName $resourceGroupName -DeploymentName $deploymentName -ErrorAction silentlycontinue
$jobInfo = (get-job)
$jobInfo.ChildJobs | ForEach-Object {
$job = $_
$status = $job.State
$currentOperation = $job.Name
$status = "$status $($job.StatusMessage). $($job.Progress)"
if (!$currentOperation -or $job.State -ieq "Completed") {
# $status = "$status $($job.Output)"
}
else {
Write-Progress -Activity $currentOperation -id ($count++) -Status $status
}
}
$status = "time elapsed: $(((get-date) - $global:startTime).TotalMinutes.ToString("0.0")) minutes. job count: $($jobInfo.ChildJobs.Count)" #`r`n"
write-verbose $status
# $pattern = "/subscriptions/(?<subscriptionId>.+?)/resourceGroups/(?<resourceGroup>.+?)/providers/(?<provider>.+?)/(?<providerType>.+?)/(?<resource>.+)"
$count = 0
if ($errorCount -ne $error.Count) {
$error.RemoveRange($errorCount - 1, $error.Count - $errorCount)
}
# if ($detail) {
$ErrorActionPreference = $VerbosePreference = 'continue'
# }
}
if ($loadFunctions) {
write-log -data "loaded script functions" -ForegroundColor Cyan
}
else {
main
}