The following code works to return the Windows Log events with ID = 100.
$Date = (Get-Date).AddDays(-30)
Get-WinEvent -FilterHashTable @{ LogName = "Microsoft-Windows-Diagnostics-Performance/Operational"; StartTime = $Date; ID = 100 } -MaxEvents 1 | Select-Object -Property TimeCreated, Id, Task, TaskDisplayName, LevelDisplayName, Message
This code returns an error for the TaskDisplayName = 'Boot Performance Monitoring'
$Date = (Get-Date).AddDays(-30)
Get-WinEvent -FilterHashTable @{ LogName = "Microsoft-Windows-Diagnostics-Performance/Operational"; StartTime = $Date; TaskDisplayName = 'Boot Performance Monitoring' } | Select-Object -Property TimeCreated, Id, Task, TaskDisplayName, LevelDisplayName, Message
Get-WinEvent : No events were found that match the specified selection criteria. At D:\tfsws\TG-Dev-ICSG2\Support\PowerShell Scripts\Get-WinEvent-TEST.ps1:6 char:1 + Get-WinEvent -FilterHashTable @{ LogName = "Microsoft-Windows-Diagnos ... + ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ + CategoryInfo : ObjectNotFound: (:) [Get-WinEvent], Exception + FullyQualifiedErrorId : NoMatchingEventsFound,Microsoft.PowerShell.Commands.GetWinEventCommand
How do I make Get-WinEvent accept the TaskDisplayName filter?
Unfortunately you can't use
-FilterHashTable
to filter on TaskDisplayName for 2 reasons:In the Microsoft docs Get-WinEvent valid FilterHashTable values are:
LogName=<String[]>
ProviderName=<String[]>
Path=<String[]>
Keywords=<Long[]>
ID=<Int32[]>
Level=<Int32[]>
StartTime=<DateTime>
EndTime=<DataTime>
UserID=<SID>
Data=<String[]>
TaskDisplayName isn't one of the
-FilterHashTable
options.... ok. So next option is to use-FilterXPath
or-FilterXML
which gives us access to some more lower level filtering. For simplicity, I will use-FilterXPath
. In order to find the right keys to filter on, you have to go to the details tab on the event. Here is a sample event:When you expand it out, you notice that there is no
TaskDisplayName
. This is becauseTaskDisplayName == Task Category
. Ok... let's look forTask Category
... Well there is noTask Category
either. That's because Categories are actually stored numerically in the event, and then mapped into a proper description using an Event Category String. That is why you can't filter based on aTaskDisplayName
orTask Category
. Instead you will have to filter on theTask
number, which you in this case is4002
. And if you use theStartDate
, which isTimeCreated
, you can calculate that 30 days is 2592000000 miliseconds, then your code becomes: