我正在尝试在 Windows 事件日志中搜索事件数据包含字符串的任何内容TCP 提供程序,错误:0作为较长错误消息的一部分。为此,我创建了以下代码:
<QueryList>
<Query Id="0" Path="Application">
<Select Path="Application">*[System[Provider[@Name='MyDemo' or @Name='AnotherDemo'] and (Level=2 or Level=3)]][EventData[Data[contains(.,'TCP Provider, error: 0')]]]</Select>
</Query>
</QueryList>
然而,这被视为无效查询 - 我猜测 contains 语句无法识别(因为看起来这里使用了 XPath 语法的特殊版本。有人知道我尝试的操作是否可行/如何去做这个吗?
提前致谢,
JB
答案1
您始终可以使用 powershell 脚本并通过 powershell 的 where 函数传递 XML(支持 -contains -like -match):
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'"
答案2
经过进一步的调查,我在 Stack Overflow 上找到了对此问题的答案: https://stackoverflow.com/questions/8671194/using-xpath-starts-with-or-contains-functions-to-search-windows-event-logs
因此看起来不可能在事件日志中进行通配符搜索。
答案3
在 powershell 中:
get-winevent @{logname='application';providername='mydemo','anotherdemo';level=2,3} |
where { [xml]$xml = $_.toxml()
$xml.event.eventdata.data -match 'TCP Provider, error: 0' }
如果是整个字符串匹配,则可以简化为:
get-winevent @{logname='application';providername='mydemo','anotherdemo';level=2,3;
data='TCP Provider, error: 0'}