1

One of the batch scripts creates a logfile and it can have error messages like as below

Msg 2714, Level 16, State 1:
Server 'ABC', Procedure 'proc_test1', Line 189:
There is already an object named 'table_test' in the database.
Msg 207, Level 16, State 4:
Server 'ABC', Procedure 'proc_test2', Line 197:
Invalid column name 'employee'.
Msg 207, Level 16, State 4:
Server 'ABC', Procedure 'proc_test2', Line 197:
Invalid column name 'address'.

Using a powershell script to read and report errors to a mail from the log file. And i'm using this, which excludes certain warning messages too.

Select-String -Path 'C:\Users\BatchLog_20200911.txt' -Pattern "Msg","Error" | Select-String -Pattern "SQLState = S1000, NativeError = 0" -notmatch | select-object -Property Line,LineNumber

Output is as below

Line                                                                                                                LineNumber
----                                                                                                                ----------
Msg 2714, Level 16, State 1:                                                                                              2791
Msg 207, Level 16, State 4:                                                                                               2794
Msg 207, Level 16, State 4:                                                                                               2797
CT-LIBRARY error:                                                                                                         2828
    ct_results(): network packet layer: internal net library error: Net-Library operation terminated due to disconnect       2829
"Errors encountered during execution.  Exited with status: 596"                                                           4168

So, it just prints line which match the query. I want to print the next lines which has actual error message description. Any advise here. Thanks.

0

1 Answer 1

1

Use the -Context parameter of the Select-String cmdlet:

Select-String -Path 'C:\Users\BatchLog_20200911.txt' "Msg","Error" -Context 0,1 | 
  ForEach-Object { 
    if ($_.Context.PostContext[0] -notmatch 'SQLState = S1000, NativeError = 0') {
      [pscustomobject] @{
        Line = $_.Context.PostContext[0]
        LineNumber = $_.LineNumber
      }
    }
  }
  • -Context 0,1 captures 0 lines before and 1 line after along with the actually matching line.

  • In the resulting Microsoft.PowerShell.Commands.MatchInfo instance, .Context.PostContext[0] provides access to the first post-context (after) line.

With your sample input, the above yields:

Line                                            LineNumber
----                                            ----------
Server 'ABC', Procedure 'proc_test1', Line 189:          1
Server 'ABC', Procedure 'proc_test2', Line 197:          4
Server 'ABC', Procedure 'proc_test2', Line 197:          7
Sign up to request clarification or add additional context in comments.

2 Comments

Thanks for your help @mklement0
Glad to hear it was helpful, @Kris.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.