I'm trying to create a powershell script to extract all lines containing "ERROR" and its database path to the item into a huge logs txt file and sort it into a csv file. Example of error :
2022-04-17 00:00:00.9999|ERROR|texte:texte|texte \\DATABASE\Path\Path\Path\Path\Item[Item Name] (ID:########-####-####-###-############ Rank:#). description of the error.
I would then like to recover the date and the full path to the element in error (\DATABASE\Path\Path\Path\Path\Item[Item Name]) as well as the description of the error and delete the duplicates. Also I don't know if it is possible to directly separate the date, the path and the message in three columns in the csv file.
Example of logs (screenshot) :
2022-04-17 00:00:00.9999|ERROR|ANDataCache:Configuration|################# Error when adding input attributes to data cache (Failed:8/Total:12) [99.9999999999999 ms].
2022-04-17 00:00:00.9999|ERROR|ANCalculationEngine:Configuration|Failed to initialize \\DATABASE\Path1\Path2\Path3\Path4\Item[1. Item Name] (ID:########-####-####-###-############ Rank:#). Failed to resolve required input 'input A name'
Failed to resolve required input 'input B name'
No output is defined.
2022-04-17 00:00:00.9999|WARN|ANTimeClassManagerHelper:Configuration|Ignoring partial cache signup errors for \\DATABASE\Path1\Path2\Path3\Path4\Item[1. Item Name] (ID:########-####-####-###-############ Rank:#). Failed to signup some input(s) for receiving updates.
Net Volume in Tank: Point not found 'Point Name'.
2022-04-17 00:00:00.9999|ERROR|ANCalculationEngine:Configuration|Failed to initialize \\DATABASE\Path1\Path2\Path3\Path4\Item[1. Item Name] (ID:########-####-####-###-############ Rank:#). Failed to resolve required input 'input name'
There is no time rule configured for this analysis.
No output is defined.
2022-04-17 00:00:00.9999|WARN|###########:#########|############[#####] Ignoring attempt to remove non-existent calculation '\\DATABASE\Path1\Path2\Path3\Path4\Item[1. Item Name] (ID:########-####-####-###-############ Rank:#)'
2022-04-17 00:00:00.9999|ERROR|ANDataCache:Configuration|DataCache:################ Error when adding input attributes to data cache (Failed:8/Total:12) [99.9999999999999 ms].
Example of expected result (according to the example above)
(I just want to retrieve ERRORS with path ("\DATABASE\Path\Path\Path\Path\Item[Item Name]"), not the WARNINGS logs or the ERRORS without path)
I started writing this:
$File = "logs.txt"
$Pattern = '(\[ERROR\[^\\]+(?<DatabasePath>[^\\]]+\])(?<ErrorText>[^\r\n]+=)'
$Content = Get-Content $File
[regex]::Matches($Content, $Pattern).Value | Set-Content "output.csv"
Or to just retrieve the path :
$File = "logs.txt"
$Pattern = '(?<=\\DATABASE\\).+?(?=])'
$Content = Get-Content $File
[regex]::Matches($Content, $Pattern).Value | Set-Content "output.csv"
But in the second case "DATABASE" does not appear in the output file.
Thank you in advance for your answers.