2017-07-29 18 views
0

このスクリプトをCSVに正しくエクスポートする方法を知っていますか?Powershellを使用してCSVに正しくエクスポートする方法

Try { 

    Invoke-Command -scriptblock {Get-EventLog System -After "7/8/2017" -Before "07/28/2017" | 
    Where-Object {$_.EventID -eq "50" -or $_.EventID -eq "51" -or $_.EventID -eq "55" -or $_.EventID -eq "57" -or $_.EventID -eq "6008"} | 
    FT -Property Machinename, TimeWritten, EntryType, Source, EventID, Message -AutoSize -wrap } -computername $computer -ErrorAction Stop 
} 
Catch { 
    Write-Host $Computer "Error/RDC Problem" -ForegroundColor Red 
} 

結果:最後に追加したとき enter image description here

輸出-CSVコマンドが正常に動作していません。異なるデータセットを出力します。 enter image description here

+0

あなたは、 "正常に動作していない" とはどういう意味ですかましたか?私はFormat-Csvがオブジェクトのすべてのプロパティを出力すると期待していますが、Format-Tableはしばしば画面に収まるサブセットを表示します。 Format-Csvは、画面の幅に制限されない形式になっているので、すべてを含めることができます。 –

答えて

2

Format-Tableのようなコマンドレットの書式設定は、オブジェクトの表示方法を変更するだけでなく、オブジェクト自体を目的の表示方法に変更します。これは、スクリプトまたは関数で書式設定コマンドレットを使用しないことが一般的に推奨される理由の一部です。

代わりにコマンドレットを使用して、Export-Csvに渡されるプロパティの数を制限する必要があります。

Invoke-Command -ComputerName $computer -ErrorAction Stop -ScriptBlock { 
    Get-EventLog System -After "7/8/2017" -Before "07/28/2017" | 
    Where-Object { 50, 51, 55, 57, 6008 -contains $_.EventID } | 
    Select-Object -Property MachineName, TimeWritten, EntryType, Source, EventID, Message 
} 
+1

それは働いた!ありがとうございました。スクリプトを短くするためのプラスの点は、私はあなたがそのようなWhere-Objectで作業できるかどうか分かりませんでした。 – KentMarionVG

1

この

Try { 

    Invoke-Command -scriptblock {Get-EventLog System -After "7/8/2017" -Before "07/28/2017" | 
    Where EventID -in ("50", "51", "55", "57", "6008") | 
    select Machinename, TimeWritten, EntryType, Source, EventID, Message } -computername $computer -ErrorAction Stop |export-csv "c:\temp\result.csv" 
} 
Catch { 
    Write-Host $Computer "Error/RDC Problem" -ForegroundColor Red 
} 

または単にこのようなこともしてみてください。

Try 
{ 
    Get-EventLog System -After "7/8/2017" -Before "07/28/2017" -ComputerName $computer | 
    Where EventID -in ("50", "51", "55", "57", "6008") | 
    select Machinename, TimeWritten, EntryType, Source, EventID, Message |export-csv "c:\temp\result.csv" -NoType 
} 
Catch 
{ 
    Write-Host $Computer "Error/RDC Problem" -ForegroundColor Red 
} 
+0

try-blockの中にExport-CSVコマンドを持ってきたとは思いませんでした。これありがとう! +1 – KentMarionVG

関連する問題