使用XPath启动或包含搜索Windows事件日志的函数

时间:2011-12-29 17:36:16

标签: xml windows xpath event-log

通过在Windows事件查看器中手动编辑XML过滤器查询,我可以找到数据与字符串完全匹配的事件:

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

现在,我想进行部分匹配:

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

事件日志给出了错误:

  

指定的查询无效

我的语法有错吗?

3 个答案:

答案 0 :(得分:19)

Windows事件日志支持XPath 1.0的子集。它仅包含3个功能:positionBandtimediff

参考:http://msdn.microsoft.com/en-us/library/windows/desktop/dd996910(v=vs.85).aspx#limitations

答案 1 :(得分:6)

如果你不介意两次通过,你可以随时使用powershell脚本重新过滤数据,因为-where运算符支持-like-match和{{1 }}:

nv.ps1

-contains

启动它的cmd(nv.cmd):

$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

答案 2 :(得分:0)

快速强大的功能可在数据中搜索会话*。即使数据是数组,也应该可以。

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

如果不需要精度,则更容易匹配消息,数据字段通常出现在消息中。

get-winevent application | where message -match session