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","Finished executing project 'Testproject' Execution Package 'Failure' Execution failed Start Time : 21/12/2020 19:47:52 End Time : 21/12/2020 19:47:56 on server: DESKTOP-ID94AN3 -Logmessage

文本转列后:

在不应该出现的列中包含信息。它似乎结合了事件查看器中的 2 windows。 'upper' window 包含错误,'lower' 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