Private/Test-BootGuard.ps1
|
function Test-BootGuard { <# .SYNOPSIS Determines whether the current invocation should proceed based on boot session state. .DESCRIPTION Coordinates Get-BootTime and Get-LastRunTime to implement the boot guard: - If no timestamp is stored (first-ever run), approves the run ($true). - If the stored timestamp is before the current boot time (new boot), approves ($true). - If the stored timestamp is at or after the current boot time (already ran this session), rejects the run ($false). The function logs both timestamps at INFO level so the decision is visible in the module log for diagnostic purposes. .PARAMETER RegistryPath The registry path where the last run timestamp is stored (e.g., 'HKCU:\Software\Envoke'). .PARAMETER RegistryKey The registry value name for the last run timestamp (e.g., 'LastRunTime'). .OUTPUTS [bool] — $true to approve the run; $false to reject it. .NOTES Author: Aaron AlAnsari Created: 2026-02-25 The comparison uses strictly-less-than ($lastRunTime -lt $bootTime). A timestamp equal to the boot time is treated as a same-session run and rejected. This is the correct behavior because Set-LastRunTime is called after the boot guard passes, meaning the stored time will always postdate the boot time in normal operation. #> [CmdletBinding()] [OutputType([bool])] param ( [Parameter(Mandatory)] [string]$RegistryPath, [Parameter(Mandatory)] [string]$RegistryKey ) $bootTime = Get-BootTime $lastRunTime = Get-LastRunTime -RegistryPath $RegistryPath -RegistryKey $RegistryKey Write-EnvkLog -Level 'INFO' -Message "Boot time: $bootTime | Last run: $lastRunTime" if ($lastRunTime -lt $bootTime) { Write-EnvkLog -Level 'INFO' -Message 'Boot guard: approving run — last run is before current boot' return $true } Write-EnvkLog -Level 'INFO' -Message 'Boot guard: rejecting run — already ran this boot session' return $false } |