【问题标题】:Exporting Windows Event Logs to CSV - Powershell 2.0将 Windows 事件日志导出到 CSV - Powershell 2.0
【发布时间】:2015-02-04 18:21:29
【问题描述】:

我需要定期将 Windows 事件日志从我们的生产环境导出到 CSV。

我有一个简单的 XML 配置文件,其中包含我需要从中获取事件的机器列表,以及我需要检索的事件 ID 列表。

从这里我依次遍历每台机器名称,然后是每个事件 ID 以检索日志,然后导出为 CSV。我希望每台机器每次执行一个 CSV。

一旦我计算出所有变量,PS 命令就可以非常简单地检索一个事件 ID 的日志

foreach ($machine in $config.Configuration.Machines.Machine)
{
    $csvname=$outputlocation + $machine.Value + "_" + $datestring + ".csv"

    foreach ($eventid in $config.Configuration.EventIds.EventId)
    {
        Get-WinEvent -ComputerName $machine.Value -ErrorAction SilentlyContinue -FilterHashTable @{Logname='Security';ID=$eventid.Value} | where {$_.TimeCreated -gt $lastexecutiondate} | export-csv -NoClobber -append $csvname
    }
}

Execpt 我无法每次都附加到 CSV,PS 2.0 显然不支持这一点。我曾尝试一次提取所有事件 ID,但这似乎有点冗长,现在可能允许使用配置文件,但我对 PowerShell 相当陌生,所以我没有太多运气。

我还需要指定多个 LogNames(系统、安全和应用程序),并且希望运行一个语句而不是同一个语句 3 次和应用,但我不确定如何执行此操作。

不幸的是,此时谷歌让我绕着圈子跑。

【问题讨论】:

    标签: powershell-2.0 event-log


    【解决方案1】:

    以下是我汇总的内容,以便我可以导出前 24 小时的事件以供选择事件日志 - 我将创建一个计划任务,以便每天执行一次。 希望这对其他人有帮助...

    $eventLogNames = "Application", "Security", "System", "Windows PowerShell"
    $startDate = Get-Date
    $startDate = $startDate.addDays(-1).addMinutes(-15)
    
    function GetMilliseconds($date)
    {
        $ts = New-TimeSpan -Start $date -End (Get-Date)
        [math]::Round($ts.TotalMilliseconds)
    }
    
    $serverName = get-content env:computername
    $serverIP = gwmi Win32_NetworkAdapterConfiguration |
        Where { $_.IPAddress } | # filter the objects where an address actually exists
        Select -Expand IPAddress | # retrieve only the property *value*
        Where { $_ -notlike '*:*' }
    $fileNameDate = Get-Date -format yyyyMMddhhmm
    $endDate = Get-Date
    $startTime = GetMilliseconds($startDate)
    $endTime = GetMilliseconds($endDate)
    
    foreach ($eventLogName in $eventLogNames)
    {
        Write-Host "Processing Log: " $eventLogName
    
    <# - Remove comment to create csv version of log files  
        $csvFile = $fileNameDate + "_" + $serverIP +"_" + $eventLogName + ".csv"
        Write-Host "Creating CSV Log: " $csvFile
        Get-EventLog -LogName $eventLogName -ComputerName $serverName -After $startDate -ErrorAction     SilentlyContinue | Sort MachineName, TimeWritten | Select MachineName, Source, TimeWritten, EventID, EntryType, Message | Export-CSV $csvFile #ConvertTo-CSV #Format-Table -Wrap -Property Source, TimeWritten, EventID, EntryType, Message -Autosize -NoTypeInformation
    #>
        $evtxFile = $fileNameDate + "_" + $serverIP + "_" + $eventLogName + ".evtx"
        Write-Host "Creating EVTX Log: " $evtxFile
        wevtutil epl $eventLogName $evtxFile /q:"*[System[TimeCreated[timediff(@SystemTime) >=   $endTime] and TimeCreated[timediff(@SystemTime) <= $startTime]]]"
    }
    

    【讨论】:

      【解决方案2】:

      为什么我无法导出日志安全性。指定的查询无效。我为每种类型的事件日志(系统、应用程序等)都得到了这个。这只发生在 evtx 导出中。我得到了 csv 文件 tho`....

      【讨论】:

      • 这是一个问题,而不是对所提出问题的回答。考虑提出一个新问题并在相关时链接到该问题。
      猜你喜欢
      • 1970-01-01
      • 2021-01-18
      • 1970-01-01
      • 1970-01-01
      • 2023-03-20
      • 1970-01-01
      • 2017-04-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多