How to read via Powershell a log file to get timestamp code error and message print it to the CSV file?
example: I have a file txt which contains:
2018-11-16 21:01:57, Info DISM DISM Package Manager: PID=5884
TID=5844 Processing the top level command token(add-capability). -
CPackageManagerCLIHandler::Private_ValidateCmdLine
2018-11-16 21:05:32, Error DISM DISM Package Manager: PID=5884
TID=5844 Failed processing package changes with session options
-CDISMPackageManager::ProcessChangesWithOptions(hr:0x800704c7)
I want a report showing only for all errors with DateTime, code_error, message. I use the following regex:
^(0x8)[0-9a-f]{2,7}
for searching the code error, but it doesn't work.
$PathLogFile = $($PathFiles+$FileLog)
$PathErrorFile = $($PathFiles+$FileError)|
$DataLog = $(Get-Content -Path $PathLogFile)
foreach ($Line in $DataLog) {
if (($Line -match '^(hr:0x)[0-9a-f]{2,8}?' ) -and ($Line.contains("Error") -and $Line.contains("Failed"))) {
Write-Host $Line
}
}
Log Parser
for reading logs. – marsze