0

我想在 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

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

            <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# 的某个地方定义的? 删除为 Microsoft.PowerShell.Diagnostics #1218 生成的类型和格式

4

1 回答 1

0

有人证实,出于性能原因,powershell 5.1 中未使用 event.format.ps1xml。格式是在 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...
于 2020-08-23T20:58:40.540 回答