powershellexport-to-csvget-eventlog

Total number of event logs then piped out to a CSV


I've got the below

Get-EventLog -LogName * -After (Get-Date).Adddays(-7)

Which returns the information I require:

Event log return

Now when I export this information out with

Export-Csv c:\temp\$([Environment]::MachineName).csv

It returns the following under the entries section in my CSV:

System.Diagnostics.EventLogEntryCollection 

I will be running this on multiple servers and collecting all logs into one area.

So Now I have changed the information to be a bit more specific

Get-EventLog -LogName * -After (Get-Date).Adddays(-7) |
  Select-Object MachineName, Log, Entries |
  Export-Csv -NoTypeInformation c:\temp\$([Environment]::MachineName).csv 

This now returns the below when I'm exporting out to a CSV:

New Export

But I can't see the amount of entries I require. Is there a work around for this?


Solution

  • The Entries property contains a collection. You need the Count of that collection. You may also want to replace the MachineName value (. for the local computer) with the actual computer name. Calculated properties are the usual way to achieve this:

    Get-EventLog -LogName * -After (Get-Date).Adddays(-7) |
      Select-Object @{n='MachineName';e={$env:COMPUTERNAME}}, Log,
                    @{n='Entries';e={$_.Entries.Count}} |
      Export-Csv "C:\temp\$env:COMPUTERNAME.csv" -NoType