如何显示包含来自 powershell 的特定文本的事件日志

Jon*_*Jon 7 windows powershell event-log

我正在尝试快速显示窗口事件日志中最后 ~ 天的所有事件,其中包含 Power Shell 中的某个字符串。

我找到了用于列出事件的 powershell 命令,但我基本上想为特定文本“GREP”它们。

我需要使用 powershell,因为目标是 Windows Server 2016 Hyper-V,但我认为能够在任何带有 powershell 的机器上快速搜索最近的事件也非常有用。

为了显示可用的日志,我运行:

PS C:\Users\Administrator> Get-EventLog -List

  Max(K) Retain OverflowAction        Entries Log
  ------ ------ --------------        ------- ---
  20,480      0 OverwriteAsNeeded       1,113 Application
  20,480      0 OverwriteAsNeeded           0 HardwareEvents
     512      7 OverwriteOlder              0 Internet Explorer
  20,480      0 OverwriteAsNeeded           0 Key Management Service
     512      7 OverwriteOlder          1,539 Microsoft-ServerManagementExperience
  20,480      0 OverwriteAsNeeded      28,667 Security
  20,480      0 OverwriteAsNeeded       4,857 System
  15,360      0 OverwriteAsNeeded       3,654 Windows PowerShell
Run Code Online (Sandbox Code Playgroud)

在这个例子中,我的目标日志被称为应用程序

我可以使用以下命令将过去 24 小时的日志打印到控制台:

Get-EventLog -LogName system -after (Get-Date).AddDays(-1)
Run Code Online (Sandbox Code Playgroud)

我尝试使用过滤输出,Select-String但从未匹配任何行。

Jon*_*Jon 5

这就是我最终做的。它搜索文本的几个事件属性的值并将它们显示在控制台上:

$search = "hyper"
Get-EventLog -LogName system -after (Get-Date).AddDays(-1) | Where-Object { $_.Category.ToLower().Contains($search.ToLower()) -or $_.Message.ToLower().Contains($search.ToLower()) -or $_.Source.ToLower().Contains($search.ToLower())} | Format-Table -AutoSize -Wrap
Run Code Online (Sandbox Code Playgroud)

示例输出:

   Index Time          EntryType   Source                 InstanceID Message
   ----- ----          ---------   ------                 ---------- -------
    4751 Aug 10 09:13  Information Microsoft-Windows...           23 NIC /DEVICE/{FD82EC81-DC0D-4655-B606-0AA9AF08E6CC} (Friendly Name: Microsoft Hyper-V Network Adapter) is now operational.
    4750 Aug 10 09:13  Information Microsoft-Windows...           11 The description for Event ID '11' in Source 'Microsoft-Windows-Hyper-V-Netvsc' cannot be found.  The local computer may not have the necessary registr...
    4749 Aug 10 09:13  Information Microsoft-Windows...           24 NIC /DEVICE/{FD82EC81-DC0D-4655-B606-0AA9AF08E6CC} (Friendly Name: Microsoft Hyper-V Network Adapter) is no longer operational.
Run Code Online (Sandbox Code Playgroud)

我是 powershell 的新手,所以它可能不是最好的方法,但它有效。我希望它会为别人节省一些时间。