使用Get-WmiObject过滤事件日志

时间:2016-01-10 07:44:49

标签: powershell logging windows-server-2008-r2

您好我制作了一个我需要过滤日志的PowerShell脚本

$logFile = Get-WmiObject Win32_NTEventlogFile | Where-Object {$_.logfilename -eq 'Application'}

在Where-Object子句中,我需要使用eventID,source(提供者),开始时间和结束时间进行过滤。

我似乎无法弄清楚如何将这些参数放入其中。请帮忙。

1 个答案:

答案 0 :(得分:1)

您最好使用Get-WinEvent或Get-EventLog Cmdlet来完成此任务:

 Get-WinEvent -LogName Application | 
 Where-Object {$_.Id -eq 903 -and $_.ProviderName -match "office" `
 -and $_.TimeCreated -gt ((Get-Date).AddHours(-6))}

当然,您可以使用-FilterXPath-FilterXml-FilterHashtable参数来使用更智能,更快速的过滤器。

了解更多信息:https://technet.microsoft.com/en-us/library/hh849682.aspx

要导出事件文件,您可以使用wevtutil:

wevtutil epl System c:\temp\system.evtx

了解更多信息:https://technet.microsoft.com/en-us/library/cc732848.aspx

从文件加载:

Get-WinEvent -Path c:\temp\system.evtx