簡體   English   中英

Powershell 將 export-csv 與 Windows Powershell ISE 中顯示的格式匹配

[英]Powershell match export-csv with format shown in Windows Powershell ISE

我有一個從事件日志中導出事件的腳本,例如:

Get-EventLog -LogName Application | Where-Object Source -Match "my application"  | export-csv "$FileDate.csv"

當我打開 CSV 時,它看起來也不像我想要的那樣。 表明

EventID,"MachineName","Data","Index","Category","CategoryNumber","EntryType","Message","Source","ReplacementStrings","InstanceId","TimeGenerated","TimeWritten", "UserName","Site","Container" 0,"DESKTOP-ID94AN3","System.Byte[]","21425","(0)","0","Error","完成執行項目' Testproject' 執行 Package 'Failure' 執行失敗 開始時間:21/12/2020 19:47:52 結束時間:21/12/2020 19:47:56 在服務器上:DESKTOP-ID94AN3 -Logmessage

在文本到列之后:

在此處輸入圖像描述

在不應該出現的列中包含信息。 它似乎結合了來自事件查看器的 2 windows。 “上” window 帶有錯誤,“下” window 帶有詳細信息。 我想要的只是上面的 window。

如果我在 Windows Powershell ISE 中運行此代碼:

Get-EventLog -LogName Application | Where-Object Source -Match "Myapplication"

它看起來像我想要的樣子(在使用文本到列之后):

   Index Time          EntryType   Source                 InstanceID Message                                                                                                                                                                                                                                 
   ----- ----          ---------   ------                 ---------- -------                                                                                                                                                                                                                                 
   21425 Dec 21 19:47  Error       Myapplication LogEn...            0 Message 1                                                                                                                                                                 
   21424 Dec 21 19:47  Information Myapplicationr LogEn...            0 Message 2

如何讓我的 CSV 導出也看起來像這樣?

在將結果發送到Export-CSV之前,您可以通過僅選擇您關心的特定列來使其看起來像您想要的方式。

Get-EventLog -LogName Application | Where-Object Source -Match "edgeupdate" | 
    Select -first 10 -Property Index, Time, EntryType, Source, InstanceId, Message | 
      Export-Csv -NoTypeInformation

在此處輸入圖像描述

暫無
暫無

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

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