簡體   English   中英

如何使用Powershell正確導出為CSV

[英]How to Properly Export to CSV Using 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
}

結果: 在此處輸入圖片說明

最后添加時,Export-CSV命令無法正常工作。 它輸出一組不同的數據。 在此處輸入圖片說明

格式化cmdlet(例如Format-Table不僅會更改對象的顯示方式,還會將對象本身更改為可以顯示所需內容的內容。 這就是為什么通常建議不要在腳本或函數中使用格式化cmdlet的原因。

相反,應該使用Select-Object cmdlet限制傳遞給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
}

嘗試這個

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
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM