使用powershell显示日志文件中的整个错误消息
其中一个批处理脚本创建一个日志文件,它可能会有如下错误消息使用powershell显示日志文件中的整个错误消息,powershell,Powershell,其中一个批处理脚本创建一个日志文件,它可能会有如下错误消息 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: Inva
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'.
使用powershell脚本从日志文件读取错误并向邮件报告错误。我用的是这个,它也排除了某些警告信息
Select-String -Path 'C:\Users\BatchLog_20200911.txt' -Pattern "Msg","Error" | Select-String -Pattern "SQLState = S1000, NativeError = 0" -notmatch | select-object -Property Line,LineNumber
输出如下所示
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
所以,它只打印与查询匹配的行。我想打印下一行,其中包含实际的错误消息描述。这里有什么建议吗。谢谢。使用cmdlet的
-Context
参数:
捕获-Context 0,1
行之前和0
行之后以及实际匹配的行1
- 在生成的实例中,
提供对第一行post上下文(后面)的访问.Context.PostContext[0]
行号
---- ----------
服务器“ABC”,程序“proc_test1”,第189行:1
服务器“ABC”,程序“proc_test2”,第197行:4
服务器'ABC',程序'proc_test2',第197行:7
谢谢你的帮助@mklement0很高兴听到你的帮助@Kris。
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
}
}
}