So I’m attempted to create a script that look for a specific string inside a .log file and export it to csv file. For example:
5008( 2780) 08/21/2020 02:01:53 JS I TypeError: task has no properties
5008( 2780) 08/21/2020 02:01:58 JS I TypeError: task has no properties
5008( 2780) 08/21/2020 02:02:03 JS I TypeError: task has no properties
5008( 2780) 08/21/2020 02:02:08 JS I TypeError: task has no properties
5008( 2780) 08/21/2020 02:02:14 JS I TypeError: task has no properties
5008( 2780) 08/21/2020 02:02:19 JS I TypeError: task has no properties
4672( 4900) 08/21/2020 02:02:50 JS I INFO - CalcChartSvc - ------- Calculating the charts ------
4672( 4900) 08/21/2020 02:02:50 JS I INFO - CalcChartSvc - Current Operator: report.calccharts
5008( 2164) 08/21/2020 02:08:20 JRTE I Initialize successfully.
4672( 4900) 08/21/2020 02:12:52 JS I INFO - CalcChartSvc - Switching to operator: Incident.Manager, previous: report.calccharts
4672( 4900) 08/21/2020 02:12:52 RTE W ResetMandanten, operator Incident.Manager is not found
5008( 364) 08/21/2020 02:14:54 RTE A Mapping-3-globallists,server.application, SQL Query incomplete because field (server.application) not mapped in file (globallists)
5008( 364) 08/21/2020 02:14:54 RTE A SUMMARY-1 The following event has been reported 15 times in the last 30 minute(s)
5008( 3336) 08/21/2020 03:00:27 RAD I [ERROR][SX_EntityChangeV2]: Trigger entityAfterUpdate incidents RF104462 failed: Could not find incident with id=RF104462, rc=No (more) records found
5008( 3336) 08/21/2020 03:00:41 RAD I [ERROR][SX_EntityChangeV2]: Trigger entityAfterUpdate request RF110543 failed: Could not find incident for dependency RF110543, rc=No (more) records found
What I want to end up seeing is:
ErrorLog DATE TIME ErrorCode ErrorType Message
5008( 3336), 08/21/2020, 03:00:41, RAD I, [ERROR][SX_EntityChangeV2]:, Trigger entityAfterUpdate request RF110543 failed: Could not find incident for dependency RF110543, rc=No (more) records found
I’m getting stuck whether I should create a custom object with hash. Or use regex and substring method.
Any input will help at this point
=======
Update
So I was able to write this script to parse the log file using regex:
(Get-Content.\sm.test.txt-Raw)-split'(\s\s\d\d\d\d.\s\s\d\d\d\d.|\d\d\d\d.\s\s\d\d\d\d.|\s+\d+.\s+\d\d\d.)\s(\d\d\D\d\d\D\d\d\d\d)\s(\d{2}:\d{2}:\d{2})\s+(\w+\s\w+)\s(.*)'|
Where{$_}|
ForEach-Object{
[PSCustomObject]@{
'Col1'=$_.SubString(0,12)
'Col2'=$_.Substring(12).Trim()
}
}