【发布时间】:2017-01-24 20:52:18
【问题描述】:
我正在尝试使用 Get-eventlog commandlet 从 EventLog 中读取所有日志文件
Get-EventLog -LogName Application, Security -after 09/15/2016 -Before 09/17/2016
我需要应用程序、系统、安全等所有日志,而不是 -LogName Application。
有什么帮助吗?
【问题讨论】:
我正在尝试使用 Get-eventlog commandlet 从 EventLog 中读取所有日志文件
Get-EventLog -LogName Application, Security -after 09/15/2016 -Before 09/17/2016
我需要应用程序、系统、安全等所有日志,而不是 -LogName Application。
有什么帮助吗?
【问题讨论】:
您可以像这样获取所有事件日志:
(Get-WinEvent –ListLog * -ErrorAction SilentlyContinue).LogName
然后在它们之间循环以从每个日志中获取事件,或者尝试将整个数组作为-LogName 传递,但我可以想象在这种情况下性能损失将是巨大的。
另外,Get-WinEvent 是为替换 Get-EventLog 而开发的,因此您可能想改用它。以下是一些信息:http://blog.netwrix.com/2015/04/06/monitoring-event-logs-with-powershell/
【讨论】:
这是基于@andrey-marchuk 回答的工作代码。所有日志都附加在一个文件中(⚠ 使用编码UTF-8 with BOM 保存文件,因为@)
$Begin = '10/02/2022 09:00:00'
$End = '10/02/2022 09:15:05'
$path= "C:\Users\user\Desktop\logevent\\"
If(!(test-path $path))
{
New-Item -ItemType Directory -Force -Path $path
}
$datetimenow = [DateTime]::Now.ToString("yyyy_MM_dd HH_mm_ss")
$allLog = (Get-WinEvent –ListLog * -ErrorAction SilentlyContinue).LogName
foreach ($lognameName in $allLog){
Get-WinEvent -FilterHashtable @{logname = $lognameName; StartTime = "$Begin"; EndTime = "$End" } | Select-Object * | Out-File -Enc UTF8 -Append "$path $datetimenow winevent.txt"
}
如果您只想检索事件的特定信息,请将Select-Object * 替换为Select-Object TimeCreated, ID, LogName, Source, LevelDisplayName, Message 之类的内容
如果您想要为每个 logname 单独的文件,您可以这样做,但要小心,这将创建 > 400 个文件:
$Begin = '10/02/2022 09:00:00'
$End = '10/02/2022 09:15:05'
$path= "C:\Users\user\Desktop\logevent\\"
If(!(test-path $path))
{
New-Item -ItemType Directory -Force -Path $path
}
$allLog = (Get-WinEvent –ListLog * -ErrorAction SilentlyContinue).LogName
foreach ($lognameName in $allLog){
$lognameFile = $lognameName.Replace("/", "-")
$datetimenow = [DateTime]::Now.ToString("yyyy_MM_dd HH_mm_ss")
Get-WinEvent -FilterHashtable @{logname = $lognameName; StartTime = "$Begin"; EndTime = "$End" } | Select-Object * | Out-File -Enc UTF8 -FilePath "$path $datetimenow winevent $lognameFile .txt"
}
【讨论】: