34ea1eb4b2
- Implemented DefaultBrowser feature to notify users when the default browser does not match the configured app. - Added DynamicLock feature to disable Dynamic Lock while connected to a specific network and re-enable it after disconnecting. - Created SandwichReminder feature to prompt users to order a sandwich during work hours based on network and time settings. Introduced helper libraries for configuration, elevation, logging, network utilities, and toast notifications. - Config.ps1: Added functions for reading and writing configuration and state files. - Elevation.ps1: Added functions to check for administrator privileges and request elevation. - Logging.ps1: Implemented a shared logging utility for consistent logging across features. - NetworkUtils.ps1: Added a function to check for DNS suffix connectivity. - ToastHelper.ps1: Created a helper for displaying Windows toast notifications. Implemented runner.ps1 as the main entry point for executing features based on configuration.
70 lines
2.9 KiB
PowerShell
70 lines
2.9 KiB
PowerShell
#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'
|
|
|