배치 스크립트 중 하나가 로그 파일을 생성하며 아래와 같은 오류 메시지가 나타날 수 있습니다.
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
매개 변수를 사용합니다 Select-String
.
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
캡처 된 0
라인 이전 및 1
라인 이후 실제로 일치하는 선을 따라.
결과 Microsoft.PowerShell.Commands.MatchInfo
인스턴스 .Context.PostContext[0]
에서 첫 번째 컨텍스트 이후 (뒤) 줄에 대한 액세스를 제공합니다.
샘플 입력으로 위의 결과는 다음과 같습니다.
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
이 기사는 인터넷에서 수집됩니다. 재 인쇄 할 때 출처를 알려주십시오.
침해가 발생한 경우 연락 주시기 바랍니다[email protected] 삭제
몇 마디 만하겠습니다