1

ハッシュテーブルにエラーコードがあり、エラーログのディレクトリ全体で発生する可能性のある最大回数です。 ので気に入り:エラーログのエラー数と比較| Powershell(V2)|配列データ構造

Key Value 
err1 2 
err2 1 

このテーブルは、制御ファイル(XML)からの入力に基づいて動的に作成されるので、要素の#は変更することができます。

「err1」と「err2」を検索し、発生した回数を数えてからハッシュテーブルと比較することができます。

ので、私はこのようなものを持っている:

ForEach($file in $logs){ 
    ForEach($key in $hashTable.keys){ 
     If(Select-String $file -pattern $key){ 
      #get key value, increment a counter for this key value (error code) 
     } 
    } 
} 

#Psuedo for next step... 
<# 
ForEach($key in $hashTable.keys){ 
     If (CountFound > Key.Value) { 
    write-host $("Error: " + $key + " occurred too much." 
    } 
} 
#> 

は、容易かつ迅速に変更可能である変数/値のペアを格納で良いですPowerShellでのデータ構造はありますか?

キー値ごとに配列を作成し、ファイル内で一致するエラーコードが見つかるたびにその配列に要素を追加し、別の配列の長さを数えたいと思います。しかし、それは私が考えることができる最高のソリューションです。

答えて

0

外部ソースから読み込んだ各エラータイプを表すクラスを持つことができ、ロジックに基づいてエラーインスタンスを更新できます。あなたのデータを保持するためのより良い構造だけを求めたので、私はそれに焦点を当てました。既に持っているロジック。

$errorClass = @" 
public class Error 
{ 
    public Error(string errorCode, int maxOccurances) { 
     ErrorCode = errorCode; 
     MaxOccurances = maxOccurances; 
    } 

    public string ErrorCode; 
    public int MaxOccurances; 
    public int ActualOccurances; 
} 
"@ 

Add-Type -TypeDefinition $errorClass 

$error1 = New-Object Error("Err1", 2) # You get these values from xml 
$error1.ActualOccurances = 5 # Update this property based on your logic 

$error2 = New-Object Error("Err2", 1) 
$error2.ActualOccurances = 3 

$errArray = @($error1, $error2) 

foreach ($err in $errArray) { 
    if ($err.ActualOccurances -gt $err.MaxOccurances) { 
     write-host $("Error: '" + $err.ErrorCode + "' occurred too much.") 
    } 
} 

出力:

Error: 'Err1' occurred too much. 
Error: 'Err2' occurred too much. 
+0

はV2でサポートされているクラスはありますか? – Christopher

+0

この例では、 'Error'型はC#を使用して実装されています - 実行時に' Add-Type'がコンパイルします。そう、はい、それはPowerShell V2で動作します –

0

これはおそらく私の目標を達成する最も簡単な方法だと思います。

ForEach($key in $hashTable.keys){ 
$count = 0 
    ForEach($file in $logs){ 
     If(Select-String $file -pattern $key){ 
      $count++ 
     } 
    } 
    If($count -gt $hashTable.Get_Item($key){ 
     #Do something 
    } 
} 

このようにして、別のデータ構造を完全には避けます。