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

恐ろしく!私はこれを考えすぎた。どうもありがとうございます。これは完全に動作します – 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"} 
関連する問題