get-winevent:将格式文件更改为 groupby 日志名无效

Posted

技术标签:

【中文标题】get-winevent:将格式文件更改为 groupby 日志名无效【英文标题】:get-winevent: changing format file to groupby logname has no effect 【发布时间】:2020-08-23 15:10:06 【问题描述】:

我想在 powershell 5.1 中将 get-winevent 的默认输出更改为如下所示,因此标题显示的是 LogName 而不是 ProviderName。

get-winevent application -MaxEvents 1


   LogName: Application

TimeCreated                     Id LevelDisplayName Message
-----------                     -- ---------------- -------
8/23/2020 10:32:25 AM            0

我可以在 powershell 7 中非常轻松地做到这一点,方法是在 $PSHOME 下的某处编辑 Event.format.ps1xml:

            <ViewSelectedBy>
                <TypeName>System.Diagnostics.Eventing.Reader.EventLogRecord</TypeName>
            </ViewSelectedBy>
            <GroupBy>
                <PropertyName>LogName</PropertyName>
                <Label>LogName</Label>
            </GroupBy>

但在 powershell 5.1 中,更改所有权和安全性以便我可以写入该文件后,相同的更改似乎没有效果!似乎 get-winevent 完全忽略了该文件。如何进行此更改?

cd $pshome
takeown /f Event.Format.ps1xml /a
icacls event.format.ps1xml /grant administrators:w

我认为这种格式是在 c# 中的某个地方定义的? Remove generated types and formats for Microsoft.PowerShell.Diagnostics #1218

【问题讨论】:

事后你还记得Update-FormatData吗? @MathiasR.Jessen 没有,但我已经重新启动了 powershell,再次登录等。我现在刚刚尝试过,但没有效果。 我记得 Don Jones 说过 ps1xml 文件是经过数字签名的,您不应该修改原件,而应制作副本。我已经确认无需更改任何权限,我可以简单地将文件复制到另一个位置,修改它,然后 Update-TypeData -Appendpath \path\to\new.ps1xml 并且它工作正常。这还不够吗? about_format.ps1xml 表示“从 PowerShell 6 开始,默认视图在 PowerShell 源代码中定义。PowerShell 5.1 和更早版本的 Format.ps1xml 文件在 PowerShell 6 和更高版本中不存在. PowerShell 源代码定义了 PowerShell 控制台中对象的默认显示。您可以创建自己的 Format.ps1xml 文件来更改对象的显示或为您在 PowerShell 中创建的新对象类型定义默认显示。" @DougMaurer 是的,制作新格式文件是一种解决方案。文档不正确,这并不罕见。该文件仍然存在,但它位于 $pshome\Modules\Microsoft.PowerShell.Diagnostics\event.format.ps1xml 中。我毫不费力地对其进行了修改并使其生效。在 powershell 5.1 中,该文件位于 $pshome\event.format.ps1xml 但没有任何用途。 【参考方案1】:

有人确认 event.format.ps1xml 出于性能原因未在 powershell 5.1 中使用。格式是在 c# 中定义的。 https://github.com/PowerShell/PowerShell/issues/1218#issuecomment-678804787

“Format-table -groupby logname”在 ps 5 中起作用。标题仍然显示“providername”,但实际上不是。最好先排序。 $pshome\event.format.ps1xml 在powershell 5.1 中没有功能。我不相信 EventLogRecord 类型可用于格式化。

get-winevent application,system -maxevents 3 | sort logname | ft -GroupBy logname


   ProviderName: Application

TimeCreated                     Id LevelDisplayName Message
-----------                     -- ---------------- -------
8/23/2020 7:10:04 PM         16384 Information      Successfully scheduled Software Protection service for re-start at 20...
8/23/2020 8:06:25 PM           455 Error            svchost (7476,R,98) TILEREPOSITORYS-1-5-18: Error -1023 (0xfffffc01) ...


   ProviderName: System

TimeCreated                     Id LevelDisplayName Message
-----------                     -- ---------------- -------
8/23/2020 8:14:59 PM         10016 Warning          The machine-default permission settings do not grant Local Activation...

【讨论】:

以上是关于get-winevent:将格式文件更改为 groupby 日志名无效的主要内容,如果未能解决你的问题,请参考以下文章

怎样将jpg更改为png格式

将语法从 sqlplus 格式更改为 mysql 格式

get-winevent 输出到以二进制形式存储的文件

将日期更改为字符串格式

如何将字典的格式更改为 value1;key1;在 Python 3.6 中?

如何通过 `Get-WinEvent` 和 Date Created 事件属性获取准确的事件系统时间?