2017-12-01 8 views
0

에 로그 맞춤 로그 리더를 작성. 데이터는 이렇게 보입니다 :내가 로그 리더의 생성을 시도하고 PowerShell을

2017-11-27 13 : 24 : 41,791 [8] 정보 CTSipEndpoint.CLogger.provider.gsiplib [(null)] - -00001 [정보] 정보 | 4744 | 등록 대화 상자 [1] 2-e : 5; t : 1-3 (dn : 85188)

2017-11-27 13 : 24 : 41,791 INFO CTSipEndpoint.CLogger.provider.gsiplib [(null)] - -00001 [정보] 정보 | 4744 | 지난 48 시간

  1. 반환에만 라인은 더 쿼리 : REGISTERdialog [1] 이벤트 2 REG/I는 다음을 수행하려고

    을 받아 들였다. 다음과 같은 문구가 포함 된 모든 행 반환 이상에서

  2. : "오류" "장치", "스피커를 식별 할 수 없습니다!", "존재하지 않는"지금까지 나는 단지이

을 "경고" 비효율적 인 방식으로 작동하도록 만들었습니다. 각 구문에 대해 파일에 대해 실행되고 배열이 추가됩니다. 불행히도 이는 날짜 시간이 비 순차적이라는 것을 의미합니다. 이제 스크립트의 끝에서 콘텐츠 개체를 순서대로 정렬하거나이 쿼리를 더 똑똑하게 실행하는 방법을 찾아야합니다. 여기 참조를 위해 내 스크립트입니다 필요가 조정하는 경우 여기

$logfile = "C:\users\test\desktop\programlogs.log" 
$content = "" 
cat $logfile | 
Select-String "ERROR" -SimpleMatch | 
    select -expand line | 
    foreach { 
       $_ -match '(.+)\s\[(ERROR)\]\s(.+)'| Out-Null 
       $error_time = [datetime]($matches[1]).split(",")[0] 
       if ($error_time -gt (Get-Date).AddDays(-2)) { 
       $content += $_ + "`n" 
       } 
      } 

cat $logfile | 
Select-String "device" -SimpleMatch | 
    select -expand line | 
    foreach { 
       $_ -match '(.+)\s\[(device)\]\s(.+)'| Out-Null 
       $error_time = [datetime]($matches[1]).split(",")[0] 
       if ($error_time -gt (Get-Date).AddDays(-2)) { 
       $content += $_ + "`n" 
       } 
      }   


cat $logfile | 
Select-String "does not exist" -SimpleMatch | 
    select -expand line | 
    foreach { 
       $_ -match '(.+)\s\[(does not exist)\]\s(.+)'| Out-Null 
       $error_time = [datetime]($matches[1]).split(",")[0] 
       if ($error_time -gt (Get-Date).AddDays(-2)) { 
       $content += $_ + "`n" 
       } 
      } 


cat $logfile | 
Select-String "Could not identify speaker!" -SimpleMatch | 
    select -expand line | 
    foreach { 
       $_ -match '(.+)\s\[(Could not identify speaker!)\]\s(.+)'| Out-Null 
       $error_time = [datetime]($matches[1]).split(",")[0] 
       if ($error_time -gt (Get-Date).AddDays(-2)) { 
       $content += $_ + "`n" 
       } 
      }   

cat $logfile | 
Select-String "Warn" -SimpleMatch | 
    select -expand line | 
    foreach { 
       $_ -match '(.+)\s\[(Warn)\]\s(.+)'| Out-Null 
       $error_time = [datetime]($matches[1]).split(",")[0] 
       if ($error_time -gt (Get-Date).AddDays(-2)) { 
       $content += $_ + "`n" 
       } 
      }     


      $content = $content | select -uniq 
      $file = "c:\temp\shortenedlog.txt" 
      $content| Add-Content -Path $file 

답변

0

짧은 테이크이야,이 도움이된다면 알려 나 :

[email protected]() 
$logfile = get-content C:\temp\programlogs.log 
$searchFor="error","device","does not exist","Could not identify speaker!","warn" 
foreach($line in $logfile){ 
    if($line.Length -gt 18){ 
     $datetime=date $line.substring(0,$line.indexof(",")) 
     if(((date)-$datetime).TotalHours -lt 49){ 
      $keep=$false 
      foreach($item in $searchFor){ 
       if($line.contains($item)){ $keep=$true } 
      } 
      if ($keep){$newlog+=$line} 
     } 
    } 
} 

$newlog | sort | % {add-content C:\temp\NewLog.log $_} 
+0

안녕, 불행하게도 난 그냥 위의 코드와 에러 코드를 얻을 : 예외 호출 "하위 문자열" "이"인수 (들) :. "길이가 제로보다 작은 매개 변수 이름이 될 수 없습니다 : 길이 "C에서 : 사용자 \ \ 사용자 이름 \ 바탕 화면 \ LogReader.ps1 : 5 문자 : 5 + $ 날짜 = 날짜 $의 line.substring (0, $의 line.indexof (", ")) + ~~~~~ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ + CategoryInfo : NotSpecified (:) [] MethodInvocationException + FullyQualifiedErrorId : ArgumentOutOfRangeException –

+0

죄송합니다. 로그에 빈 줄이 있어야합니다. 스크립트를 업데이트했습니다. –

+0

도움 주셔서 감사합니다. 파일이 방대하고 반복 할 때 너무 오랜 시간이 걸렸기 때문에 단순한 해결책을 찾지 못했습니다. –

0

내가 일에 공급 한 해결책을 온 전 관심이 있고, 검색 기준이 뒤 따른다. 이 솔루션을 신속하게 작동으로 사용했습니다.

#Set parameters. 
$File = "c:\temp\RefinedLogs.txt" 
$DateParam = (Get-Date).AddDays(-1).ToString('yyyy-MM-dd') 
$DateParam1 = (Get-Date).ToString('yyyy-MM-dd') 
$SearchForDate = @("$dateparam", "$dateparam1") 
[email protected]("error","device","does not exist","Could not identify speaker!","warn") 

#Filter file with dates set in $SearchForDate. 
$DateFiltered = Get-Content '.\MyAPP.log' | Select-String -Pattern $SearchForDate -SimpleMatch 


#Filter variable for phrases set in $SearchFor. 
$Content = $DateFiltered | Select-String -Pattern $SearchFor -SimpleMatch 

#Make results readable 
ForEach($line in $content){ 
    $Object = "$line" + "`n" 
    $FinalResult += $Object 
    } 

#Output results. 
write-host $FinalResult