首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >收集事件日志

收集事件日志
EN

Stack Overflow用户
提问于 2021-11-23 09:26:57
回答 4查看 75关注 0票数 0

我想要收集自定义的时间戳以来的所有事件日志。下面是我的分块代码:

代码语言:javascript
复制
$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文件

EN

回答 4

Stack Overflow用户

发布于 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

代码语言:javascript
复制
$filterXml = '
<QueryList>
  <Query Id="0" Path="Application">
    <Select Path="Application">*[System[TimeCreated[@SystemTime&gt;=''2021-11-23T10:00:00.000Z'' and @SystemTime&lt;=''2021-11-23T14:03:00.999Z'']]]</Select>
  </Query>
</QueryList>'

Get-WinEvent –FilterXml $filterXml
票数 0
EN

Stack Overflow用户

发布于 2021-11-23 14:21:53

不确定是否阅读扩展描述,但您可以使用以下内容读取事件日志。注意logname允许使用通配符,因此*。

代码语言:javascript
复制
$time = (get-date).AddMinutes(-5)
Get-WinEvent  –FilterHashtable @{logname='*'; starttime=$time}
票数 0
EN

Stack Overflow用户

发布于 2021-11-27 15:09:41

我用我写的代码块找到了一个解决方案:

代码语言:javascript
复制
    # 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'

为了方便起见,我将只捕获消息的第一行。为了实现这一点,我采用了以下代码块:

代码语言:javascript
复制
            $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,还有一些我不知道。最后这块代码不能让我满意,即使它可以工作。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/70078306

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档