Powershell 将 export-csv 与 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","时间写入”、“用户名”、“站点”、“容器” 0,"DESKTOP-ID94AN3","System.Byte[]","21425","(0)","0","错误","完成 执行项目 'Testproject' 执行包 'Failure' 执行 失败开始时间:21/12/2020 19:47:52 结束时间:21/12/2020 19:47:56 在服务器上:DESKTOP-ID94AN3 -日志消息

文本到列之后:

enter image description here

在不应该出现的列中包含信息。它似乎结合了事件查看器中的 2 个窗口。带有错误的“上”窗口和带有详细信息的“下”窗口。我想要的只是上面的窗口。

如果我在 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

enter image description here

相关问答

Selenium Web驱动程序和Java。元素在(x,y)点处不可单击。其...
Python-如何使用点“。” 访问字典成员?
Java 字符串是不可变的。到底是什么意思?
Java中的“ final”关键字如何工作?(我仍然可以修改对象。...
“loop:”在Java代码中。这是什么,为什么要编译?
java.lang.ClassNotFoundException:sun.jdbc.odbc.JdbcOdbc...