从事件日志中获取独特的事件及其计数

时间:2016-08-04 04:27:37

标签: windows powershell-v2.0

我有一个工作脚本,可以提取Windows应用程序,安全性,设置和系统日志的报告,仅显示过去30天内的严重和错误事件。但是,我也非常希望脚本能够计算过去30天内每次报告的严重或错误事件发生的次数。这是工作命令:

Get-EventLog Application -ComputerName $server -After $starttime |
    ? { $_.entryType -Match "Error" -and "Critical" } | 
    Sort-Object EventID -Unique |
    Select-Object TimeGenerated,EventID,Source,Message | 
    ft -AutoSize -Wrap | 
    Out-File $file -Append

以下是文本文件中输出的示例:

TimeGenerated       EventID Source                                      Message                                        
-------------       ------- ------                                      -------                                        
7/8/2016 1:23:20 PM       0 SQL Server Report Service                   Service cannot be started. Microsoft.ReportingS
                                                                    ervices.Diagnostics.Utilities.InternalCatalogEx
                                                                    ception: An internal error occurred on the repo
                                                                    rt server. See the error log for more details. 
                                                                       at Microsoft.ReportingServices.Library.Nativ
                                                                    e.GetSid(String name, Int32& length)           
                                                                       at Microsoft.ReportingServices.Library.Nativ
                                                                    e.NameToSid(String name)                       
                                                                       at Microsoft.ReportingServices.Library.Servi
                                                                    ceAppDomainController.StartRPCServer(Boolean fi
                                                                    rstTime)                                       
                                                                       at Microsoft.ReportingServices.Library.Servi
                                                                    ceAppDomainController.Start(Boolean firstTime) 
                                                                       at Microsoft.ReportingServices.NTService.Rep
                                                                    ortService.OnStart(String[] args)              
                                                                       at System.ServiceProcess.ServiceBase.Service
                                                                    QueuedMainCallback(Object state)               
7/8/2016 1:23:20 PM     121 Report Server Windows Service (MSSQLSERVER) The Remote Procedure Call (RPC) service failed 
                                                                    to start.                                      

在结果中有另一列显示指定时间段内每个EventID的出现次数会很棒。

1 个答案:

答案 0 :(得分:1)

您可以这样做(第3行和第4行是新的,Count中的Select-Object) 从技术上讲,您也可以从-Unique中删除Sort-Object,因为在对它们进行分组后,只传递该组中的第一项或多或少相同。

Get-EventLog Application -ComputerName $server -After $starttime | 
    ? { $_.entryType -Match "Error" -and "Critical" } |
    Group-Object -Property EventID |
    % { $_.Group[0] | Add-Member -PassThru -NotePropertyName Count -NotePropertyValue $_.Count } |
    Sort-Object EventID -Unique | 
    Select-Object Count, TimeGenerated, EventID, Source, Message | 
    ft -AutoSize -Wrap | 
    Out-File $file -Append