带排序和管道的PowerShell问题

带排序和管道的PowerShell问题,powershell,syntax,select-object,Powershell,Syntax,Select Object,您好,我正在使用PowerShell版本5 我正在运行一个命令,它正在工作,但缩小的搜索不会返回结果 Get-EventLog System -Newest 5 | where {$_.eventID -eq 1074} 所以我想,哦,我只想看到最后5个与我的过滤器匹配的对象。它运行但不返回结果,因为在事件日志中,最后5个条目中没有eventID 1074。所以我只需要把这个参数移到最后。不走运 Get-EventLog System | where {$_.eventID -eq 1074}

您好,我正在使用PowerShell版本5 我正在运行一个命令,它正在工作,但缩小的搜索不会返回结果

Get-EventLog System -Newest 5 | where {$_.eventID -eq 1074}
所以我想,哦,我只想看到最后5个与我的过滤器匹配的对象。它运行但不返回结果,因为在事件日志中,最后5个条目中没有eventID 1074。所以我只需要把这个参数移到最后。不走运

Get-EventLog System | where {$_.eventID -eq 1074} | -newest 5

-newest : The term '-newest' is not recognized as the name of a cmdlet, function, script file, or operable program. Check 
the spelling of the name, or if a path was included, verify that the path is correct and try again.
At line:1 char:53
+ Get-EventLog System | where {$_.eventID -eq 1074} | -newest 5
+                                                     ~~~~~~~
    + CategoryInfo          : ObjectNotFound: (-newest:String) [], CommandNotFoundException
    + FullyQualifiedErrorId : CommandNotFoundException
因此,在管道将参数移动到我认为无法理解的位置后,定位
-newest

任何人都有一些建议,告诉我如何思考这一点,这将在将来帮助我解决问题?

要将筛选结果限制为最多5个事件,您必须在最终管道段中使用
选择对象-前5个

Get-EventLog System | Where-Object { $_.eventID -eq 1074 } | Select-Object -First 5
-latest
是一个特定于
Get EventLog
的参数,它无条件返回第一个
条目,而不管其内容如何


没有提供类似功能的方法,但有一种通用方法,允许通过
-First

从任何输入中选择最多
对象。这里有一种可能更快的方法来获取您想要的信息。它使用
Get-WinEvent
而不是
Get-EventLog
,还使用
-FilterHashtable
参数让事件系统进行一些过滤

#requires -RunAsAdministrator

$FilterHash = @{
    Logname = 'System'
    ID = 1074
    StartTime = (Get-Date).AddDays(-20)
    }
Get-WinEvent -FilterHashtable $FilterHash -MaxEvents 20
这通常明显快于使用
Get EventLog
。[咧嘴笑]

这里有一篇关于这些想法的文章

使用FilterHashTable使用PowerShell筛选事件日志–嘿,脚本编写人员!博客

-

使用get WinEvent从我的环境中的远程计算机中提取该信息大约需要1.5秒。使用get EventLog获取相同的信息大约需要3分钟。我肯定会调查更多。谢谢你的提示和进一步学习的参考资料。@Brian-非常欢迎你!很高兴能帮助。。。[咧嘴笑]