Get-WinEvent 用于导出 .evt 文件。但无法在事件查看器中打开 evt 文件 - 文件已损坏消息

Get-WinEvent used to export .evt file. But can't open the evt file in Event Viewer - File corrupted message

我制作了一个 Powershell 脚本来从不同域的远程服务器中提取应用程序和系统日志。脚本完美运行!但是,我在导出文件时遇到问题。当我尝试在事件查看器中打开日志文件时,我收到一条消息,指出 日志文件已损坏且无法读取 。以下是脚本的一部分:

$apppath = "\server01\D$\temp\Automated_Logs\applog_" + $server + ".evtx"
Get-WinEvent -FilterHashtable @{ logname = 'Application'; StartTime = $start; EndTime = $end } -ComputerName $server -Credential $c | Out-File -FilePath $apppath 

服务器 OS:Windows 服务器 2003/2008

Get-WinEvent 将事件日志和 .evt 文件作为 .NET 对象读入内存,您不能将这些对象保存为有效的 .evtx 文件。 查看 wevtutil.exe 将日志条目写入 .evtx 文件:https://technet.microsoft.com/en-us/library/cc732848.aspx

如果您想以 PowerShell 方式执行此操作,可以使用 WMI BackupEventlog 方法。

例如:

(Get-WmiObject -Class Win32_NTEventlogFile | Where-Object LogfileName -EQ 'System').BackupEventlog('C:\Temp\System.evtx')