0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

高速スタートアップを含む起動とシャットダウンのイベントログを PowerShell を使って取得する

Last updated at Posted at 2025-11-16

参考サイト

※「高速スタートアップ」を判別する精度が高い方法は Event ID 27 — Microsoft-Windows-Kernel-Boot を参照することですが─‬‭─‬
BootType = 0 : 完全シャットダウンからの起動
BootType = 1 : 高速スタートアップが有効な起動
BootType = 2 : 休止状態からの復帰
─‬‭─‬ここでは代わりに Event ID 1 — Microsoft-Windows-Power-Troubleshooter を参照しています。

※「モダンスタンバイ」関連に関しては実際の結果を確認していません(また Event ID 566 — Microsoft-Windows-Kernel-Power も参考にしてください)。

バージョン情報

 以下のスクリプトはたぶん、良い具合に情報を取得できると思います。ただ Windows のバージョンによって、TargetState の各値の意味が変わったり、([xml]$Event.ToXml()).Event.EventData.Data での取得の可否が変わったりするみたいです。

PowerShell
# Get-ItemProperty "HKLM:\SOFTWARE\Microsoft\Windows NT\CurrentVersion"
Windows 10 22H2 (OS Build 19045)
Windows 11 23H2 (OS Build 22631)

スクリプト

PowerShell
# 現在時刻から遡る日数
$Days  = 7
# 取得するログの数
$Count = 1000
# 対象とするイベントID
$IDs   = @(6008, 1074, 507, 506, 42, 41, 29, 13, 12, 1)
# ビットフラグ(スリープ:1、休止状態:2、高速スタートアップ:4、モダンスタンバイ:8)
$Flag  = 7

$EventLog = Get-WinEvent -Oldest -MaxEvents $Count -FilterHashtable @{
    LogName   = "System"
    ID        = $IDs
    StartTime = (Get-Date).AddDays(-$Days)
} | Sort-Object TimeCreated

class band: Management.Automation.ArgumentTransformationAttribute {
    $mask; band($mask) { $this.mask = $mask }
    [object] Transform([Management.Automation.EngineIntrinsics]$EngineIntrinsics, $input) {
        return $input -band $this.mask
    }
}
function Detection {
    [CmdletBinding()]
    param(
        [ValidateSet(1,42)]
        [int]$Id,
        [ValidateRange(0,7)]
        [int]$TargetState,
        [ValidateRange(0,15)]
        [band(7)][int]$Flag = 7,
        [string]$ErrorMessage = ""
    )
    if (!$Flag) { return $ErrorMessage }

    switch ($Id) {
         1 { $Side =  0 }
        42 { $Side = 10 }
    }

    $Map = [ordered]@{
        4 = "復帰:  スリープ"
        5 = "復帰:  休止状態"
        6 = "起動:fast startup"

       14 = "移行:  スリープ"
       15 = "移行:  休止状態"
       16 = "終了:fast startup"
    }

    $Description = $ErrorMessage
    switch ($TargetState) {
        4 { if (($Flag -band 1) -ne 0) { $Description = $Map.($_+$Side) } }
        5 { if (($Flag -band 2) -ne 0) { $Description = $Map.($_+$Side) } }
        6 { if (($Flag -band 4) -ne 0) { $Description = $Map.($_+$Side) } } 
        default { if ($Id -eq 1) { $Description = "TS ${_}:スリープ解除" } }
    }

    return [string]$Description
}

$BlankBox = [PSCustomObject][ordered]@{
    発生日時  = $null
    識別    = $null
    イベント  = $null
    提供元   = $null
    メッセージ = $null
}
$BeginBox = [PSCustomObject][ordered]@{
    発生日時  = "#- - - - - - - - -#"
    識別    = 0x0
    イベント  = "#- - - - - - - - -"
    提供元   = "- - - - - - -#"
    メッセージ = $null
}
$CeaseBox = [PSCustomObject][ordered]@{
    発生日時  = "#- - - - - - - - -#"
    識別    = $null
    イベント  = "#- - - - - - - - -"
    提供元   = "- - - - - - -#"
    メッセージ = $null
}

$OutputList = [Collections.Generic.List[PSObject]]::new()
:HEAD foreach ($Event in $EventLog) {
    [int]$ID = 0; [string]$Description = ""
    switch ($Event.Id) {
        6008 {
            if ($Event.ProviderName -eq "EventLog") {
                $ID = $_; $Description = "前回: 不正な終了"
            }
        }
        1074 {
            if ($Event.ProviderName -eq "User32") {
                $ID = $_; $Description = "再起動/電源OFF"
            }
        }
         507 {
            if ($Event.ProviderName -eq "Microsoft-Windows-Kernel-Power") {
                if (($Flag -band 8) -ne 0) {
                    $ID = $_; $Description = "out Modern Standby"
                }
            }
        }
         506 {
            if ($Event.ProviderName -eq "Microsoft-Windows-Kernel-Power") {
                if (($Flag -band 8) -ne 0) {
                    $ID = $_; $Description = " in Modern Standby"
                }
            }
        }
          42 {
            if ($Event.ProviderName -eq "Microsoft-Windows-Kernel-Power") {
                $TargetState = ([xml]$Event.ToXml()).Event.EventData.Data.Where{$_.Name -eq "TargetState"}.InnerText
                $ID = $_; $Description = Detection $_ $TargetState $Flag
            }
        }
          41 {
            if ($Event.ProviderName -eq "Microsoft-Windows-Kernel-Power") {
                $ID = $_; $Description = "-予期しない電源断-"
            }
        }
          29 {
            if ($Event.ProviderName -eq "Microsoft-Windows-Kernel-Boot") {
                $ID = $_; $Description = "失敗:fast startup"
            }
        }
          13 {
            if ($Event.ProviderName -eq "Microsoft-Windows-Kernel-General") {
                $ID = $_; $Description = "OSシャットダウン"
            }
        }
          12 {
            if ($Event.ProviderName -eq "Microsoft-Windows-Kernel-General") {
                $ID = $_; $Description = "OSスタートアップ"
            }
        }
           1 {
            if ($Event.ProviderName -eq "Microsoft-Windows-Power-Troubleshooter") {
                $TargetState = ([xml]$Event.ToXml()).Event.EventData.Data.Where{$_.Name -eq "TargetState"}.InnerText
                $ID = $_; $Description = Detection $_ $TargetState $Flag
            }
        }
        default { $ID = $_; $Description = "Other references" }
    }; if ($Description -eq "") { continue HEAD }

    $Provider   = $Event.ProviderName -replace "^Microsoft-Windows-" -replace "^Power-"
    $Formatting = ($Event.Message -replace "\r?\n").Trim()

    if ($Description -match "OSスタートアップ|復帰:  休止状態|起動:fast startup") { 
        $OutputList.Add($BeginBox) 
    }
    $OutputList.Add([PSCustomObject][ordered]@{
        発生日時  = $Event.TimeCreated
        識別    = $ID
        イベント  = $Description
        提供元   = $Provider
        メッセージ = $Formatting
    })
    if ($Description -match "OSシャットダウン|移行:  休止状態|終了:fast startup") { 
        $OutputList.AddRange([PSObject[]]($CeaseBox, $BlankBox, $BlankBox)) 
    }
}
# $OutputList | Format-Table -AutoSize -Wrap
$OutputList | Format-Table 発生日時,識別,イベント,提供元
0
0
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
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?