1

解析しようとしているログ ファイルがありますが、解決策を見つけることができませんでした。以下のログ ファイルの一部をコピーしました。3 行の各グループが 1 つのエラーです。

csrak17 remove int_rpsmkt
Could not perform the administration request
Error: HPDMG1064E   The group member was not found. (status 0x14c01428)
csrak17 add int_rpsops
Could not perform the administration request
Error: HPDMG1064E   The group member was not found. (status 0x14c01428)
csrak17 remove int_rpssales
Could not perform the administration request
Error: HPDMG1064E   The group member was not found. (status 0x14c01428)
csrak17 remove int_tpd
Could not perform the administration request
Error: HPDMG1064E   The group member was not found. (status 0x14c01428)
csrak17 remove int_trpit
Could not perform the administration request
Error: HPDMG1064E   The group member was not found. (status 0x14c01428)

ログ ファイルを配列に読み込んでいますが、問題なくエラー コードを含む行が返されます。まず、「エラー: HPDMG1064E」の行を見つけてから、2 行前に戻って「add」という単語が含まれているかどうかを確認します (2 番目のグループの例のように)。

別の配列で見つかった値に基づいて配列内の行を返す方法はありますか? もちろん、ファイルをループしてプロセスを繰り返します。

これが私がこれまでに持っているものですが、何らかの理由で出力ファイルに1行しか返されません。また、ご覧のとおり、現在のインデックスに基づいて別のインデックスを見つけようとするコードはありません。

$logs = Get-Content C:\Temp\test\ProdtamResults_HPDMG1064E_errors.doc
$Value = "HPDMG1064E"

foreach($log in $logs) {
    $i = 0
    while ($i -le $logs.length-1) {

        if ($logs[$i] -match $Value)
            {
                return $logs[$i] | out-file C:\Temp\test\results.txt
            }
    $i++
    }
}
4

2 に答える 2

3

これを試してみてください

$logs = Get-Content C:\Temp\test\ProdtamResults_HPDMG1064E_errors.doc
$Value = "HPDMG1064E"

gc $logs | select-string -Pattern $value -Context 3 | 
? { $_.context.precontext[1] -match '\sadd\s' } | select linenumber, line

psobject[] これは、必要な一致の行番号と行の値を含む a を返します。

于 2013-09-18T14:31:01.047 に答える
1

(i-2) 番目の行に「add」が含まれている場合、i 番目の行を出力します。

$file = @"
csrak17 remove int_rpsmkt
Could not perform the administration request
Error: HPDMG1064E   The group member was not found. (status 0x14c01428)
csrak17 add int_rpsops
Could not perform the administration request
Error: HPDMG1064E   The group member was not found. (status 0x14c01428)
csrak17 remove int_rpssales
Could not perform the administration request
Error: HPDMG1064E   The group member was not found. (status 0x14c01428)
csrak17 remove int_tpd
Could not perform the administration request
Error: HPDMG1064E   The group member was not found. (status 0x14c01428)
csrak17 remove int_trpit
Could not perform the administration request
Error: HPDMG1064E   The group member was not found. (status 0x14c01428)
"@;

$lines = $file -split "`n"

$Value = "HPDMG1064E";
for($i=0; $i -lt $lines.Count; $i++) {
  if($lines[$i] -match "HPDMG1064E") {
    if($lines[$i-2] -match "add") {
      Write-Host $lines[$i]; #or do something else
    }
  }
}

コードGet-Contentでは既に改行で分割されているため、それを行う必要はありません。

于 2013-09-18T14:30:16.023 に答える