当前位置:网站首页 > 更多 > 玩电脑 > 正文

[玩转系统] 使用 PowerShell 脚本获取 Windows 事件日志年龄

作者:精品下载站 日期:2024-12-14 03:38:28 浏览:16 分类:玩电脑

使用 PowerShell 脚本获取 Windows 事件日志年龄


Windows 事件日志应至少在系统上保留 7 天,以便您可以查看它们以进行故障排除或任何其他目的。默认情况下,当达到最大事件日志大小时,事件日志将被覆盖。这意味着您将没有足够的日志历史记录,这可能会导致问题。在本文中,您将了解如何使用 PowerShell 脚本获取 Windows 事件日志年龄。

Windows 事件日志时代 PowerShell 脚本

Get-EventLogAge.ps1 PowerShell 脚本将检查系统上的所有事件日志文件夹,并为您提供包含以下信息的报告:

  1. 日志名称
  2. 最旧事件日期
  3. 最新事件日期
  4. 自最古老事件以来的天数
  5. 最大MB
  6. 使用百分比

下载 Windows 事件日志时代 PowerShell 脚本

(C:)驱动器上创建两个文件夹:

  • 脚本
  • 温度

下载 Get-EventLogAge.ps1 PowerShell 脚本并将其放置在系统 C:\scripts 文件夹中。如果您没有脚本文件夹,请创建一个。

确保该文件未被阻止,以防止运行脚本时出现任何错误。请阅读文章运行 PowerShell 脚本时出现未数字签名错误来了解更多信息。

另一种选择是将以下代码复制并粘贴到记事本中。将其命名为 Get-EventLogAge.ps1 并将其放置在 C:\scripts 文件夹中。

<#
    .SYNOPSIS
    Get-EventLogAge.ps1

    .DESCRIPTION
    Export the oldest and newest event logs dates to a CSV file.

    .LINK
    www.a-d.site/get-windows-event-log-age-powershell-script/

    .NOTES
    Written by: ALI TAJRAN
    Website:    www.a-d.site
    LinkedIn:   linkedin.com/in/a-d

    .CHANGELOG
    V1.00, 08/22/2024 - Initial version
#>

# CSV file path to export
$CsvPath = "C:\temp\EventLogAgeReport.csv"

# Get the current date
$currentDate = Get-Date

# Get all log names
$logNames = Get-WinEvent -ListLog * | Select-Object -ExpandProperty LogName

# Create an array to hold the results
$results = @()

# Iterate through each log and find the oldest and newest events
foreach ($logName in $logNames) {
    # Get the oldest event directly
    $oldestEvent = Get-WinEvent -LogName $logName -MaxEvents 1 -Oldest -ErrorAction SilentlyContinue |
    Select-Object -Property TimeCreated

    # Get the newest event directly
    $newestEvent = Get-WinEvent -LogName $logName -MaxEvents 1 -ErrorAction SilentlyContinue |
    Select-Object -Property TimeCreated

    # Get the log's maximum size and current size
    $logInfo = Get-WinEvent -ListLog $logName

    $maxSize = [math]::Round($logInfo.MaximumSizeInBytes / 1048576, 2)
    $currentSize = [math]::Round($logInfo.FileSize / 1048576, 2)

    if ($maxSize -ne 0) {
        $percentageUsed = [math]::Round(($currentSize / $maxSize) * 100, 2)
    }
    else {
        $percentageUsed = "N/A"
    }

    if ($oldestEvent) {
        # Calculate the number of days since the oldest event
        $daysAgo = ($currentDate - $oldestEvent.TimeCreated).Days

        $results += [PSCustomObject]@{
            LogName              = $logName
            OldestEventDate      = $oldestEvent.TimeCreated
            NewestEventDate      = $newestEvent.TimeCreated
            DaysSinceOldestEvent = "$daysAgo"
            MaxSizeMB            = "$maxSize MB"
            PercentageUsed       = "$percentageUsed %"
        }
    }
    else {
        $results += [PSCustomObject]@{
            LogName              = $logName
            OldestEventDate      = "No events found"
            NewestEventDate      = "No events found"
            DaysSinceOldestEvent = "N/A"
            MaxSizeMB            = "$maxSize MB"
            PercentageUsed       = "$percentageUsed %"
        }
    }
}

# Export the results to a CSV file
$results | Export-Csv -Path $CsvPath -NoTypeInformation

这就是它的样子。

[玩转系统] 使用 PowerShell 脚本获取 Windows 事件日志年龄

运行获取 Windows 事件日志时代 PowerShell 脚本

将 Windows 事件日志期限报告导出到 CSV 文件。

以管理员身份运行 PowerShell 并运行以下命令。

C:\scripts\.\Get-EventLogAge.ps1

在路径 C:\temp 中找到文件 EventLogAgeReport.csv

[玩转系统] 使用 PowerShell 脚本获取 Windows 事件日志年龄

使用您喜欢的应用程序打开 CSV 文件。在我们的示例中,它是 Microsoft Excel。

[玩转系统] 使用 PowerShell 脚本获取 Windows 事件日志年龄

报告看起来很棒!

了解更多:使用 PowerShell 脚本进行 Active Directory 运行状况检查 »

结论

您学习了如何使用 PowerShell 脚本获取 Windows 事件日志年龄。运行脚本并查看报告以获取所有详细信息。请记住,对于您重要的文件夹,应保留至少 7 天的事件日志历史记录。

您喜欢这篇文章吗?您可能还喜欢使用 PowerShell 脚本检查 Windows 上的可用磁盘空间。不要忘记关注我们并分享这篇文章。

您需要 登录账户 后才能发表评论

取消回复欢迎 发表评论:

关灯