2011-12-29 48 views
21

在Windows事件查看器中手動編輯XML過濾器的查詢,我可以在其中找到數據的字符串完全匹配的事件:使用XPath開始 - 使用或含有功能來搜索Windows事件日誌

<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> 

事件日誌給我的錯誤:

The specified query is invalid

待辦事項我有語法錯誤?

回答

4

如果你不介意的兩次傳球,你總是可以使用PowerShell腳本,以作爲其                -where運營商支持-like-match數據重新進行篩選,和-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 

一個cmd啓動它(nv.cmd):

powershell.exe -executionpolicy bypass "& '.\nv.ps1'"