Files
PowershellAutomation/internal/features/DynamicLock.ps1
T
Arne Moerman 34ea1eb4b2 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.
2026-05-08 11:48:39 +02:00

100 lines
4.2 KiB
PowerShell

# ── Feature metadata ──────────────────────────────────────────────────────────
# $FeatureMeta is read by configure.ps1 for discovery, display, and config UI.
# Invoke-Feature is called by runner.ps1 on each scheduled cycle.
$FeatureMeta = @{
Name = 'DynamicLock'
Description = 'Disable Dynamic Lock while on a specific network; re-enable after disconnecting'
Settings = @(
@{
Key = 'network'
Label = 'DNS Suffix'
Type = 'string'
Default = 'h.arnemoerman.be'
Description = 'Connection-specific DNS suffix of the target network (e.g. h.arnemoerman.be)'
},
@{
Key = 'revertAfterMinutes'
Label = 'Revert After (minutes)'
Type = 'int'
Default = 10
Description = 'Re-enable Dynamic Lock after being disconnected for this many minutes'
}
)
}
# ── Feature implementation ────────────────────────────────────────────────────
function Invoke-Feature {
param(
[hashtable]$Config,
[hashtable]$State
)
if (-not $State) { $State = @{} }
if (-not $State.ContainsKey('isConnected')) { $State['isConnected'] = $false }
if (-not $State.ContainsKey('lastConnectedTime')) { $State['lastConnectedTime'] = $null }
if (-not $State.ContainsKey('dynamicLockDisabled')) { $State['dynamicLockDisabled'] = $false }
$regPath = 'HKCU:\SOFTWARE\Microsoft\Windows NT\CurrentVersion\Winlogon'
$isConnected = Test-DnsSuffixConnected -Suffix $Config['network']
if ($isConnected) {
$State['isConnected'] = $true
$State['lastConnectedTime'] = (Get-Date).ToString('o')
if (-not $State['dynamicLockDisabled']) {
if (-not (Test-Path $regPath)) {
New-Item -Path $regPath -Force | Out-Null
}
Set-ItemProperty -Path $regPath -Name 'EnableGoodbye' -Value 0 -Type DWord
$State['dynamicLockDisabled'] = $true
Write-Log -Level Info `
-Message "Dynamic Lock disabled (connected to '$($Config['network'])')" `
-Feature 'DynamicLock'
}
}
else {
$State['isConnected'] = $false
# If we disabled Dynamic Lock earlier, check whether we've been away long enough to re-enable
if ($State['dynamicLockDisabled'] -and $null -ne $State['lastConnectedTime']) {
try {
$lastConnected = [datetime]::Parse($State['lastConnectedTime'])
$minutesGone = ((Get-Date) - $lastConnected).TotalMinutes
if ($minutesGone -ge [double]$Config['revertAfterMinutes']) {
if (-not (Test-Path $regPath)) {
New-Item -Path $regPath -Force | Out-Null
}
Set-ItemProperty -Path $regPath -Name 'EnableGoodbye' -Value 1 -Type DWord
$State['dynamicLockDisabled'] = $false
Write-Log -Level Info `
-Message ("Dynamic Lock re-enabled (disconnected for {0} min, threshold {1} min)" -f
[int]$minutesGone, $Config['revertAfterMinutes']) `
-Feature 'DynamicLock'
}
else {
Write-Log -Level Info `
-Message ("Waiting to re-enable Dynamic Lock ({0}/{1} min elapsed)" -f
[int]$minutesGone, $Config['revertAfterMinutes']) `
-Feature 'DynamicLock'
}
}
catch {
Write-Log -Level Warn `
-Message "Could not parse lastConnectedTime '$($State['lastConnectedTime'])': $_" `
-Feature 'DynamicLock'
}
}
else {
Write-Log -Level Info `
-Message "Not connected to '$($Config['network'])'; Dynamic Lock state unchanged." `
-Feature 'DynamicLock'
}
}
return $State
}