ConvertFrom-String

Another way to parse log files is to use the ConvertFrom-String cmdlet, with which you can create a pattern file. You can easily extract two lines from your log file and replace the desired values with {%Name%:%Value%}. The following example demonstrates this approach for the dism.log log file:

#Reading content of log file - last 50 lines
$targetData = Get-Content C:WindowsLogsDISMdism.log -Tail 50

#2 example lines of code - replaced pattern values with {%Name%:%Value%}
$TemplateContent = @'
{Date*:2015-12-15} {Time:21:04:26}, {Level:Info} {Component:DISM} API: PID={PID:1472} TID={TID:5760} {Info:DismApi.dll: - DismInitializeInternal}
{Date*:2015-12-15} {Time:21:04:26}, {Level:Info} {Component:DISM} API: PID={PID:1472} TID={TID:5760} {Info:DismApi.dll: <----- Starting DismApi.dll session -----> - DismInitializeInternal}
'@

#Parse File
$parsedFile = $targetData | ConvertFrom-String -TemplateContent $TemplateContent

#Show parsed data
$parsedFile | Select-Object -First 50 | Format-Table -AutoSize -Wrap

Unfortunately, some complex log files having dynamic content will not always be correctly converted using this method. Therefore, you should always validate your retrieved information.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
18.119.131.72