Refactor undo handling in ExecuteParameter function to improve clarity and error handling

This commit is contained in:
Raphire
2026-03-23 20:47:32 +01:00
parent c15a18c376
commit b0125ddcd2

View File

@@ -6,32 +6,26 @@ function ExecuteParameter {
[string]$paramKey [string]$paramKey
) )
# Check if this feature has metadata in Features.json # Check if this feature exists in Features.json
$feature = $null $feature = $null
if ($script:Features.ContainsKey($paramKey)) { if ($script:Features.ContainsKey($paramKey)) {
$feature = $script:Features[$paramKey] $feature = $script:Features[$paramKey]
} }
# Check if undo is requested and if this feature supports undo
$undoChanges = $script:Params.ContainsKey('Undo') $undoChanges = $script:Params.ContainsKey('Undo')
$undoFeature = if ($undoChanges) { GetUndoFeatureForParam -paramKey $paramKey } else { $null }
# In global undo mode, skip any parameter that does not define undo metadata. if ($undoChanges) {
if ($undoChanges -and -not $undoFeature) { $undoFeature = GetUndoFeatureForParam -paramKey $paramKey
if ($null -eq $undoFeature) {
# This parameter doesn't support undo, so skip it
return return
} }
# If this feature was requested in undo mode, use undo metadata from Features.json.
if ($undoChanges -and $undoFeature) {
$undoRegFile = $undoFeature.RegistryUndoKey $undoRegFile = $undoFeature.RegistryUndoKey
$usesOfflineHive = $script:Params.ContainsKey("Sysprep") -or $script:Params.ContainsKey("User") $undoFolderPath = Join-Path $script:RegfilesPath (Join-Path 'Undo' $undoRegFile)
$undoFolderPath = if ($usesOfflineHive) {
Join-Path $script:RegfilesPath (Join-Path 'Sysprep' (Join-Path 'Undo' $undoRegFile))
}
else {
Join-Path $script:RegfilesPath (Join-Path 'Undo' $undoRegFile)
}
# Prefer dedicated Undo subfolder files when present, with fallback to legacy root location.
if (Test-Path $undoFolderPath) { if (Test-Path $undoFolderPath) {
$undoRegFile = Join-Path 'Undo' $undoRegFile $undoRegFile = Join-Path 'Undo' $undoRegFile
} }
@@ -40,7 +34,7 @@ function ExecuteParameter {
return return
} }
# If feature has RegistryKey and ApplyText, use dynamic ImportRegistryFile # If feature has RegistryKey and ApplyText, dynamically import the registry file for this feature
if ($feature -and $feature.RegistryKey -and $feature.ApplyText) { if ($feature -and $feature.RegistryKey -and $feature.ApplyText) {
ImportRegistryFile "> $($feature.ApplyText)" $feature.RegistryKey ImportRegistryFile "> $($feature.ApplyText)" $feature.RegistryKey
@@ -194,13 +188,8 @@ function ExecuteAllChanges {
} }
} }
# If no undo-capable changes remain, disable explorer restart for this run.
if ($undoChanges -and $actionableKeys.Count -eq 0) { if ($undoChanges -and $actionableKeys.Count -eq 0) {
if (-not $script:Params.ContainsKey('NoRestartExplorer')) { throw "Undo was requested but none of the selected parameters support undo. No changes were reverted."
$script:Params['NoRestartExplorer'] = $true
}
Write-Warning "None of the selected changes can be undone automatically."
Write-Host ""
} }
$totalSteps = $actionableKeys.Count $totalSteps = $actionableKeys.Count