2016-10-30 26 views
1

スクリプトの入力として変数の出力を使用する際に問題があります。私はこの実行するとPowershell Get-Eventlogオブジェクト出力の問題

は:

$listoflogs = Get-EventLog -List | Select "Log" 


$listoflogss | % { Get-EventLog -LogName $_ | Where-Object {$_.EntryType -Match "Error"} } 

を私はそれが入力としてオブジェクトとの契約をdoesntのことを理解し、次のエラーが発生し、得ました。

Get-EventLog : The event log '@{Log=System}' on computer '.' does not exist. At line:5 char:31 
+ $listoflogs | % { Get-EventLog <<<< -LogName $_ | Where-Object {$_.EntryType -Match "Error"} } 
    + CategoryInfo   : NotSpecified: (:) [Get-EventLog], InvalidOperationException 
    + FullyQualifiedErrorId : System.InvalidOperationException,Microsoft.PowerShell.Commands.GetEventLogCommand 
Get-EventLog : The event log '@{Log=ThinPrint Diagnostics}' on computer '.' does not exist. At line:5 char:31 
+ $listoflogs | % { Get-EventLog <<<< -LogName $_ | Where-Object {$_.EntryType -Match "Error"} } 
    + CategoryInfo   : NotSpecified: (:) [Get-EventLog], InvalidOperationException 
    + FullyQualifiedErrorId : System.InvalidOperationException,Microsoft.PowerShell.Commands.GetEventLogCommand 
Get-EventLog : The event log '@{Log=Windows PowerShell}' on computer '.' does not exist. At line:5 char:31 
+ $listoflogs | % { Get-EventLog <<<< -LogName $_ | Where-Object {$_.EntryType -Match "Error"} } 
    + CategoryInfo   : NotSpecified: (:) [Get-EventLog], InvalidOperationException 
    + FullyQualifiedErrorId : System.InvalidOperationException,Microsoft.PowerShell.Commands.GetEventLogCommand 

次に、オブジェクトを文字列に変換するスクリプトを変更しましたが、依然として私が望む出力を得ることができません。

$listoflogs = Get-EventLog -List | Select "Log" 

$listoflogss = ($listoflogs | Out-String) 

$listoflogss | % { Get-EventLog -LogName $_ | Where-Object {$_.EntryType -Match "Error"} } 

Get-EventLog : Event log names must consist of printable characters and cannot contain \, *, ?, or space s At line:7 char:32 
+ $listoflogss | % { Get-EventLog <<<< -LogName $_ | Where-Object {$_.EntryType -Match "Error"} } 
    + CategoryInfo   : NotSpecified: (:) [Get-EventLog], ArgumentException 
    + FullyQualifiedErrorId : System.ArgumentException,Microsoft.PowerShell.Commands.GetEventLogCommand 

私は間違っていることを知っていませんが、この作業を達成するのが良いですか?それでも、オブジェクト出力をスクリプトの読み込み可能な文字列に変換するこの概念を理解することはできません。

+1

こんにちは、 '$ listoflogs =のGet-EventLogに - リストを試す|:

はまた、このようなエラーを抑制している可能性がSelect-Object -ExpandProperty Log'を選択します。 – sodawillow

答えて

0

のようなものを試してみてください、私はあなたが達成したいものを理解思う:

Get-EventLog -List | ForEach-Object { 

    $logName = $_.Log 

    Get-EventLog $logName | 
     Where-Object { $_.EntryType -Match "Error" } | 
     Select-Object *, @{ n = "LogName"; e = { $logName } } 
} 

Select-Objectコマンドレットを使用するには、その場でプロパティを作成することができますそれを表示する(表示された結果にログ名を追加するためにここに必要)。

*はすべて本来の特性を意味し、nenameexpressionための速記であり、私たちが望む追加のプロパティを記述します。これはcalculated propertyと呼ばれます。

元のコードでは、Logプロパティの後ろに指定しなくても、オブジェクトのコレクションをGet-EventLogにパイプしていたため、-ExpandPropertyが役に立ちました。しかし、Selectプロパティを設定すると、収集しているオブジェクトから他のオブジェクトをすべて削除することもできます。

$listoflogss | 
    ForEach-Object { 
     Get-EventLog -LogName $_.Log | 
     Where-Object { $_.EntryType -Match "Error" } 
    } 
+0

sodawillow:あなたは、天才です。これはexectly私が探していたが、上に、あなたのソリューションは、すべてのプロパティを提供するか、または私が選ぶことができ、私が見たいものを選択するfelxibiltyを提供します。ですから、これについても、それぞれのビットが何をしているのかを説明してくれてありがとう。 ちょうど最後のことは、私はウェブサイトに私を指すことができますか、または私にこれを説明することができ、私は非常にうれしいです。 次のビットが何をしているか教えてください。 @ {n = "LogName"; e = {$ logName}} ありがとう – metho

-1

テストのビットの後、この

Get-EventLog -list | where {$_.Entries.Count -gt 0} | %{ $_.Entries } | where EntryType -EQ "Error" 
+0

-Expandpropertyは動作しているようですが、出力はどのエラーがどのログに属するのかを教えてくれません。たとえば、LogName:Application、EntryType、Timeなどの出力は残念ながら、違うログに属するエラーの大きなリストです。 Esperento57:申し訳ありませんが、それは何も生産していない、私はまだ把握しようとしています、どのように大文字小文字のために働くか。 $ _Entries.Count ???おそらく私は何かを紛失しています – metho

+0

'Get-EventLog -List'は' Log'をリストしていますので、イベントを取得するには 'Log'ごとに' Get-EventLog'を再度実行する必要があります。 – sodawillow

+0

"$ _。Entries.Count -gt 0"をテストしないと、カテゴリにエントリがないとエラーになります。のログ。私は自分のコードを変更しました – Esperento57