How to extract specific string from files that matches Pattern in Powershell?

Viewed 40

I have a requirement: To scan in a directory for all the application logs ($dir) present and pick the latest one ($fileList) to check that if the latest log file contains "error" (pattern) in the log file ($fileList). Then extract all the required information from them and store them in variable so as to pass to json as parameter to Azure Log Analytics workspace.

I have written like :

$dir = 'C:\ProgramData\AecorsoftDataIntegrator\logs\'
$fileList = (Get-ChildItem -Path $dir -Filter '2022*.log' | Sort-Object LastWriteTime -Descending | Select-Object -First 1).fullname
$search= (Get-Content $fileList | Select-String -Pattern 'error')

The log looks like : log file content

I would like to extract the : error time : (highlighted in yellow in the image link) ; job name : OPT_VIM1_HEAD (highlighted in yellow in the image link) and if possible the whole error message (marked in red barces) and store them in some variables so that I can process them and display them in console

Please help me with the script. Newbie to powershell

0 Answers
Related