1
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

Windows イベントログの時間枠と出力先を指定して出力する Powershell スクリプト

Posted at

Windows のイベントビューアーの全ログを指定した時間枠で、指定したフォルダに出力するスクリプトを作りました。
障害時の調査で再現テストしてもらった時に指定した時間枠のイベントログだけを送ってもらえるとデータ量も少なくて済むし、お互いに調査対象が絞れるから効率的かなと。

Powershell スクリプト

logexport.ps1
<#
.SYNOPSIS
  指定した時間枠で複数のイベントログを .evtx にエクスポートするスクリプト

.PARAMETER StartTime
引数無し実行でパラメーターを都度入力のときはダブルクォートはなしでも大丈夫
  抽出開始時刻 :  例:2025/08/05 14:00:00 , 2025/8/5 14:00 "2025-08-05 14:00:00"

.PARAMETER EndTime
  抽出終了時刻 : 例: 2025/08/05 14:00:00 , 2025/8/5 14:00 "2025-08-05 14:00:00"

.PARAMETER OutputDir
  エクスポート先フォルダを指定 。存在しなければ作成されます。
  エクスポートフォルダ : 例 : c:\tmp\logs
  
パラメータをすべて指定して実行する場合この場合は ""が必要
  .\Export-EventLogs.ps1 `
    -StartTime "2025-08-05 14:00:00" `
    -EndTime   "2025-08-05 14:10:00" `
    -OutputDir "C:\Logs\EventExports"
#>

param(
    [Parameter(Mandatory=$true)] [datetime] $StartTime,
    [Parameter(Mandatory=$true)] [datetime] $EndTime,
    [Parameter(Mandatory=$true)] [string]   $OutputDir
)

# ログ種別の配列
$logs = @(
    'Application',
    'Security',
    'Setup',
    'System',
    'ForwardedEvents'
)

# 出力フォルダを作成
if (-not (Test-Path $OutputDir)) {
    New-Item -ItemType Directory -Path $OutputDir | Out-Null
}

# UTC 形式の文字列に変換
function ToUtcString($dt) {
    return $dt.ToUniversalTime().ToString("yyyy-MM-ddTHH:mm:ss.000Z")
}

$startUtc = ToUtcString $StartTime
$endUtc   = ToUtcString $EndTime

Write-Host "Exporting logs from $StartTime to $EndTime (UTC: $startUtc - $endUtc)" -ForegroundColor Cyan

foreach ($logName in $logs) {
    $outFile = Join-Path $OutputDir "$logName.evtx"
    Write-Host "→ Exporting $logName to $outFile ..." -NoNewline

    $xpath = "*[System[TimeCreated[@SystemTime>='$startUtc' and @SystemTime<='$endUtc']]]"
    $args = @(
        'export-log', $logName, $outFile,
        '/ow:true',
        "/q:`"$xpath`""
    )

    # wevtutil を呼び出し
    $proc = Start-Process -FilePath wevtutil.exe -ArgumentList $args -NoNewWindow -Wait -PassThru
    if ($proc.ExitCode -eq 0) {
        Write-Host " OK" -ForegroundColor Green
    }
    else {
        Write-Host " Failed (ExitCode=$($proc.ExitCode))" -ForegroundColor Red
    }
}

Write-Host "done"

スクリプトの実行方法

引数ありで実行する場合

日付と時間をダブルクォートで括ってください。
-StartTime = 取得開始日の日付と時間
-EndtTime = 取得終了日の日付と時間
-OuputDir = イベントログの出力先フォルダを指定

.\logexport.ps1 -StartTime "2024/8/4 13:00" -EndTime "2024/8/5 19:00" -OutputDir c:\com

引数なしでパラメータ手入力する場合

ダブルクォートなしでも入力可能

PS C:\com> .\logexport.ps1
コマンド パイプライン位置 1 のコマンドレット logexport.ps1
次のパラメーターに値を指定してください:
StartTime: 2024/8/4 13:00
EndTime: 2024/8/5 19:00
OutputDir: c:\com

セキュリティログはアクセスが拒否されたけどそういうもんだろうということでスルーします。

Exporting logs from 08/04/2024 13:00:00 to 08/05/2024 19:00:00 (UTC: 2024-08-04T04:00:00.000Z - 2024-08-05T10:00:00.000Z)
Exporting Application to c:\com\Application.evtx ... OK
Exporting Security to c:\com\Security.evtx ...ログ Security をエクスポートできませんでした。
アクセスが拒否されました。
Failed (ExitCode=5)
Exporting Setup to c:\com\Setup.evtx ... OK
Exporting System to c:\com\System.evtx ... OK
Exporting ForwardedEvents to c:\com\ForwardedEvents.evtx ... OK
done

出力された.evtxファイルをイベントビューアーからインポートして確認

イベントビューアーを開いて 操作 → 保存されたログを開く を選択

image.png

エクスポートしたログを開けば指定した時間帯のログが確認可能です
image.png

以上、終了。

ログ解析はたまにやるといろんな発見があって楽しいけどついつい夜中になっちゃうからほどほどにしないと。

1
1
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
1
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?