#Requires -Version 5.1 # runner.ps1 — background entry point; called by the scheduled task. # All output goes to the log file only (no console window). $ErrorActionPreference = 'Stop' $script:InternalRoot = $PSScriptRoot # runner.ps1 lives in internal\ # ── Load shared libraries ───────────────────────────────────────────────────── . (Join-Path $InternalRoot 'lib\Logging.ps1') . (Join-Path $InternalRoot 'lib\NetworkUtils.ps1') . (Join-Path $InternalRoot 'lib\ToastHelper.ps1') . (Join-Path $InternalRoot 'lib\Config.ps1') Write-Log -Level Info -Message '────── Runner started ──────' -Feature 'Runner' # ── Load config and state ───────────────────────────────────────────────────── $config = Get-Config $state = Get-State # ── Run each enabled feature ────────────────────────────────────────────────── $featuresDir = Join-Path $InternalRoot 'features' foreach ($featureKey in $config.features.Keys) { $featureConfig = $config.features[$featureKey] if (-not $featureConfig['enabled']) { continue } $featureFile = Join-Path $featuresDir "$featureKey.ps1" if (-not (Test-Path $featureFile)) { Write-Log -Level Warn ` -Message "Feature '$featureKey' is enabled but '$featureFile' was not found. Skipping." ` -Feature 'Runner' continue } try { # Dot-source the feature — defines $FeatureMeta and Invoke-Feature in local scope. # Each iteration overwrites Invoke-Feature, which is fine because we call it immediately. $ErrorActionPreference = 'Stop' . $featureFile $featureState = if ($state.ContainsKey($featureKey)) { $state[$featureKey] } else { @{} } $updatedState = Invoke-Feature -Config $featureConfig -State $featureState if ($null -ne $updatedState) { $state[$featureKey] = $updatedState } Write-Log -Level Info -Message "Feature '$featureKey' completed." -Feature 'Runner' } catch { Write-Log -Level Error ` -Message "Feature '$featureKey' threw an unhandled exception: $_" ` -Feature 'Runner' # Continue to the next feature regardless of this failure } finally { $ErrorActionPreference = 'Stop' } } # ── Persist updated state ───────────────────────────────────────────────────── Save-State $state Write-Log -Level Info -Message '────── Runner finished ──────' -Feature 'Runner'