我正在尝试编写回顾过去30天远程服务器上的应用程序和系统事件日志的脚本,仅查找警告、错误或关键条目。
借鉴我在这里和其他论坛上找到的东西,我得出了以下结论:
$Date = Get-Date
$Range = $Date.AddDays(-30)
$Range = $range.ToShortDateString();
$LogName = Read-Host "Which Log? (Application, System)"
$Server = Read-Host "Please Enter Server Name"
get-eventlog $LogName -ComputerName $Server -After $range | where {$_.EntryType -eq "Error" -or $_.EntryType -eq "Warning" -or $_.EntryType -eq "Critical"}这似乎运行得相当快,但如果确实如此,它会在返回提示符之前挂起几分钟(5-10分钟以上)。
注意:如果我删除代码:
-After $range我可以使用ctrl-c简单地中断输出,然后继续我的工作,但我更希望它按预期运行,然后停止……
那么:有没有关于如何消除这个挂起的想法?
我也对如何让代码更优雅(更快)的想法持开放态度!我不介意脚本检查应用程序和系统日志,而不必运行两次……
发布于 2013-09-26 09:40:38
在Get-EventLog上使用-EntryType字符串数组参数比检索整个事件日志然后使用Where-Object过滤要快得多
试试get-eventlog -Logname System -EntryType ("Error", "Warning")
然而..。如果我将"Critical“放在-EntryType数组中,我会得到:The argument "Critical" does not belong to the set "Error,Information,FailureAudit,SuccessAudit,Warning" specified by the ValidateSet attribute.,这让我怀疑您是否应该听从Get-Help Get-EventLog中列出的建议:
包含EventLog名词的cmdlet( EventLog Cmdlet)仅适用于传统事件日志。若要从使用Windows Vista和Windows更高版本中的Windows事件日志技术的日志中获取事件,请使用Get-WinEvent。
改用Get-WinEvent,我想这就是您想要的:Get-Winevent -FilterHashtable @{LogName="System","Application"; Level=1,2,3; startTime=$range}
这将检查级别1、2或3的事件(分别为严重、错误、警告),并在同一调用中搜索应用程序和系统日志。
发布于 2014-07-02 07:30:47
我发现,对于远程系统,如果我将其封装到一个Invoke-Command中,使用相同的命令,我可以一次查询多个系统,速度比单独查询要快。这是我的解决方案。系统越多,节省的时间就越多。YMMV
$command = {Get-EventLog -LogName Application -After (Get-Date).AddHours("-24")}
Invoke-Command -ComputerName "foo1","foo2","foo3","foo4" -ScriptBlock $commandhttps://stackoverflow.com/questions/19017338
复制相似问题