【问题标题】:Show Type of Event in PowerShell在 PowerShell 中显示事件类型
【发布时间】:2021-01-15 14:35:29
【问题描述】:

我需要创建一个脚本,其中将要求显示一种类型的事件(例如:系统)。该脚本将在屏幕上显示按事件 ID 分组的所选类型的事件。这些将按相同事件的数量排序显示在屏幕上。

我尝试创建脚本,这些是我的结果。我想知道上面有没有错误。

cls
$eventType = Read-host "Introduce one kind of event"
try { Get-EventLog -LogName $eventType | Group-Object 'InstanceID' | Sort-Object -Property InstanceID -Descending -ErrorAction Stop} 
catch { Write-Output "unrecognizable event" }

【问题讨论】:

    标签: powershell powershell-ise


    【解决方案1】:

    我通过将用户输入命令分解为它自己的变量来让你的脚本工作。

    $LogName = Read-host "Introduce one kind of event" 
    $eventType = try { Get-EventLog -LogName $LogName | Group-Object 'InstanceID' | Sort-Object -Property InstanceID -Descending -ErrorAction Stop} catch { Write-Output "unrecognizable event" }
    $eventType
    

    【讨论】:

      【解决方案2】:

      因此,在阅读了您的代码预编辑后,我想强调几件事;

      1,您已将代码粘贴为一行,这不会执行,因为它会将cls 之后的所有其他内容视为参数。新行和; 表示命令结束。

      作为一行,您必须使用 ; 分隔您的命令

      cls; $eventType = Read-host "Introduce one kind of event"; try { Get-EventLog -LogName $eventType | Group-Object 'InstanceID' | Sort-Object -Property InstanceID -Descending -ErrorAction Stop} catch { Write-Output "unrecognizable event" }
      

      不过,我建议您将代码分成多行。

      2,要按实例数排序(按相同事件数排序),您需要修改您的sort-object

      cls
      $eventType = Read-host "Introduce one kind of event"
      try { Get-EventLog -LogName $eventType | Group-Object 'InstanceID' | Sort-Object -Property Count -Descending -ErrorAction Stop} 
      catch { Write-Output "unrecognizable event" }
      

      初始对象Get-EventLog 被销毁并替换为Group-Object 对象。然后您想要sort-object 的属性是Count,您可以使用get-member 检查对象的属性。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2012-12-08
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2011-03-06
        相关资源
        最近更新 更多