Powershell - 提取事件查看器日志的有效方法

标签 powershell performance event-viewer

目前提取特定事件查看器日志需要 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/

相关文章:

ASP.NET Core 2.0 EventLog 更改事件查看器中的应用程序名称

c# - 如何确定引用 Microsoft.VC90.DebugCRT 的内容?

powershell - 检查文件中的计算机是否在线

带 RunspacePool 的 C# PowerShell - 如何导入 Exchange Cmdlet,如 Get-Mailbox?

performance - Entity Framework 5 和 Oracle : Expression with closure affecting performance when querying against a non-unique indexed field

performance - Oracle 编号和 varchar 连接

Powershell ISE 控制台窗口命令行自动换行

需要 Powershell 脚本帮助

android - 多重通知--android

asp.net - IIS 7.0 给出 503 错误并停止应用程序池