2
3

イベントログファイル (.evtx) を csv ファイルに変換

Posted at

Windows に標準でインストールされているイベント ビューアーは、ちょっと重いし使いにくいなぁと思って、イベントログ ファイル(.evtx)を .csv ファイルへよく変換しています。

イベント ID、イベントソース、メッセージでフィルターする方法は、覚えてられないので残しておきます。

PowerShell スクリプト

1. evtx を CSV へ変換(基本系)

# イベントログファイル
$filePath = "C:\path\to\system.evtx"

# .evtx を .csv に置き換える
$csvFilePath = $filePath -replace '\.evtx$', '.csv'

# CSV へ変換して出力
Get-WinEvent -Path $filePath |
Select-Object TimeCreated,Id,LevelDisplayName,@{ name = "Message"; expression = { $_.Message.Replace("`r`n", "").Replace("`r","").Replace("`n","") }} | 
Sort-Object TimeCreated -Descending | 
Export-Csv -Path $csvFilePath -NoTypeInformation -Encoding UTF8

この PowerShell スクリプトは、指定されたイベントログ ファイルからイベント情報を抽出し、特定の形式に整形した後、CSV ファイルとしてエクスポートします。以下に、各ステップの動作を詳しく説明します。

Get-WinEvent -Path $filePath

Get-WinEvent コマンドレットは、イベントログやイベントログ ファイルからイベントを取得します。
-Path $filePath では、$filePath に指定されたイベントログ ファイル(通常は .evtx ファイル)のパスを指定します。このコマンドは、指定されたログファイルからイベントを取得します。

Select-Object TimeCreated, Id, LevelDisplayName, @{ name = "Message"; expression = { $_.Message.Replace("`r`n", "").Replace("`r","").Replace("`n","") }}

Select-Object は、取得したイベントから必要なプロパティだけを選択します。選択されたプロパティは以下の通りです。

  • TimeCreated: イベントが発生した日時
  • Id: イベントID
  • LevelDisplayName: イベントの重要度(例: 情報、警告、エラー)
  • Message: イベントのメッセージ内容

Message に対しては、改行文字(\r\n, \r, \n)をすべて削除するためのカスタム式が適用されています。これは、メッセージ内容を1行にするための処理です。

Sort-Object TimeCreated -Descending

Sort-Object は、選択されたオブジェクトを指定されたプロパティで並べ替えます。
TimeCreated プロパティで並べ替えを行い、-Descending オプションによって、最新のイベントが最初に来るように降順に並べ替えます。

Export-Csv -Path $csvFilePath -NoTypeInformation -Encoding UTF8

Export-Csv コマンドレットは、オブジェクトのコレクションをCSVファイルとしてエクスポートします。
-Path $csvFilePath では、生成されるCSVファイルのパスを指定します($csvFilePath は変数に格納されたパス)。
-NoTypeInformation は、CSVファイルにタイプ情報(#TYPE System.Management.Automation.PSCustomObject のような行)を出力しないようにします。
-Encoding UTF8 は、生成されるCSVファイルをUTF-8でエンコードします。

全体の動作

このスクリプトは、指定されたイベントログファイル(.evtx)からイベントデータを取得し、特定の情報(発生日時、イベントID、重要度、メッセージ)を抽出して整形し、その結果を最新のイベントから順に並べ替えて、CSVファイルとして保存します。

このCSVファイルは、後で解析やレポート作成に使用できます。メッセージから改行を削除して1行に整形することで、CSVファイルが見やすくなります。

2. 指定した ID でフィルター

例:イベント ID 6005, 6006, 1074 のログを抽出

# イベントログファイル
$filePath = "C:\path\to\system.evtx"

# .evtx を .csv に置き換える
$csvFilePath = $filePath -replace '\.evtx$', '.csv'

Get-WinEvent -Path $filePath -FilterXPath "Event/System[EventID=6005 or EventID=6006 or EventID=1074]" |
Select-Object TimeCreated,LevelDisplayName,Id,@{ name = "Message"; expression = { $_.Message.Replace("`r`n", "").Replace("`r","").Replace("`n","") }} | 
Sort-Object TimeCreated -Descending | 
Export-Csv -Path $csvFilePath -NoTypeInformation -Encoding UTF8

3. メッセージの内容でフィルター

例:イベント メッセージに MEMORY.DMP を含むログを抽出

# イベントログファイル
$filePath = "C:\temp\system.evtx"

# .evtx を .csv に置き換える
$csvFilePath = $filePath -replace '\.evtx$', '.csv'

Get-WinEvent -Path $filePath |
Where-Object{$_.Message -like '*MEMORY.DMP*'} |
Select-Object TimeCreated,LevelDisplayName,Id,@{ name = "Message"; expression = { $_.Message.Replace("`r`n", "").Replace("`r","").Replace("`n","") }} | 
Sort-Object TimeCreated -Descending | 
Export-Csv -Path $csvFilePath -NoTypeInformation -Encoding UTF8

4. イベント ソース名でフィルター

例:イベント ソースが Service Control Manager であるログを抽出

# イベントログファイル
$filePath = "C:\temp\system.evtx"

# .evtx を .csv に置き換える
$csvFilePath = $filePath -replace '\.evtx$', '.csv'

Get-WinEvent -Path $filePath -FilterXPath "Event/System[Provider[@Name='Service Control Manager']]" | 
Select-Object TimeCreated,LevelDisplayName,Id,@{ name = "Message"; expression = { $_.Message.Replace("`r`n", "").Replace("`r","").Replace("`n","") }} | 
Sort-Object TimeCreated -Descending | 
Export-Csv -Path $csvFilePath -NoTypeInformation -Encoding UTF8

さいごに

CSV ファイルを VS Code 等で開けば、イベント ビューアーよりも状況の把握が楽になると思いますので、お試しください。CSV ファイルにすると見えない情報があるので、そこは注意です。

参考URL

Get-WinEvent (Microsoft.PowerShell.Diagnostics)
https://learn.microsoft.com/ja-jp/powershell/module/microsoft.powershell.diagnostics/get-winevent

Where-Object (Microsoft.PowerShell.Core)
https://learn.microsoft.com/ja-jp/powershell/module/microsoft.powershell.core/where-object

Select-Object (Microsoft.PowerShell.Utility)
https://learn.microsoft.com/ja-jp/powershell/module/microsoft.powershell.utility/select-object

Sort-Object (Microsoft.PowerShell.Utility)
https://learn.microsoft.com/ja-jp/powershell/module/microsoft.powershell.utility/sort-object

Export-Csv (Microsoft.PowerShell.Utility)
https://learn.microsoft.com/ja-jp/powershell/module/microsoft.powershell.utility/export-csv

2
3
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
2
3