2

我正在尝试使用 Get-eventlog commandlet 从 EventLog 中读取所有日志文件

Get-EventLog -LogName Application, Security -after 09/15/2016 -Before 09/17/2016

我需要应用程序、系统、安全等所有日志,而不是 -LogName Application。

有什么帮助吗?

4

2 回答 2

3

您可以像这样获取所有事件日志:

(Get-WinEvent –ListLog * -ErrorAction SilentlyContinue).LogName

然后在它们之间进行循环以从每个日志中获取事件,或者尝试将整个数组作为 传递-LogName,但我可以想象在这种情况下性能损失将是巨大的。

此外,Get-WinEvent它是为替换 而开发的Get-EventLog,因此您可能想改用它。这里有一些信息:http: //blog.netwrix.com/2015/04/06/monitoring-event-logs-with-powershell/

于 2016-09-16T15:15:24.647 回答
1

这是基于@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"

}
于 2022-02-11T08:19:21.223 回答