Private/Set-LastRunTime.ps1

function Set-LastRunTime {
    <#
    .SYNOPSIS
        Writes a timestamp to the Windows registry as an ISO-8601 string.
 
    .DESCRIPTION
        Stores the given [DateTime] as an ISO-8601 round-trip string (format 'o') at the
        specified registry path and key. Creates the registry path (including any missing
        parent keys) if it does not yet exist, using New-Item -Force.
 
        Errors during write are logged at ERROR level but do not terminate the caller —
        a write failure means the next run will re-run (first-run behavior), which is a
        safe fallback.
 
    .PARAMETER RegistryPath
        The registry path to write to (e.g., 'HKCU:\Software\Envoke').
        Created if absent.
 
    .PARAMETER RegistryKey
        The registry value name to write (e.g., 'LastRunTime').
 
    .PARAMETER Time
        The [DateTime] value to store.
 
    .NOTES
        Author: Aaron AlAnsari
        Created: 2026-02-25
 
        The 'o' format specifier produces an ISO-8601 round-trip string that preserves
        full sub-second precision and timezone offset. This string is readable in the
        Registry Editor for manual inspection.
    #>


    [CmdletBinding(SupportsShouldProcess)]
    [OutputType([void])]
    param (
        [Parameter(Mandatory)]
        [string]$RegistryPath,

        [Parameter(Mandatory)]
        [string]$RegistryKey,

        [Parameter(Mandatory)]
        [DateTime]$Time
    )

    try {
        if (-not (Test-Path -Path $RegistryPath)) {
            New-Item -Path $RegistryPath -Force | Out-Null
        }

        Set-ItemProperty -Path $RegistryPath -Name $RegistryKey -Value $Time.ToString('o') -Force
        Write-EnvkLog -Level 'DEBUG' -Message "Registry updated: $RegistryKey = $($Time.ToString('o'))"
    }
    catch {
        Write-EnvkLog -Level 'ERROR' -Message "Failed to write registry: $RegistryPath\$RegistryKey" -ErrorRecord $_
    }
}