Using XPath starts-with or contains functions to search Windows event logs
Asked Answered
A

3

33

By editing the XML filter query manually in Windows event viewer, I can find events where the data matches a string exactly:

<QueryList>
  <Query Id="0" Path="Application">
    <Select Path="Application">*[EventData[Data and (Data="Session end: imzcjflrrsq1sfdk3okc4jpf")]]</Select>
  </Query>
</QueryList>

Now, I want to do a partial match:

<QueryList>
  <Query Id="0" Path="Application">
    <Select Path="Application">*[EventData[Data and (Data[starts-with(.,"Session")])]]</Select>
  </Query>
</QueryList>

Event log gives me the error:

The specified query is invalid

Do I have the syntax wrong?

Asternal answered 29/12, 2011 at 17:36 Comment(0)
S
31

Windows Event Log supports a subset of XPath 1.0. It has only three functions: position, Band, timediff.

Reference: https://learn.microsoft.com/en-us/windows/desktop/WES/consuming-events#xpath-10-limitations

Septime answered 29/12, 2011 at 18:4 Comment(1)
really sad... so much limitationCorral
U
8

If you don't mind two passes, you can always use a powershell script to re-filter the data as its          -where operator supports -like, -match, and -contains:

nv.ps1

$Query = @"
  <QueryList>
    <Query Id="0" Path="System">
      <Select Path="System">
        *[System[(EventID=20001)]]
      </Select>
    </Query>
  </QueryList>
"@

$events = Get-WinEvent -FilterXml $Query
ForEach ($Event in $Events) {
    # Convert the event to XML
    $eventXML = [xml]$Event.ToXml()
    Add-Member -InputObject $Event -MemberType NoteProperty -Force -Name  DriverVersion -Value $eventXML.Event.UserData.InstallDeviceID.DriverVersion
    Add-Member -InputObject $Event -MemberType NoteProperty -Force -Name  DriverDescription -Value $eventXML.Event.UserData.InstallDeviceID.DriverDescription
    Add-Member -InputObject $Event -MemberType NoteProperty -Force -Name  Data -Value $eventXML.Event.EventData.Data
}
$Events | Select TimeCreated, Id, DriverDescription, DriverVersion, ProviderName, @{Name="MessageData";Expression={$_.Message + $_.Data}} | Where {$_.DriverDescription -match "NVIDIA GeForce GTX*"} | Out-GridView
pause

A cmd to launch it (nv.cmd):

powershell.exe -executionpolicy bypass "& '.\nv.ps1'"
Uncommon answered 16/10, 2015 at 18:5 Comment(0)
I
5

A quick powershell to search for session* in data. Even if data were an array, this should work.

get-winevent application | where { $xml = [xml]$_.toxml() 
  $xml.event.eventdata.data -like 'session*' } | select -first 3


   ProviderName: Microsoft-Windows-Winlogon

TimeCreated                     Id LevelDisplayName Message
-----------                     -- ---------------- -------
2/22/2020 11:05:30 AM         6000 Information      The winlogon notification subscriber <SessionEnv> was unavailable to handle a notification event.
2/22/2020 11:05:30 AM         6003 Information      The winlogon notification subscriber <SessionEnv> was unavailable to handle a critical notification event.
2/21/2020 6:28:38 PM          6000 Information      The winlogon notification subscriber <SessionEnv> was unavailable to handle a notification event.


$xml.event.eventdata.data # the last one

SessionEnv

If you don't need the precision, it's easier to match on the message, which the data fields often appear in.

get-winevent application | where message -match session
Ignazio answered 22/2, 2020 at 16:44 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.