Author Posts

June 8, 2013 at 7:37 am

The function below Works fine in PowerShell 3.0 but only displays the Count in Powershell 2.0.

Function Get-OAEventLog {

param([Parameter(Mandatory=$True)]$LogName)

Get-EventLog -LogName $LogName -EntryType Error,Warning |
Select-Object EventID,TimeGenerated,Message |
Group-Object EventID |
Select-Object Count,`
@{Name='Between';Expression={$_.Group.TimeGenerated[0],$_.Group.TimeGenerated[($_.Group.TimeGenerated.Count – 1)]}},`
@{Name='EventID';Expression={$_.Group.EventID[0]}},`
@{Name='Message';Expression={IF($_.Group.Message.Count -gt 1){ $_.Group.Message[0]} Else {$_.Group.Message }}}
}

Get-OAEventLog -LogName Application

June 8, 2013 at 11:59 am

You're right. Was there a question?

If you're wondering why that function doesn't work the same way in PowerShell 2.0, it's because you're referencing properties on the objects that are part of the group (e.g. $_.Group.TimeGenerated, $_.Group.EventId, $_.Group.Message). PowerShell 3.0 allows you to directly reference properties on objects in a collection, but PowerShell 2.0 did not have that capability. To make this work in PowerShell 2.0, you'll have to replace each of these with something that does work in the older version.

For example, this:

$_.Group.TimeGenerated

would have to be replaced with this:

@($_.Group | Select-Object -ExpandProperty TimeGenerated)

Since you're using it multiple times inside of one expression, you probably only want to do that pipeline once, store it in a variable, and then use that variable in the remainder of the expression.

Similar changes are required for $_.Group.EventID and $_.Group.Message.

June 8, 2013 at 1:12 pm

That is an amazing reply and sorry for not actually posting a question. The things you do when you are in a hurry! Thanks again this really helps.

June 11, 2013 at 2:33 am

Is there a way for me to mark your answer as correct? or mark this as solved or shall I just change the title?

June 11, 2013 at 3:57 am

This forum software doesn't support marking posts as solved or answers as correct, so you can just change the title if you want to make sure others see it is resolved. We archive posts regularly though, so you can also just leave it as is and it will get archived later.