简体   繁体   English

Powershell 将 export-csv 与 Windows Powershell ISE 中显示的格式匹配

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

I have a script that exports evens from the eventlog like:我有一个从事件日志中导出事件的脚本,例如:

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

When I open the CSV it does not look how I want it too.当我打开 CSV 时,它看起来也不像我想要的那样。 It shows表明

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 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

After text-to-columns:在文本到列之后:

在此处输入图像描述

With information in columns where it should not be.在不应该出现的列中包含信息。 It seems to combine the 2 windows from the event viewer.它似乎结合了来自事件查看器的 2 windows。 The 'upper' window with the errors and the 'lower' window with the details. “上” window 带有错误,“下” window 带有详细信息。 All I want is the upper window.我想要的只是上面的 window。

If I run this code in Windows Powershell ISE:如果我在 Windows Powershell ISE 中运行此代码:

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

It looks how I want it too look (after using text to columns):它看起来像我想要的样子(在使用文本到列之后):

   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

How do I get my CSV export to look like this also?如何让我的 CSV 导出也看起来像这样?

You can make it look the way you want by selecting only the specific columns you care about, before sending the results over to Export-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