PowerShell的:正则表达式的获取,事件日志输出和发现数量最多

问题描述:

我需要从这个命令的输出拉特定号码:PowerShell的:正则表达式的获取,事件日志输出和发现数量最多

Get-EventLog "application" | Where-Object {$_.EventID -eq 6006} 

示例输出:

Index Time   EntryType Source     InstanceID Message 
----- ----   --------- ------     ---------- ------- 
18297 May 15 18:49 Warning  Wlclntfy    2147489654 The winlogon notification subscriber <Profiles> took 60 second(s) to handle the notification event (Logon). 
11788 Jan 31 08:11 Warning  Wlclntfy    2147489654 The winlogon notification subscriber <Profiles> took 68 second(s) to handle the notification event (Logon). 
5794 Oct 16 09:41 Warning  Wlclntfy    2147489654 The winlogon notification subscriber <Sens> took 225 second(s) to handle the notification event (Logoff). 
5596 Oct 11 08:03 Warning  Wlclntfy    2147489654 The winlogon notification subscriber <Profiles> took 69 second(s) to handle the notification event (Logon). 
2719 Aug 30 07:50 Warning  Wlclntfy    2147489654 The winlogon notification subscriber <Profiles> took 65 second(s) to handle the notification event (Logon). 

我其实需要做的是拉动<Profiles>事件报告的秒数,并拉出最大的一个。我已经得到了尽可能多的(?<=<Profiles> took)(\d+)将拉动我需要的数字,但我不知道如何继续实际提取它们。我已经尝试将它传递给Select-String -pattern,但它完全没有任何返回。

+0

您需要编写一个循环,并继续比较正则表达式返回值以找到最大值。 AFAIK,正则表达式不能返回最大数字:) – Bill

你想要$matches内建变量。 $matches[0]是匹配正则表达式的文本,并且$matches[1] .. $matches[n]是匹配的括号表达式(如果有的话)。 可悲的是,我没有任何事件ID = 6006我的机器上,所以我没有测试这样做,但这应该从秒排序列表中选择最后一项:

Get-EventLog "application" | 
    Where-Object {$_.EventID -eq 6006} | 
    Where-Object { $_.Message -match "<Profiles> took (\d*) second" } | 
    foreach { [int]$matches[1] } | 
    sort | 
    select -last 1 
+0

这样做完美,谢谢! – rmart

你可以得到的值( s)没有正则表达式。查看事件的ReplacementStrings属性。它包含一个数组,用于保存事件条目中存储的替换字符串。

PS> $event.ReplacementStrings 
Profiles 
71 
Logon 

基于此,您可以使用数组索引来获取您之后的值。

Get-EventLog application | 
Where-Object {$_.EventID -eq 6006 -and $_.ReplacementStrings -eq 'Profiles'} | 
Foreach-Object { $_.ReplacementStrings[1] }