Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/jsf-2/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
使用powershell显示日志文件中的整个错误消息_Powershell - Fatal编程技术网

使用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
    行之后以及实际匹配的行

  • 在生成的实例中,
    .Context.PostContext[0]
    提供对第一行post上下文(后面)的访问

根据您的示例输入,上述结果如下:

行号
----                                            ----------
服务器“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
      }
    }
  }