gpt4 book ai didi

xpath - NT事件日志XPath查询

转载 作者:行者123 更新时间:2023-12-03 16:29:22 28 4
gpt4 key购买 nike

我有一个现有的脚本,可以这样说:

set cimv2=getobject("winmgmts:root\cimv2")
set evcol=cimv2.execquery("select * from win32_ntlogevent where logfile='System' and (sourcename='Microsoft-Windows-Kernel-General' or sourcename='Disk')")

for each evt in evcol
wscript.echo evt.timewritten & ": " & evt.sourcename & ", " & evt.type & ", " & evt.eventcode & ", " & evt.message
next


有没有一种方法可以使用XPath查询而不是WMI select查询来查询Windows事件日志?

例如:

*[System[Provider[@Name='Microsoft-Windows-Disk' or @Name='Microsoft-Windows-Kernel-General']]]


编辑:我仍然想将VBscript集合作为一个对象,而不仅仅是执行“ wevtutil”。

最佳答案

PowerShell Get-WinEvent cmdlet具有一个-FilterXPath参数,您可以向其传递XPath表达式:

$xpath = "*[System[Provider[@Name='Microsoft-Windows-Disk' or @Name='Microsoft-Windows-Kernel-General']]]"
Get-WinEvent -LogName 'Security' -FilterXPath $xpath


在VBScript中,您需要脱壳到 wevutil,然后将XML数据加载到 DOMDocument对象中:

Function qq(s) : qq = """" & s & """" : End Function

xpath = "*[System[Provider[@Name='Microsoft-Windows-Disk' or @Name='Microsoft-Windows-Kernel-General']]]"
datafile = "C:\temp.xml"

Set sh = CreateObject("WScript.Shell")
Set fso = CreateObject("Scripting.FileSystemObject")

Set evt = sh.Exec("cmd /c wevtutil qe Security /q:" & qq(xpath) & " > " & qq(datafile))

While evt.Status = 0 : WScript.Sleep 100 : Wend

Set xml = CreateObject("Msxml2.DOMDocument.6.0")
xml.async = False
xml.loadXML "<events>" & fso.OpenTextFile(datafile).ReadAll & "</events>"

If xml.parseError <> 0 Then
WScript.Echo xml.parseError.reason
WScript.Quit 1
End If


有关通过XPath表达式过滤事件日志的更多信息,请参见 here

关于xpath - NT事件日志XPath查询,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31605145/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com