0

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

After text-to-columns:

enter image description here

With information in columns where it should not be. It seems to combine the 2 windows from the event viewer. The 'upper' window with the errors and the 'lower' window with the details. All I want is the upper window.

If I run this code in 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?

5
  • what you show is NOT csv format ... it is the powershell display system showing the collection of objects that came from a CSV import. the actual file is just text divided by a delimiter character. Commented Dec 21, 2020 at 19:16
  • Edited for clarification. Commented Dec 21, 2020 at 19:29
  • you say It looks how I want it too look: and show what powershell shows after you import a CSV ... but that is not a CSV - it is a powershell array created by importing the CSV. ///// you CANNOT make a CSV that looks like that ... it would not be a CSV! [grin] it would only be a plain text file with almost zero structure. Commented Dec 21, 2020 at 19:42
  • Ok, to rephrase, that's how i want it to look after i use text-to-columns on my CSV. And it doesn't. Everything under 'Execution failed' (see screenshot) should not be in the same column as EventID. Commented Dec 21, 2020 at 19:53
  • kool! now, PLEASE change your Question to say what you actually want. it still refers to wanting a CSV file to look like a plain text report. Commented Dec 22, 2020 at 13:28

1 Answer 1

1

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.

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

Sign up to request clarification or add additional context in comments.

6 Comments

That works, except for the Message. It seems to put it under the first column as seen in my screenshot.
@Elmer Try it with -UseCulture switch appended to the Export-Csv cmdlet. I have the feeling your Excel uses a different delimiter character than the default comma.
Does your application have a lot of comma characters in your Message field? Maybe that's related
@Elmer did you double-click the file or imported into excel and then did text-to-columns? message is probably a multiline string. text-to-columns probably messes up when it sees newlines. Simply double-click, but do use -UseCulture otherwise excel puts it all into column 1
@Elmer If you export using the delimiter character Excel expects (by adding `-UseCulture``) you don't need to perform Text-To_Columns. That is the part that messes up your Excel sheet.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.