Private/Get-LastRunTime.ps1
|
function Get-LastRunTime { <# .SYNOPSIS Reads the last run timestamp from the Windows registry. .DESCRIPTION Retrieves the ISO-8601 timestamp stored at the specified registry path and key, parses it into a [DateTime], and returns it. Returns [DateTime]::MinValue when the registry path is absent, the key does not exist, or parsing fails — ensuring the boot guard treats any missing or corrupt state as a first-ever run. .PARAMETER RegistryPath The registry path to read from (e.g., 'HKCU:\Software\Envoke'). .PARAMETER RegistryKey The registry value name to read (e.g., 'LastRunTime'). .OUTPUTS [DateTime] — The stored timestamp, or [DateTime]::MinValue if unavailable. .NOTES Author: Aaron AlAnsari Created: 2026-02-25 The registry value is expected to be an ISO-8601 round-trip string produced by $dateTime.ToString('o'). This format is written by Set-LastRunTime. #> [CmdletBinding()] [OutputType([DateTime])] param ( [Parameter(Mandatory)] [string]$RegistryPath, [Parameter(Mandatory)] [string]$RegistryKey ) try { if (-not (Test-Path -Path $RegistryPath)) { return [DateTime]::MinValue } # Check if the specific registry value exists before reading. # Get-ItemProperty throws on a missing value name even when the path exists. $properties = Get-ItemProperty -Path $RegistryPath -ErrorAction Stop if ($null -eq $properties.$RegistryKey) { return [DateTime]::MinValue } $value = $properties.$RegistryKey return [DateTime]::Parse($value) } catch { Write-EnvkLog -Level 'ERROR' -Message "Failed to read registry: $RegistryPath\$RegistryKey" -ErrorRecord $_ return [DateTime]::MinValue } } |