在 PowerShell 中显示事件类型

问题描述

我需要创建一个脚本来要求显示某种类型的事件(例如:系统)。该脚本将在屏幕上显示按事件 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" }

解决方法

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

$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
,

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

1,您已将代码粘贴为一行,这将不会执行,因为它会将 echo $PATH /opt/pkg-big/anaconda3/bin:... 之后的所有其他内容视为参数。新行和 cls 表示命令结束。

作为一行,您必须使用 ;

分隔您的命令
;

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

2、要按实例数排序(按相同事件数排序),您需要修改您的 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" }

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 检查对象的属性。