我想要收集自定义的时间戳以来的所有事件日志。下面是我的分块代码:
$StartTime = (Get-Date).AddMinutes(-5)
$rawdata = Get-WinEvent -ListLog *
$eventlogs = @{}
foreach ($record in $rawdata) {
if ($record.LastWriteTime -gt $StartTime) {
$eventlogs[$record.GetHashCode()] = @{
'LogType' = $record.LogType
'Name' = $record.LogName
'Provider' = $record.OwningProviderName
'Path' = $record.LogFilePath
'Mode' = $record.LogMode
'Time' = $record.LastWriteTime
}
}
}除上述信息外,如何检索每个事件日志的完整扩展描述?我希望避免解析每个单独的.evtx文件
发布于 2021-11-23 14:20:16
最好的方法是使用Get-WinEvent的FilterXml参数。您可以通过使用事件查看器创建筛选器来实际创建筛选器:


然后将其复制>粘贴到PowerShell中。此外,您还需要转义时间'2021-11-23T10:00:00.000Z‘应变为''2021-11-23T10:00:00.000Z'’的单引号。注意,它们是2* ',而不是双引号“。当然,如果你想通过变量传递日期,你可以使用Get-Date
$filterXml = '
<QueryList>
<Query Id="0" Path="Application">
<Select Path="Application">*[System[TimeCreated[@SystemTime>=''2021-11-23T10:00:00.000Z'' and @SystemTime<=''2021-11-23T14:03:00.999Z'']]]</Select>
</Query>
</QueryList>'
Get-WinEvent –FilterXml $filterXml发布于 2021-11-23 14:21:53
不确定是否阅读扩展描述,但您可以使用以下内容读取事件日志。注意logname允许使用通配符,因此*。
$time = (get-date).AddMinutes(-5)
Get-WinEvent –FilterHashtable @{logname='*'; starttime=$time}发布于 2021-11-27 15:09:41
我用我写的代码块找到了一个解决方案:
# get events log
Write-Host -NoNewline "Retrieving events log... "
$logtime = (Get-Date).AddMinutes(-15)
$eventlogs.Clear()
$string = ''
$ErrorActionPreference= 'SilentlyContinue'
foreach ($logtype in ('System', 'Security','Application')) {
$rawdata = Get-WinEvent -FilterHashTable @{LogName=$logtype; StartTime=$logtime}
foreach ($record in $rawdata) {
$logkey = '[' + $record.LogName + ']_'
$logkey += Get-Date $record.TimeCreated -format "yyyy-MM-dd_HH-mm-ss-fff"
$record.Message -match "^(.*)\r+" > $null
if ($matches[1]) {
$string = $matches[1]
$matches[1] = $null
} else {
$record.Message -match "^(.*)\n+" > $null
if ($matches[1]) {
$string = $matches[1]
$matches[1] = $null
} else {
$string = $record.Message
}
}
$eventlogs[$logkey] = @{
'Name' = $record.LogName
'Time' = Get-Date $record.TimeCreated -format "yyyy-MM-dd_HH-mm-ss"
'Id' = $record.Id
'Message' = $string
'Type' = $record.LevelDisplayName
}
}
}
$ErrorActionPreference= 'Inquire'
$logfile = $logpath + '\' + $timestamp + '_EventLogs.csv'
'ID;LOGTYPE;NAME;TIME;MESSAGE' | Out-File $logfile -Encoding UTF8 -Append
foreach ($item in ($eventlogs.Keys | Sort-Object)) {
$new_record = @(
$eventlogs[$item].Id,
$eventlogs[$item].Type,
$eventlogs[$item].Name,
$eventlogs[$item].Time,
$eventlogs[$item].Message
)
$new_string = [system.String]::Join(";", $new_record)
$new_string | Out-File $logfile -Encoding UTF8 -Append
}
Write-Host -ForegroundColor Green 'DONE'为了方便起见,我将只捕获消息的第一行。为了实现这一点,我采用了以下代码块:
$record.Message -match "^(.*)\r+" > $null
if ($matches[1]) {
$string = $matches[1]
$matches[1] = $null
} else {
$record.Message -match "^(.*)\n+" > $null
if ($matches[1]) {
$string = $matches[1]
$matches[1] = $null
} else {
$string = $record.Message
}
}这样的解决方案是因为一些消息使用\r\n,其他消息只使用\n,还有一些我不知道。最后这块代码不能让我满意,即使它可以工作。
https://stackoverflow.com/questions/70078306
复制相似问题