Dan Metheus
Dan Metheus

Reputation: 1438

Showing LogName in table when querying multiple logs with PowerShell get-eventlog

I'm new to PowerShell and I am trying to get recent events for a group of logs (right now application and system for testing purposes).
What's missing is a column in the final output showing the log the event came from.
I tried piping the output before the format-table to get member to see if there was a property that contained the logname and there doesn't seem to be one.
Is there a workaround to get this?

What I have right now is:

"application", "system" | foreach { Get-EventLog -LogName $_ -Newest 10 } | sort time -Descending | ft timegenerated, LogName, source, message -wrap -AutoSize

This is what I want, but the LogName column is blank because there's no such property.

Upvotes: 1

Views: 1603

Answers (1)

Frode F.
Frode F.

Reputation: 54871

Someone may be able to clean this up a little, but you could try this:

"application", "system" | foreach { $events = Get-EventLog -LogName $_ -Newest 10; $events | Add-Member -MemberType NoteProperty -Name LogName -Value (Get-Culture).TextInfo.ToTitleCase($_); $events } | sort timegenerated -Descending | ft timegenerated, LogName, source, message -wrap -AutoSize

I fixed the sort command(typo) and format the logname as proper(uppercase first letter) to clean it up.

Upvotes: 1

Related Questions