首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >Powershell:在Get-EventLog输出上执行正则表达式并查找最大数字

Powershell:在Get-EventLog输出上执行正则表达式并查找最大数字
EN

Stack Overflow用户
提问于 2013-05-26 06:24:00
回答 2查看 1.4K关注 0票数 3

我需要从这个命令的输出中提取一个特定的数字:

代码语言:javascript
运行
复制
Get-EventLog "application" | Where-Object {$_.EventID -eq 6006}

示例输出为:

代码语言:javascript
运行
复制
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,但结果什么也没有返回。

EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2013-05-26 07:49:14

您需要$matches内置变量。$matches[0]是与正则表达式匹配的文本,$matches[1] .. $matches[n]是匹配的括号表达式(如果有)。遗憾的是,我的机器上没有任何EventID=6006,所以我在没有测试的情况下这样做,但这应该会从排序后的秒列表中选择最后一项:

代码语言:javascript
运行
复制
Get-EventLog "application" | 
    Where-Object {$_.EventID -eq 6006} | 
    Where-Object { $_.Message -match "<Profiles> took (\d*) second" } |
    foreach { [int]$matches[1] } |
    sort |
    select -last 1
票数 3
EN

Stack Overflow用户

发布于 2013-05-26 14:49:47

您可以在不使用正则表达式的情况下获得值。看一下事件的ReplacementStrings属性。它包含一个保存事件条目中存储的替换字符串的数组。

代码语言:javascript
运行
复制
PS> $event.ReplacementStrings
Profiles
71
Logon

在此基础上,您可以使用数组索引来获取您想要的值。

代码语言:javascript
运行
复制
Get-EventLog application | 
Where-Object {$_.EventID -eq 6006 -and $_.ReplacementStrings -eq 'Profiles'} | 
Foreach-Object { $_.ReplacementStrings[1] }
票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/16754448

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档