function Trace-WinEvent {
param(
[Parameter(Mandatory)] [string] $LogName
)
$sourceId = 'TraceWinEvent' # self-chosen
try {
$query = [System.Diagnostics.Eventing.Reader.EventLogQuery]::new($LogName, 'LogName')
$watcher = [System.Diagnostics.Eventing.Reader.EventLogWatcher]::new($query)
$watcher.Enabled = $true
Write-Verbose "Registering for EventLogWatcher events for the '$LogName' log..."
Register-ObjectEvent -InputObject $watcher -SourceIdentifier $sourceId -EventName EventRecordWritten
Write-Verbose 'Waiting for such events indefinitely; use Ctrl+C to stop...'
while ($true) {
$evt = Wait-Event -SourceIdentifier $sourceId
$evt | Remove-Event
# Extract the event record.
$evtRec = $evt.SourceEventArgs.EventRecord
# Add a .Message NoteProperty member and output the decorated record.
$evtRec | Add-Member -PassThru Message $evtRec.FormatDescription()
}
}
finally {
Unregister-Event $sourceId
}
}
调用它,例如如下:
# Trace (monitor) the Application event log for newly created records
# and also capture them in variable $records.
# -Verbose provides verbose feedback.
# Use Ctrl-C to stop.
Trace-WinEvent Application -OutVariable records -Verbose
stackprotector 答案中的
[System.Diagnostics.Eventing.Reader.EventLogWatcher]
基于解决方案[ *]很有希望,但由于需要必须单独读取的临时文件,因此有些麻烦。可以使用以下函数来避免这种情况
Trace-WinEvent
,该函数将任何新添加的事件记录直接流式传输到管道:调用它,例如如下:
示例输出:
笔记:
流式传输到管道不仅可以产生即时控制台输出,而且还允许以编程方式处理
[System.Diagnostics.Eventing.Reader.EventLogRecord]
管道中的记录(即实例)。要停止跟踪(监控),请按Ctrl+C。
实施基于:
无需脚本块即可调用,这会使 PowerShell 将事件排队以供按需检索。
Register-ObjectEvent
-Action
这种按需检索是在无限循环中执行的,该循环
Wait-Event
以阻塞方式调用一次检索一个待处理事件。由于 PowerShell 的实例显示格式
EventLogRecord
依赖于.Message
ETS 属性(类型为NoteProperty
,它会自动修饰其输出对象)的存在,因此上述代码通过方法手动修饰从事件对象中提取的每个实例,并将修饰后的实例输出到管道。Get-WinEvent
.FormatDescription()
Get-WinEventTail
[*]最好避免使用另一个解决方案,即函数,因为它可能会因递归调用而耗尽堆栈空间,并且它每次都会无条件地输出最后 N 个条目,这会产生许多重复。相当于。但这没有
tail -f
帮助Get-Content -Wait
,因为您仍然需要将事件保存在文本文件中。Windows 将其事件存储在 的.evtx
文件中C:\Windows\System32\winevt\Logs
,并且这些文件是二进制文件。作为一种解决方法,您可以定期打印最后的 x 个事件:
一种更复杂的方法是使用 EventLogWatcher 将传入事件写入文本文件,然后跟踪该文件: