我如何使用PowerShell来尾巴特定的Windows事件日志? 可能吗?
我有时做了这个:
$idx = (get-eventlog -LogName System -Newest 1).Index while ($true) { start-sleep -Seconds 1 $idx2 = (Get-EventLog -LogName System -newest 1).index get-eventlog -logname system -newest ($idx2 - $idx) | sort index $idx = $idx2 }
每个MSDN文档:
Get-WinEvent
旨在替换运行Windows Vista和更高版本Windows的计算机上的Get-EventLog
cmdlet。Get-EventLog
仅在经典事件日志中获取事件。 为了向后兼容,Get-EventLog
保留在Windows PowerShell中。
并由我自己的需要,以促使尾巴非经典事件日志(这将是一个事件日志nouveau偶然?)这里是@mjolinor奇妙简洁的代码repurposed使用Get-WinEvent
:
Set-PSDebug -Strict function Get-WinEventTail($LogName, $ShowExisting=10) { if ($ShowExisting -gt 0) { $data = Get-WinEvent -provider $LogName -max $ShowExisting $data | sort RecordId $idx = $data[0].RecordId } else { $idx = (Get-WinEvent -provider $LogName -max 1).RecordId } while ($true) { start-sleep -Seconds 1 $idx2 = (Get-WinEvent -provider $LogName -max 1).RecordId if ($idx2 -gt $idx) { Get-WinEvent -provider $LogName -max ($idx2 - $idx) | sort RecordId } $idx = $idx2 # Any key to terminate; does NOT work in PowerShell ISE! if ($Host.UI.RawUI.KeyAvailable) { return; } } }
为了方便,我添加了一些花里胡哨的东西:
ShowExisting
参数将其调整为任意数字。 Get-WinEvent
的缺省值相反)。 我在上面的答案中修正了一些错误…
Set-PSDebug -Strict function Get-WinEventTail($LogName, $ShowExisting=50) { if ($ShowExisting -gt 0) { $data = Get-WinEvent -provider $LogName -max $ShowExisting $data | sort RecordId $idx = $data[0].RecordId } else { $idx = (Get-WinEvent -provider $LogName -max 1).RecordId } while ($true) { start-sleep -Seconds 1 $idx2 = (Get-WinEvent -provider $LogName -max 1).RecordId if ($idx2 -gt $idx) { $data = Get-WinEvent -provider $LogName -max ($idx2 - $idx + 1000) | where {$_.RecordId -gt $idx} | sort RecordId $data $idx = $data[-1].RecordId } # Any key to terminate; does NOT work in PowerShell ISE! #if ($Host.UI.RawUI.KeyAvailable) { return; } } }