【发布时间】:2015-04-29 07:20:55
【问题描述】:
我需要在 PowerShell 中显示事件日志消息的最后一行(“:”之后的几乎所有内容。我无法做到这一点,所以我的替代方法是输出消息和子字符串。我的消息中的“:”在位置 200-ish。下面的代码在“:”-ish 之后显示 56 个字符。
如何让它显示“:”之后的所有内容?
Get-Eventlog -Logname Application -Source "HELPME" | format-table timewritten, @{l="User";e={$_.message.substring(309, 56)}} -wrap -autosize
如果有帮助,这里是示例消息:
在源“帮助”中找不到事件 ID“1234567890”的描述。本地计算机可能没有必要的注册表信息或消息 DLL 文件来显示消息,或者您可能没有访问它们的权限。以下信息是活动的一部分**:** 'Blah', 'Blahh', 'Blahhh'
我只需要代码来显示时间和“Blah”、“Blahh”、“Blahhh”。
【问题讨论】:
-
已回答,我想将我的最终代码粘贴到此处以供需要快速复制粘贴的任何人使用:
Get-Eventlog -Logname Application -Source "HelpMe" | format-table timewritten, @{l="User";e={$_.message.substring(($_.message.lastindexof(':')+1))}} -wrap -autosize
标签: powershell substring get-eventlog