用于过滤 Windows 事件日志的 Powershell 脚本

Powershell script to filter Windows Event Logs

我正在尝试编写一个脚本来过滤 Window 的事件日志。我只想提取在异常消息行中具有特定短语的事件。

我已经尝试了这个脚本的几个迭代,我试图查询 "Exception Message:" 后面的文本,以便我可以过滤短语 "Unable to establish a connection to the database"

这是我失败的脚本的样子:

Get-EventLog -LogName Application |
    Select-Object -Expand Message |
    Select-String -Pattern '(?<=Exception message:\s+)\d+' |
    Select-Object -Expand Matches |
    Select-Object -ExpandProperty value |
    where -filterscript {$_.Message -contains 'Unable to establish a connection to the database'}

这就是我要过滤的事件日志消息的样子:

异常信息:

异常类型:异常

异常消息:无法建立与数据库的连接。可能挂了

不需要任何额外的过滤。 Get-EventLog自己可以做到:

Get-EventLog -LogName Application -Message "*Unable to establish a connection to the database*"