2016-12-15 8 views
1

VDI 환경의 로그온 모니터링 제품에서 PowerShell의 로그를 구문 분석하려고합니다. 이 로그 파일을 기록하며이 줄을 쓴다 :로그 파일에서 값 추출

2016-12-15T14:15:02.863 INFO (0908-0bd8) [LogonMonitor::LogSummary] Logon Time: 4.03 seconds 

내가 뭘하려고 오전 그냥 "4.03"문자열에서 구문 분석과 값의 배열에 저장입니다. 다음을 수행하여 로그 파일에서 전체 문자열을 선택할 수 있습니다.

$LogPath = "\\file-svr\Logs\" 

$strings = Select-String -path $LogPath\*.txt -pattern "[LogonMonitor::LogSummary] Logon Time:" -AllMatches -simplematch 

foreach ($string in $strings) { 
$found = $string -match '\d\.' 
if ($found) { 
    $time = $matches[1] 
    $array[$i] = $time 
    } 
$i++ 
} 

내가 할 수있는 더 좋은 방법이 있습니까?

답변

2

예, 캡처 그룹을 Select-String 패턴으로 사용하고 정보를 가져올 수 있습니다. 여기

한 줄 예 :

$array = Select-String -path $scripts.tmp -Pattern "\[LogonMonitor::LogSummary\] Logon Time:\s*([\d|.]+)" | ForEach-Object { $_.Matches.Groups[1].Value } 

대안, 더 이 읽을 수 :

$regex = "\[LogonMonitor::LogSummary\] Logon Time:\s*([\d|.]+)" 

$array = Select-String -path $scripts.tmp -Pattern $regex | 
    ForEach-Object { 
     $_.Matches.Groups[1].Value 
    } 
+1

굉장! 나는 이것을 overthinking했다. 정말 고맙습니다. 이것은 완벽하게 작동합니다. – Koecerion

+0

당신은 충분히 가깝습니다. 천만에요. –

0

당신은 ConvertFrom-문자열 정규식 또는 템플릿을 사용할 수 있습니다

#----------- Detailled example ------------------------------------------ 

#define temple example for define informations to extracts 
[email protected]" 
{[date]DEvent*:2016-12-15T14:15:02.863} {LevelEvent:INFO} {TypeEvent:(0908-0bd8)} {TypeMOnitor:[LogonMonitor::LogSummary]} Logon Time: {LogonTime:4.03 seconds} 
{[date]DEvent*:2017-12-15T14:15:02.863} {LevelEvent:FATAL} {TypeEvent:(090d-x01y)} {TypeMOnitor:[LogonMonitor::Log2Summary]} Logon Time: {LogonTime:123455.156 seconds} 
"@ 


#date example, you can replace by $date=gc "yourpathfilelog" 
[email protected]" 
2016-12-15T14:15:02.863 INFO (0908-0bd8) [LogonMonitor::LogSummary] Logon Time: 4.03 seconds 
1987-09-02T01:00:00.00 WARNING (101-0bd8) [LogonMonitor::LogxxxSummary] Logon Time: 1.00 minutes 
"@ 


#explode data 
$dataexploded=$datas | ConvertFrom-String -TemplateContent $template 

#note, you can the filter like you want 
$dataexploded | where {$_.LevelEvent -eq "INFO"} 



#----------- short example ------------------------------------------ 

[email protected]" 
{[date]DEvent*:2016-12-15T14:15:02.863} {LevelEvent:INFO} {TypeEvent:(0908-0bd8)} {TypeMOnitor:[LogonMonitor::LogSummary]} Logon Time: {LogonTime:4.03 seconds} 
{[date]DEvent*:2017-12-15T14:15:02.863} {LevelEvent:FATAL} {TypeEvent:(090d-x01y)} {TypeMOnitor:[LogonMonitor::Log2Summary]} Logon Time: {LogonTime:123455.156 seconds} 
"@ 

gc "c:\temp\myfile.log" | ConvertFrom-String -TemplateContent $template | where {$_.LevelEvent -eq "INFO"}