Add new features: DefaultBrowser, DynamicLock, SandwichReminder
- 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.
This commit is contained in:
@@ -0,0 +1,38 @@
|
||||
# Logging.ps1 — shared logging utility
|
||||
# Requires $InternalRoot to be defined in the calling script's scope before dot-sourcing.
|
||||
|
||||
function Write-Log {
|
||||
[CmdletBinding()]
|
||||
param(
|
||||
[ValidateSet('Info', 'Warn', 'Error')]
|
||||
[string]$Level = 'Info',
|
||||
|
||||
[Parameter(Mandatory)]
|
||||
[string]$Message,
|
||||
|
||||
[string]$Feature = 'General'
|
||||
)
|
||||
|
||||
$logDir = Join-Path $InternalRoot 'data\logs'
|
||||
if (-not (Test-Path $logDir)) {
|
||||
New-Item -ItemType Directory -Path $logDir -Force | Out-Null
|
||||
}
|
||||
|
||||
$logFile = Join-Path $logDir "automation-$(Get-Date -Format 'yyyy-MM-dd').log"
|
||||
$timestamp = Get-Date -Format 'yyyy-MM-dd HH:mm:ss'
|
||||
$levelPad = $Level.ToUpper().PadRight(5)
|
||||
$line = "[$timestamp] [$levelPad] [$Feature] $Message"
|
||||
|
||||
try {
|
||||
Add-Content -Path $logFile -Value $line -Encoding UTF8 -ErrorAction Stop
|
||||
}
|
||||
catch {
|
||||
# Logging must never crash the caller — silently ignore write failures
|
||||
}
|
||||
|
||||
# Rotate: remove logs older than 7 days
|
||||
Get-ChildItem -Path $logDir -Filter 'automation-*.log' -ErrorAction SilentlyContinue |
|
||||
Where-Object { $_.LastWriteTime -lt (Get-Date).AddDays(-7) } |
|
||||
Remove-Item -Force -ErrorAction SilentlyContinue
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user