【Get-EventLog】PowerShell でイベントログを取得する

スポンサーリンク
TEST

1. Log名を指定してイベントログを取得する

これが基本の形となります。Log名の指定は必須です。

書式

Get-EventLog -LogName <Log名>

実行例: System Log を指定した場合

PS> Get-EventLog -LogName "System"

   Index Time          EntryType   Source                 InstanceID Message
   ----- ----          ---------   ------                 ---------- -------
   69415 3 03 08:29    Information Service Control M...   1073748860 Software Protection サービスは 実行中 状態に移行しました。
   69414 3 03 08:29    Information Service Control M...   1073748860 Software Protection サービスは 実行中 状態に移行しました。
   69413 3 03 08:28    Information Service Control M...   1073748860 Software Protection サービスは 実行中 状態に移行しました。
   69412 3 03 08:28    Information Service Control M...   1073748860 Software Protection サービスは 実行中 状態に移行しました。
<以下略>

※指定できるLog名は下記のとおりです。Get-Eventlog -List コマンドで確認できます。

No.Log名
1System
2Application
3Security
4Active Directory Web Services
5DFS Replication
6Directory Service
7DNS Server
8HardwareEvents
9Internet Explorer
10Key Management Service
11Windows PowerShell

2. 出力結果をList 表示に変更する

書式

Get-EventLog -LogName <Log名>| Format-List

デフォルトだと Format-Table という表示形式となっており画面に入り切らない情報が表示されません。表示形式を Format-List という表示形式に変更することで、内容をすべて表示させることができます。

実行例

PS> Get-EventLog -LogName System | Format-List


Index              : 70128
EntryType          : Information
InstanceId         : 1073748860
Message            : Software Protection サービスは 実行中 状態に移行しました。
Category           : (0)
CategoryNumber     : 0
ReplacementStrings : {Software Protection, 実行中}
Source             : Service Control Manager
TimeGenerated      : 2024/03/03 14:21:12
TimeWritten        : 2024/03/03 14:21:12
UserName           :

Index              : 70127
EntryType          : Information
InstanceId         : 1073748860
Message            : Software Protection サービスは 実行中 状態に移行しました。
Category           : (0)
CategoryNumber     : 0
ReplacementStrings : {Software Protection, 実行中}
Source             : Service Control Manager
TimeGenerated      : 2024/03/03 14:20:42
TimeWritten        : 2024/03/03 14:20:42
UserName           :
<以下略>

3. 表示項目を選択する

書式

Get-EventLog -LogName <Log名> | Select-object <項目名1>, <項目名2>, ….,<項目名n>

※すべての項目を表示させる場合は * を指定します。

実行例: イベントID、発生日、メッセージを表示

PS> Get-EventLog -LogName Security | Select-object EventID, TimeGenerated, Message

EventID TimeGenerated       Message
------- -------------       -------
   4634 2024/03/03 14:29:33 アカウントがログオフしました。...
   4627 2024/03/03 14:29:33 グループ メンバーシップ情報。...
   4624 2024/03/03 14:29:33 アカウントが正常にログオンしました。...
   4672 2024/03/03 14:29:33 新しいログオンに特権が割り当てられました。...
   4634 2024/03/03 14:28:33 アカウントがログオフしました。...
   4627 2024/03/03 14:28:33 グループ メンバーシップ情報。...
   4624 2024/03/03 14:28:33 アカウントが正常にログオンしました。...
   4672 2024/03/03 14:28:33 新しいログオンに特権が割り当てられました。...
   4634 2024/03/03 14:28:26 アカウントがログオフしました。...
   4627 2024/03/03 14:28:26 グループ メンバーシップ情報。...
<以下略>

実行例: すべての項目を表示

PS> Get-EventLog -LogName System | Select-object *


EventID            : 7036
MachineName        : WIN201902.test_domain3.test
Data               : {115, 0, 112, 0...}
Index              : 70152
Category           : (0)
CategoryNumber     : 0
EntryType          : Information
Message            : Software Protection サービスは 実行中 状態に移行しました。
Source             : Service Control Manager
ReplacementStrings : {Software Protection, 実行中}
InstanceId         : 1073748860
TimeGenerated      : 2024/03/03 14:33:12
TimeWritten        : 2024/03/03 14:33:12
UserName           :
Site               :
Container          :

EventID            : 7036
MachineName        : WIN201902.test_domain3.test
Data               : {115, 0, 112, 0...}
Index              : 70151
Category           : (0)
CategoryNumber     : 0
EntryType          : Information
Message            : Software Protection サービスは 実行中 状態に移行しました。
Source             : Service Control Manager
ReplacementStrings : {Software Protection, 実行中}
InstanceId         : 1073748860
TimeGenerated      : 2024/03/03 14:32:42
TimeWritten        : 2024/03/03 14:32:42
UserName           :
Site               :
Container          :

<以下略>

4. 期間を指定してイベントログを取得する

書式

Get-EventLog -LogName <Log名> -After <YYYY/MM/DD hh:mm:ss> -Before <YYYY/MM/DD hh:mm:ss>

4.1 指定した日時より後に記録されたログ

実行例: 2023/09/24 00:00:01以降に記録されたログをすべて表示 ※2024/03/03 にコマンド実行
※Afterは指定した時刻は含まないことに注意 (この例では2023/09/24 00:00:01以降のログが表示されます)

PS> Get-EventLog -LogName System -After "2023/09/24 00:00:00"

   Index Time          EntryType   Source                 InstanceID Message
   ----- ----          ---------   ------                 ---------- -------
   69672 3 03 10:40    Information Service Control M...   1073748860 Software Protection サービスは 実行中 状態に移行しました。
   69670 3 03 10:39    Information Service Control M...   1073748860 Software Protection サービスは 実行中 状態に移行しました。
   69668 3 03 10:38    Information Service Control M...   1073748860 Software Protection サービスは 実行中 状態に移行しました。
<以下略>

4.2. 指定した日時より前に記録されたログ

実行例: 2024/03/03 23:59:59 以前に記録されたログをすべて表示
※Beforeは指定した時刻は含まないことに注意 (2024/03/02 23:59:59以降のログが表示されます)

PS> Get-EventLog -LogName System -Before "2024/03/03 00:00:00"

   Index Time          EntryType   Source                 InstanceID Message
   ----- ----          ---------   ------                 ---------- -------
   68404 3 02 23:59    Information Service Control M...   1073748860 Software Protection サービスは 実行中 状態に移行しました。
   68402 3 02 23:58    Information Service Control M...   1073748860 Software Protection サービスは 実行中 状態に移行しました。
   68401 3 02 23:58    Information Service Control M...   1073748860 Software Protection サービスは 実行中 状態に移行しました。
   68400 3 02 23:57    Information Service Control M...   1073748860 Software Protection サービスは 実行中 状態に移行しました。
<以下略>

4.3. 指定した期間に記録されたログ

実行例: 2023/09/24 00:00:01 ~ 2024/03/03 23:59:59に記録されたログをすべて表示

PS> Get-EventLog -LogName System -After "2023/09/24 00:00:00" -Before "2024/03/03 00:00:00"

   Index Time          EntryType   Source                 InstanceID Message
   ----- ----          ---------   ------                 ---------- -------
   68404 3 02 23:59    Information Service Control M...   1073748860 Software Protection サービスは 実行中 状態に移行しました。
   68402 3 02 23:58    Information Service Control M...   1073748860 Software Protection サービスは 実行中 状態に移行しました。
   68401 3 02 23:58    Information Service Control M...   1073748860 Software Protection サービスは 実行中 状態に移行しました。
   68400 3 02 23:57    Information Service Control M...   1073748860 Software Protection サービスは 実行中 状態に移行しました。
<以下略>

5. イベントIDを指定してイベントログを取得する

5.1. 単一のイベントIDを指定

書式

Get-EventLog -LogName <Log名> | Where-Object {$_.EventID -eq <イベントID>}

実行例1: イベントID 12のログを抽出

PS> Get-EventLog -LogName System | Where-Object {$_.EventID -eq 12}

   Index Time          EntryType   Source                 InstanceID Message
   ----- ----          ---------   ------                 ---------- -------
   66870 3 01 13:51    Warning     Microsoft-Windows...           12 タイム プロバイダー NtpClient: このコンピューターは、ドメイン階層を使ってタイムソースを決定するように構成されていますが、フォレストのルートにあるドメイン用の AD PDC...
   66744 3 01 13:51    Information Microsoft-Windows...           12 ソース 'Microsoft-Windows-Kernel-General' のイベント ID '12' の説明が見つかりません。必要なレジストリ情報またはメッセージを表示するメッセージ DLL ファイルがローカル...
   66533 9 24 10:43    Warning     Microsoft-Windows...           12 タイム プロバイダー NtpClient: このコンピューターは、ドメイン階層を使ってタイムソースを決定するように構成されていますが、フォレストのルートにあるドメイン用の AD PDC...
   66417 9 24 10:43    Information Microsoft-Windows...           12 ソース 'Microsoft-Windows-Kernel-General' のイベント ID '12' の説明が見つかりません。必要なレジストリ情報またはメッセージを表示するメッセージ DLL ファイルがローカル...
   66320 9 24 10:40    Information Microsoft-Windows...           12 ソース 'Microsoft-Windows-Kernel-General' のイベント ID '12' の説明が見つかりません。必要なレジストリ情報またはメッセージを表示するメッセージ DLL ファイルがローカル...
   66237 9 24 10:31    Warning     Microsoft-Windows...           12 タイム プロバイダー NtpClient: このコンピューターは、ドメイン階層を使ってタイムソースを決定するように構成されていますが、フォレストのルートにあるドメイン用の AD PDC...
   66125 9 24 10:31    Information Microsoft-Windows...           12 ソース 'Microsoft-Windows-Kernel-General' のイベント ID '12' の説明が見つかりません。必要なレジストリ情報またはメッセージを表示するメッセージ DLL ファイルがローカル...
   66040 9 24 10:23    Warning     Microsoft-Windows...           12 タイム プロバイダー NtpClient: このコンピューターは、ドメイン階層を使ってタイムソースを決定するように構成されていますが、フォレストのルートにあるドメイン用の AD PDC...

5.2. 複数のイベントIDを指定

複数のイベントIDを指定する場合は括弧でくくり -or 条件を使います。

書式

Get-EventLog -LogName <Log名> | Where-Object {($_.EventID -eq <イベントID>) -or ($_.EventID -eq <イベントID>)}

実行例1: イベントIDが 12 および 13のログを抽出

PS> Get-EventLog -LogName System | Where-Object {($_.EventID -eq 12) -or ($_.EventID -eq 13)}

   Index Time          EntryType   Source                 InstanceID Message
   ----- ----          ---------   ------                 ---------- -------
   66870 3 01 13:51    Warning     Microsoft-Windows...           12 タイム プロバイダー NtpClient: このコンピューターは、ドメイン階層を使ってタイムソースを決定するように構成されていますが、フォレストのルートにあるドメイン用の AD PDC...
   66744 3 01 13:51    Information Microsoft-Windows...           12 ソース 'Microsoft-Windows-Kernel-General' のイベント ID '12' の説明が見つかりません。必要なレジストリ情報またはメッセージを表示するメッセージ DLL ファイルがローカル...
   66743 9 24 19:47    Information Microsoft-Windows...           13 ソース 'Microsoft-Windows-Kernel-General' のイベント ID '13' の説明が見つかりません。必要なレジストリ情報またはメッセージを表示するメッセージ DLL ファイルがローカル...
   66533 9 24 10:43    Warning     Microsoft-Windows...           12 タイム プロバイダー NtpClient: このコンピューターは、ドメイン階層を使ってタイムソースを決定するように構成されていますが、フォレストのルートにあるドメイン用の AD PDC...

6. ソースを指定してイベントログを取得する

書式

Get-EventLog -LogName <Log名> -Source <Source名>

PS> Get-EventLog -LogName System -Source Microsoft-Windows-Kernel-General

   Index Time          EntryType   Source                 InstanceID Message
   ----- ----          ---------   ------                 ---------- -------
   67347 3 02 15:25    Information Microsoft-Windows...            1 CVE の検出の可能性: 2024-03-02T06:25:57.124000000Z...
   67205 3 02 09:42    Information Microsoft-Windows...            1 CVE の検出の可能性: 2024-03-02T00:42:29.058000000Z...
   67085 3 01 19:18    Information Microsoft-Windows...            1 CVE の検出の可能性: 2024-03-01T10:18:44.395000000Z...
   66977 3 01 14:01    Information Microsoft-Windows...           16 ソース 'Microsoft-Windows-Kernel-General' のイベント ID '16' の説明が見つかりません。必要なレジストリ情報またはメッセージを表示するメッセージ DLL ファイルがローカル...
   66974 3 01 14:01    Information Microsoft-Windows...           16 ソース 'Microsoft-Windows-Kernel-General' のイベント ID '16' の説明が見つかりません。必要なレジストリ情報またはメッセージを表示するメッセージ DLL ファイルがローカル...
   66969 3 01 14:01    Information Microsoft-Windows...           16 ソース 'Microsoft-Windows-Kernel-General' のイベント ID '16' の説明が見つかりません。必要なレジストリ情報またはメッセージを表示するメッセージ DLL ファイルがローカル...

7. ファイルとしてエクスポートする

画面表示ではなくファイルに出力することもできます。

書式

Get-EventLog -LogName <Log名> | Export-Csv -Path <ファイルのパス>

PS> Get-EventLog -LogName System | Export-Csv -Path C:\SystemLog.csv
PS>

文字コードを指定することもできます。ファイルの中身が文字化けする場合は UTF-8 を指定してみましょう。

書式

Get-EventLog -LogName <Log名> | Export-Csv -Path <ファイルのパス> -Encoding UTF8

PS> Get-EventLog -LogName System | Export-Csv -Path C:\SystemLog.csv -Encoding UTF8
PS>

以上。

コメント