目前提取特定事件查看器日志需要 15-20 秒,是否有更有效的方法来实现相同的最终结果?
我需要实例 ID 21 的最后 5 分钟的应用程序日志。
Start-Transcript -Path C:\Windows\Blah\Data\Logs\Temp\StatusErrors.TXT -Append -Force -ErrorAction SilentlyContinue
Get-EventLog -LogName application -After (Get-Date).AddMinutes(-5) -InstanceID 21 -Message "*device*" | Select-Object -ExpandProperty message
Stop-Transcript
最佳答案
我不会深入探讨它的逻辑,因为它已经产生了结果。 Get-Eventlog
有点过时了。使用 Get-WinEvent
,您可以在其中使用高级 XPath 和 XML 过滤器,并且日志将使用其索引非常快速地返回目标事件。
以下示例:
$filter = @{
LogName = 'application'
ID = 21
StartTime = (Get-Date).AddMinutes(-5)
}
#$Computer = "Hostname" ## In case you are running it remotely
Get-WinEvent -FilterHashTable $filter #-ComputerName $Computer (Commented out since it is when you run remotely)
希望有帮助。
关于Powershell - 提取事件查看器日志的有效方法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/74659675/