Haosolo
Haosolo

Reputation: 11

How do you output extra lines near a specific string pattern in powershell 2.0?

I'm working on a Powershell script for Server log monitoring purpose. Basically, I need to look into the log file and check the date where next to a specific string pattern.

With the following code, I was only able to locate the string pattern, but I need couple more lines near this string patter.

$fPath = "C:\temp\imlog.txt"
Get-Content $fPath | Where-Object {$_.Contains('ORA-')}

An Example of the log:

ORA-03135: connection lost contact

Tue Sep 26 12:49:29 2017

ORA-01775: looping chain of synonyms

Sat Sep 30 16:29:22 2017

Any help is appreciated.

Thanks

Upvotes: 0

Views: 59

Answers (1)

Haosolo
Haosolo

Reputation: 11

Credit goes to @EBGreen and @Bill_Stewart.

the following script will do the job.

#Define log file path
$fPath = "C:\temp\imlog.txt"

#Output Context near string pattern
Get-Content $fPath | Select-String -Patter 'ORA-' -Context 2 | Select -Last 1

Thanks to all.

Upvotes: 1

Related Questions