diff --git a/Scripts/AppRemoval/Invoke-ForceRemoveEdge.ps1 b/Scripts/AppRemoval/Invoke-ForceRemoveEdge.ps1 index 48cca13..2573975 100644 --- a/Scripts/AppRemoval/Invoke-ForceRemoveEdge.ps1 +++ b/Scripts/AppRemoval/Invoke-ForceRemoveEdge.ps1 @@ -5,32 +5,42 @@ function Invoke-ForceRemoveEdge { if ($script:Params.ContainsKey("WhatIf")) { Write-Host "[WhatIf] Forcefully uninstall Microsoft Edge" -ForegroundColor Cyan - Write-Host "" - return + return $true } - Write-Host "> Forcefully uninstalling Microsoft Edge..." + try { + Write-Host "> Forcefully uninstalling Microsoft Edge..." - $regView = [Microsoft.Win32.RegistryView]::Registry32 - $hklm = [Microsoft.Win32.RegistryKey]::OpenBaseKey([Microsoft.Win32.RegistryHive]::LocalMachine, $regView) - $hklm.CreateSubKey('SOFTWARE\Microsoft\EdgeUpdateDev').SetValue('AllowUninstall', '') + $regView = [Microsoft.Win32.RegistryView]::Registry32 + $hklm = [Microsoft.Win32.RegistryKey]::OpenBaseKey([Microsoft.Win32.RegistryHive]::LocalMachine, $regView) + $hklm.CreateSubKey('SOFTWARE\Microsoft\EdgeUpdateDev').SetValue('AllowUninstall', '') - # Create stub (This somehow allows uninstalling Edge) - $edgeStub = "$env:SystemRoot\SystemApps\Microsoft.MicrosoftEdge_8wekyb3d8bbwe" - New-Item $edgeStub -ItemType Directory | Out-Null - New-Item "$edgeStub\MicrosoftEdge.exe" | Out-Null + # Create stub (This somehow allows uninstalling Edge) + $edgeStub = "$env:SystemRoot\SystemApps\Microsoft.MicrosoftEdge_8wekyb3d8bbwe" + New-Item $edgeStub -ItemType Directory -Force -ErrorAction Stop | Out-Null + New-Item "$edgeStub\MicrosoftEdge.exe" -ItemType File -Force -ErrorAction Stop | Out-Null # Remove edge - $uninstallRegKey = $hklm.OpenSubKey('SOFTWARE\Microsoft\Windows\CurrentVersion\Uninstall\Microsoft Edge') - if ($null -ne $uninstallRegKey) { + $uninstallRegKey = $hklm.OpenSubKey('SOFTWARE\Microsoft\Windows\CurrentVersion\Uninstall\Microsoft Edge') + if ($null -eq $uninstallRegKey) { + Write-Host "Unable to forcefully uninstall Microsoft Edge, uninstaller could not be found" -ForegroundColor Red + return $false + } + Write-Host "Running uninstaller..." $uninstallString = $uninstallRegKey.GetValue('UninstallString') + ' --force-uninstall' - Invoke-NonBlocking -ScriptBlock { + $exitCode = Invoke-NonBlocking -ScriptBlock { param($cmd) - Start-Process cmd.exe "/c $cmd" -WindowStyle Hidden -Wait + $process = Start-Process cmd.exe "/c $cmd" -WindowStyle Hidden -Wait -PassThru + return $process.ExitCode } -ArgumentList $uninstallString + if ($exitCode -ne 0) { + Write-Warning "Microsoft Edge uninstaller failed with exit code $exitCode." + return $false + } Write-Host "Removing leftover files..." + $cleanupSucceeded = $true $edgePaths = @( "$env:ProgramData\Microsoft\Windows\Start Menu\Programs\Microsoft Edge.lnk", @@ -44,22 +54,81 @@ function Invoke-ForceRemoveEdge { foreach ($path in $edgePaths) { if (Test-Path -Path $path) { - Remove-Item -Path $path -Force -Recurse -ErrorAction SilentlyContinue - Write-Host " Removed $path" -ForegroundColor DarkGray + try { + Remove-Item -Path $path -Force -Recurse -ErrorAction Stop + Write-Host " Removed $path" -ForegroundColor DarkGray + } + catch { + Write-Warning "Failed to remove Edge leftover '$path': $($_.Exception.Message)" + $cleanupSucceeded = $false + } } } Write-Host "Cleaning up registry..." + $registryCleanupSucceeded = $true - # Remove MS Edge from autostart - reg delete "HKEY_CURRENT_USER\Software\Microsoft\Windows\CurrentVersion\Run" /v "MicrosoftEdgeAutoLaunch_A9F6DCE4ABADF4F51CF45CD7129E3C6C" /f *>$null - reg delete "HKEY_CURRENT_USER\Software\Microsoft\Windows\CurrentVersion\Run" /v "Microsoft Edge Update" /f *>$null - reg delete "HKEY_CURRENT_USER\Software\Microsoft\Windows\CurrentVersion\Explorer\StartupApproved\Run" /v "MicrosoftEdgeAutoLaunch_A9F6DCE4ABADF4F51CF45CD7129E3C6C" /f *>$null - reg delete "HKEY_CURRENT_USER\Software\Microsoft\Windows\CurrentVersion\Explorer\StartupApproved\Run" /v "Microsoft Edge Update" /f *>$null + # Remove MS Edge from autostart. Missing values are already-clean state, + # while failures to inspect or remove an existing value are reported. + $autostartValues = @( + @{ Path = 'HKCU:\Software\Microsoft\Windows\CurrentVersion\Run'; Name = 'MicrosoftEdgeAutoLaunch_A9F6DCE4ABADF4F51CF45CD7129E3C6C' }, + @{ Path = 'HKCU:\Software\Microsoft\Windows\CurrentVersion\Run'; Name = 'Microsoft Edge Update' }, + @{ Path = 'HKCU:\Software\Microsoft\Windows\CurrentVersion\Explorer\StartupApproved\Run'; Name = 'MicrosoftEdgeAutoLaunch_A9F6DCE4ABADF4F51CF45CD7129E3C6C' }, + @{ Path = 'HKCU:\Software\Microsoft\Windows\CurrentVersion\Explorer\StartupApproved\Run'; Name = 'Microsoft Edge Update' } + ) + foreach ($autostartValue in $autostartValues) { + if (-not (Remove-EdgeAutostartValue -Path $autostartValue.Path -Name $autostartValue.Name)) { + $registryCleanupSucceeded = $false + } + } + + if (-not $cleanupSucceeded -or -not $registryCleanupSucceeded) { + Write-Warning "Microsoft Edge was uninstalled, but some leftover files or autostart entries could not be removed." + return $false + } Write-Host "Microsoft Edge was uninstalled" + return $true } - else { - Write-Host "Unable to forcefully uninstall Microsoft Edge, uninstaller could not be found" -ForegroundColor Red + catch { + Write-Warning "Failed to forcefully uninstall Microsoft Edge: $($_.Exception.Message)" + return $false + } + finally { + if ($uninstallRegKey) { $uninstallRegKey.Dispose() } + if ($hklm) { $hklm.Dispose() } + } +} + +function Remove-EdgeAutostartValue { + param( + [Parameter(Mandatory)] + [string]$Path, + [Parameter(Mandatory)] + [string]$Name + ) + + try { + $properties = Get-ItemProperty -Path $Path -ErrorAction Stop + } + catch [System.Management.Automation.ItemNotFoundException] { + return $true + } + catch { + Write-Warning "Failed to inspect Edge autostart entry '$Path\$Name': $($_.Exception.Message)" + return $false + } + + if (-not $properties.PSObject.Properties[$Name]) { + return $true + } + + try { + Remove-ItemProperty -Path $Path -Name $Name -ErrorAction Stop + return $true + } + catch { + Write-Warning "Failed to remove Edge autostart entry '$Path\$Name': $($_.Exception.Message)" + return $false } } diff --git a/Scripts/AppRemoval/Remove-SelectedApps.ps1 b/Scripts/AppRemoval/Remove-SelectedApps.ps1 index ea06da8..19f4f13 100644 --- a/Scripts/AppRemoval/Remove-SelectedApps.ps1 +++ b/Scripts/AppRemoval/Remove-SelectedApps.ps1 @@ -29,10 +29,10 @@ function Remove-SelectedApps { Write-Host "[WhatIf] Remove App Package: $app" -ForegroundColor Cyan } - Write-Host "" - return + return $true } + $failuresBefore = $script:AppRemovalFailures $targetUser = Get-TargetUserForAppRemoval $appCount = @($appsList).Count $appIndex = 0 @@ -42,7 +42,7 @@ function Remove-SelectedApps { $wingetRemovalFailures = @{} Foreach ($app in $appsList) { - if ($script:CancelRequested) { return } + if ($script:CancelRequested) { return $false } $appIndex++ @@ -53,9 +53,9 @@ function Remove-SelectedApps { Write-Host "Removing $app" if ((Get-AppRemovalMethod $app) -eq 'WinGet') { - if (-not (Remove-WinGetApp -app $app)) { - $wingetRemovalFailures[$app] = $true - } + # WinGet exit codes are not a reliable removal outcome. The single + # post-removal inventory check below determines final success. + $null = Remove-WinGetApp -app $app $wingetRemovedApps += $app } else { @@ -66,17 +66,20 @@ function Remove-SelectedApps { } if ($script:CancelRequested) { - Write-Host "" - return + return $false } # Check whether any winget-removed apps are still present, and report errors for each one. if ($wingetRemovedApps.Count -gt 0) { $postRemovalList = if ($script:WingetInstalled) { Get-WingetInstalledApps -TimeOut 10 -NonBlocking } else { $null } $edgeForceRemoveRequested = $false + $edgeForceRemoveSucceeded = $false if ($null -eq $postRemovalList) { $script:AppRemovalVerificationUnavailable = $true + foreach ($app in $wingetRemovedApps) { + $wingetRemovalFailures[$app] = $true + } } else { foreach ($app in $wingetRemovedApps) { @@ -87,8 +90,11 @@ function Remove-SelectedApps { if ($edgeIds -contains $app) { Write-Host "Unable to uninstall Microsoft Edge via WinGet" -ForegroundColor Red if (-not $edgeForceRemoveRequested) { - Request-EdgeForceRemove $edgeForceRemoveRequested = $true + $edgeForceRemoveSucceeded = Request-EdgeForceRemove + } + if ($edgeForceRemoveSucceeded) { + continue } } else { @@ -101,7 +107,7 @@ function Remove-SelectedApps { $script:AppRemovalFailures += $wingetRemovalFailures.Count - Write-Host "" + return ($script:AppRemovalFailures -eq $failuresBefore) } <# @@ -133,20 +139,30 @@ function Remove-WinGetApp { $uninstallSucceeded = $true try { - $uninstallSucceeded = Invoke-NonBlocking -ScriptBlock { + $uninstallResult = Invoke-NonBlocking -ScriptBlock { param($appId) - $null = & winget uninstall --accept-source-agreements --disable-interactivity --id $appId 2>&1 - return $true + $output = @(& winget uninstall --accept-source-agreements --disable-interactivity --id $appId 2>&1) + $exitCode = $LASTEXITCODE + return [PSCustomObject]@{ + Success = ($exitCode -eq 0) + ExitCode = $exitCode + Output = $output + } } -ArgumentList $app -TimeoutSeconds $TimeoutSeconds - $uninstallSucceeded = [bool]$uninstallSucceeded + $uninstallSucceeded = [bool]($uninstallResult -and $uninstallResult.Success) + Write-WinGetUninstallOutput -Output $(if ($uninstallResult) { $uninstallResult.Output } else { $null }) + if (-not $uninstallSucceeded) { + $exitCode = if ($uninstallResult) { $uninstallResult.ExitCode } else { 'unknown' } + Write-Verbose "WinGet uninstall for $app returned exit code $exitCode. The post-removal inventory check will determine whether the app is still installed." + } } catch { $uninstallSucceeded = $false if ($_.Exception.Message -like 'Operation timed out after *') { - Write-Error "WinGet uninstall for $app did not complete within $TimeoutSeconds seconds: $_" + Write-Verbose "WinGet uninstall for $app did not complete within $TimeoutSeconds seconds: $_" } else { - Write-Error "WinGet uninstall for $app failed: $_" + Write-Verbose "WinGet uninstall for $app failed: $_" } } @@ -163,6 +179,21 @@ function Remove-WinGetApp { return ($uninstallSucceeded -and $scheduleSucceeded) } +function Write-WinGetUninstallOutput { + param( + [object[]]$Output + ) + + foreach ($line in @($Output)) { + if ($null -eq $line) { continue } + + $lineText = if ($line -is [System.Management.Automation.ErrorRecord]) { $line.Exception.Message } else { $line.ToString() } + if ([string]::IsNullOrWhiteSpace($lineText)) { continue } + + Write-Verbose $lineText + } +} + <# .SYNOPSIS Removes an app via Remove-AppxPackage / Remove-ProvisionedAppxPackage. @@ -283,13 +314,15 @@ function Request-EdgeForceRemove { $result = Show-MessageBox -Message 'Unable to uninstall Microsoft Edge via WinGet. Would you like to forcefully uninstall it? NOT RECOMMENDED!' -Title 'Force Uninstall Microsoft Edge?' -Button 'YesNo' -Icon 'Warning' if ($result -eq 'Yes') { Write-Host "" - Invoke-ForceRemoveEdge + return (Invoke-ForceRemoveEdge) } } elseif ($(Read-Host -Prompt "Would you like to forcefully uninstall Microsoft Edge? NOT RECOMMENDED! (y/n)") -eq 'y') { Write-Host "" - Invoke-ForceRemoveEdge + return (Invoke-ForceRemoveEdge) } + + return $false } <# diff --git a/Scripts/Features/Import-RegistryFile.ps1 b/Scripts/Features/Import-RegistryFile.ps1 index 1c17d98..92403f6 100644 --- a/Scripts/Features/Import-RegistryFile.ps1 +++ b/Scripts/Features/Import-RegistryFile.ps1 @@ -12,19 +12,15 @@ function Import-RegistryFile { if (-not (Test-Path $regFilePath)) { $errorMessage = "Unable to find registry file: $path ($regFilePath)" - $script:RegistryImportFailures++ Write-Host "Error: $errorMessage" -ForegroundColor Red - Write-Host "" - throw $errorMessage + return $false } $importScript = { param($targetRegFilePath, $hiveContext) if ($script:Params.ContainsKey("WhatIf")) { - Invoke-RegistryOperationsFromRegFile -RegFilePath $targetRegFilePath - Write-Host "" - return + return (Invoke-RegistryOperationsFromRegFile -RegFilePath $targetRegFilePath) } # When the target user's hive is already loaded under their SID, the .reg file's @@ -33,10 +29,11 @@ function Import-RegistryFile { $usePowerShellFallbackOnly = $hiveContext -and [bool]$hiveContext.WasAlreadyLoaded if ($usePowerShellFallbackOnly) { - Invoke-RegistryOperationsFromRegFile -RegFilePath $targetRegFilePath - Write-Host "The operation completed successfully via PowerShell registry writer." - Write-Host "" - return + $fallbackSucceeded = Invoke-RegistryOperationsFromRegFile -RegFilePath $targetRegFilePath + if ($fallbackSucceeded) { + Write-Host "The operation completed successfully via PowerShell registry writer." + } + return $fallbackSucceeded } $regResult = Invoke-NonBlocking -ScriptBlock { @@ -89,26 +86,29 @@ function Import-RegistryFile { if (-not $hasSuccess) { $details = if ($regResult.Error) { $regResult.Error } else { "Exit code: $($regResult.ExitCode)" } Write-Warning "reg import failed for '$path'. Falling back to PowerShell registry writer. Details: $details" - Invoke-RegistryOperationsFromRegFile -RegFilePath $targetRegFilePath - Write-Host "The operation completed successfully via PowerShell registry writer." + $fallbackSucceeded = Invoke-RegistryOperationsFromRegFile -RegFilePath $targetRegFilePath + if ($fallbackSucceeded) { + Write-Host "The operation completed successfully via PowerShell registry writer." + } + return $fallbackSucceeded } - Write-Host "" + return $true } try { if ($usesOfflineHive) { # Sysprep targets Default user, User targets the specified user. Logged-in users already have their hive mounted under HKU\. $targetUserName = if ($script:Params.ContainsKey("Sysprep")) { "Default" } else { $script:Params.Item("User") } - Invoke-WithTargetUserHive -TargetUserName $targetUserName -ScriptBlock $importScript -ArgumentObject $regFilePath -PassHiveContext + $succeeded = Invoke-WithTargetUserHive -TargetUserName $targetUserName -ScriptBlock $importScript -ArgumentObject $regFilePath -PassHiveContext } else { - & $importScript $regFilePath $null + $succeeded = & $importScript $regFilePath $null } + return [bool]$succeeded } catch { - $script:RegistryImportFailures++ Write-Host $_.Exception.Message -ForegroundColor Red - Write-Host "" + return $false } } diff --git a/Scripts/Features/Invoke-Changes.ps1 b/Scripts/Features/Invoke-Changes.ps1 index 02015b3..3e496dc 100644 --- a/Scripts/Features/Invoke-Changes.ps1 +++ b/Scripts/Features/Invoke-Changes.ps1 @@ -7,7 +7,9 @@ - Registry-backed: imports the .reg file via Import-RegistryFile, then runs any post-import side effects (e.g., removing companion app packages). - Custom logic: app removal, Windows optional features, start menu - replacement, and other special-case features. + replacement, and other special-case features. Returns $true when the + feature completes successfully; otherwise writes a warning and returns + $false. #> function Invoke-FeatureApply { param( @@ -15,30 +17,32 @@ function Invoke-FeatureApply { [string]$FeatureId ) - # Resolve feature metadata from Features.json - $feature = $script:Features[$FeatureId] - $applyText = $feature.ApplyText + try { + # Resolve feature metadata from Features.json + $feature = $script:Features[$FeatureId] + $applyText = $feature.ApplyText - # ---- Registry-backed features: import .reg file, then handle side effects ---- + # ---- Registry-backed features: import .reg file, then handle additional tasks ---- if ($feature.RegistryKey) { - Import-RegistryFile "> $applyText..." $feature.RegistryKey + if (-not (Import-RegistryFile "> $applyText..." $feature.RegistryKey)) { + return $false + } - # Post-import side effects for specific features switch ($FeatureId) { 'DisableBing' { # Also remove the app package for Bing search - Remove-SelectedApps @('Microsoft.BingSearch') + return (Remove-SelectedApps @('Microsoft.BingSearch')) } 'DisableCopilot' { # Also remove the app packages for Copilot - Remove-SelectedApps @('Microsoft.Copilot', 'XP9CXNGPPJ97XX') + return (Remove-SelectedApps @('Microsoft.Copilot', 'XP9CXNGPPJ97XX')) } 'DisableTelemetry' { # Also disable telemetry scheduled tasks - Disable-TelemetryScheduledTasks + return (Disable-TelemetryScheduledTasks) } } - return + return $true } # ---- Custom features (no registry backing, or special handling required) ---- @@ -49,30 +53,25 @@ function Invoke-FeatureApply { if ($appsList.Count -eq 0) { Write-Host "No valid apps were selected for removal" -ForegroundColor Yellow - Write-Host "" - return + return $true } Write-Host "$($appsList.Count) apps selected for removal" - Remove-SelectedApps $appsList - return + return (Remove-SelectedApps $appsList) } 'RemoveGamingApps' { $appsList = @('Microsoft.GamingApp', 'Microsoft.XboxGameOverlay', 'Microsoft.XboxGamingOverlay') Write-Host "> $applyText..." - Remove-SelectedApps $appsList - return + return (Remove-SelectedApps $appsList) } 'RemoveHPApps' { $appsList = @('AD2F1837.HPAIExperienceCenter', 'AD2F1837.HPJumpStarts', 'AD2F1837.HPPCHardwareDiagnosticsWindows', 'AD2F1837.HPPowerManager', 'AD2F1837.HPPrivacySettings', 'AD2F1837.HPSupportAssistant', 'AD2F1837.HPSureShieldAI', 'AD2F1837.HPSystemInformation', 'AD2F1837.HPQuickDrop', 'AD2F1837.HPWorkWell', 'AD2F1837.myHP', 'AD2F1837.HPDesktopSupportUtilities', 'AD2F1837.HPQuickTouch', 'AD2F1837.HPEasyClean', 'AD2F1837.HPConnectedMusic', 'AD2F1837.HPFileViewer', 'AD2F1837.HPRegistration', 'AD2F1837.HPWelcome', 'AD2F1837.HPConnectedPhotopoweredbySnapfish', 'AD2F1837.HPPrinterControl') Write-Host "> $applyText..." - Remove-SelectedApps $appsList - return + return (Remove-SelectedApps $appsList) } 'ForceRemoveEdge' { Write-Host "> $applyText..." - Invoke-ForceRemoveEdge - return + return (Invoke-ForceRemoveEdge) } 'DisableWidgets' { Write-Host "> $applyText..." @@ -81,76 +80,75 @@ function Invoke-FeatureApply { Get-Process *Widget* -ErrorAction SilentlyContinue | Stop-Process -Force -ErrorAction SilentlyContinue } - Remove-SelectedApps @('Microsoft.StartExperiencesApp','MicrosoftWindows.Client.WebExperience','Microsoft.WidgetsPlatformRuntime') - return + return (Remove-SelectedApps @('Microsoft.StartExperiencesApp','MicrosoftWindows.Client.WebExperience','Microsoft.WidgetsPlatformRuntime')) } 'EnableWindowsSandbox' { Write-Host "> $applyText..." - Enable-WindowsFeature "Containers-DisposableClientVM" - Write-Host "" - return + return (Enable-WindowsFeature "Containers-DisposableClientVM") } 'EnableWindowsSubsystemForLinux' { Write-Host "> $applyText..." - Enable-WindowsFeature "VirtualMachinePlatform" - Enable-WindowsFeature "Microsoft-Windows-Subsystem-Linux" - Write-Host "" - return + if (-not (Enable-WindowsFeature "VirtualMachinePlatform")) { return $false } + return (Enable-WindowsFeature "Microsoft-Windows-Subsystem-Linux") } 'ClearStart' { Write-Host "> $applyText for user $(Get-UserName)..." $startMenuBinFile = Get-StartMenuBinPathForUser -UserName (Get-UserName) if (-not [string]::IsNullOrWhiteSpace($startMenuBinFile)) { - Replace-StartMenu -startMenuBinFile $startMenuBinFile + return (Replace-StartMenu -startMenuBinFile $startMenuBinFile) } - Write-Host "" - return + Write-Warning "Unable to apply '$applyText': the Start menu path for user $(Get-UserName) could not be resolved." + return $false } 'ReplaceStart' { Write-Host "> $applyText for user $(Get-UserName)..." $startMenuBinFile = Get-StartMenuBinPathForUser -UserName (Get-UserName) if (-not [string]::IsNullOrWhiteSpace($startMenuBinFile)) { - Replace-StartMenu -startMenuBinFile $startMenuBinFile -startMenuTemplate $script:Params.Item("ReplaceStart") + return (Replace-StartMenu -startMenuBinFile $startMenuBinFile -startMenuTemplate $script:Params.Item("ReplaceStart")) } - Write-Host "" - return + Write-Warning "Unable to apply '$applyText': the Start menu path for user $(Get-UserName) could not be resolved." + return $false } 'ClearStartAllUsers' { - Replace-StartMenuForAllUsers - return + return (Replace-StartMenuForAllUsers) } 'ReplaceStartAllUsers' { - Replace-StartMenuForAllUsers -startMenuTemplate $script:Params.Item("ReplaceStartAllUsers") - return + return (Replace-StartMenuForAllUsers -startMenuTemplate $script:Params.Item("ReplaceStartAllUsers")) } 'DisableStoreSearchSuggestions' { if ($script:Params.ContainsKey("Sysprep")) { Write-Host "> Disabling Microsoft Store search suggestions in the start menu for all users..." - Set-StoreSearchSuggestionsDisabledForAllUsers - Write-Host "" - return + return (Set-StoreSearchSuggestionsDisabledForAllUsers) } Write-Host "> Disabling Microsoft Store search suggestions for user $(Get-UserName)..." $storeDb = Get-StoreAppsDatabasePathForUser -UserName (Get-UserName) if ($storeDb) { - Set-StoreSearchSuggestionsDisabled -StoreAppsDatabase $storeDb + return (Set-StoreSearchSuggestionsDisabled -StoreAppsDatabase $storeDb) } - Write-Host "" - return + Write-Warning "Unable to disable Microsoft Store search suggestions because the Store database for user $(Get-UserName) could not be resolved." + return $false } } + } + catch { + Write-Warning "Failed to apply '$applyText': $($_.Exception.Message)" + return $false + } + + Write-Warning "Unknown feature '$FeatureId' could not be applied." + return $false } <# .SYNOPSIS - Undoes a single feature that has no RegistryUndoKey. + Undoes a single feature. .DESCRIPTION - Handles undo for features that require custom logic rather than a simple - .reg file import. Features with a RegistryUndoKey are handled directly - via Import-RegistryFile in Invoke-UndoFeatures. + Handles registry-backed undo imports and custom undo logic. Returns + $true when the requested undo succeeds; otherwise writes a warning and + returns $false. #> function Invoke-FeatureUndo { param( @@ -159,43 +157,65 @@ function Invoke-FeatureUndo { ) $feature = if ($script:Features.ContainsKey($FeatureId)) { $script:Features[$FeatureId] } else { $null } + if (-not $feature) { + Write-Warning "Unknown feature '$FeatureId' could not be undone." + return $false + } - switch ($FeatureId) { - 'DisableStoreSearchSuggestions' { - if ($script:Params.ContainsKey('Sysprep')) { - Write-Host "> Re-enabling Microsoft Store search suggestions in the start menu for all users..." - Set-StoreSearchSuggestionsEnabledForAllUsers - Write-Host "" - return + $undoText = if ($feature.ApplyUndoText) { $feature.ApplyUndoText } elseif ($feature.UndoLabel) { $feature.UndoLabel } else { $FeatureId } + + try { + # ---- Registry-backed features: import undo data, then handle additional tasks ---- + if ($feature.RegistryUndoKey) { + if (-not (Import-RegistryFile "> $undoText" (Resolve-UndoRegFilePath $feature.RegistryUndoKey))) { + return $false } - Write-Host "> Re-enabling Microsoft Store search suggestions for user $(Get-UserName)..." - $storeDb = Get-StoreAppsDatabasePathForUser -UserName (Get-UserName) - if ($storeDb) { - Set-StoreSearchSuggestionsEnabled -StoreAppsDatabase $storeDb + switch ($FeatureId) { + 'DisableTelemetry' { + # Also re-enable telemetry scheduled tasks. + return (Enable-TelemetryScheduledTasks) + } } - Write-Host "" - return + + return $true } - 'EnableWindowsSandbox' { - Write-Host "> $($feature.ApplyUndoText)..." - Disable-WindowsFeature 'Containers-DisposableClientVM' - Write-Host "" - return - } - 'EnableWindowsSubsystemForLinux' { - Write-Host "> $($feature.ApplyUndoText)..." - Disable-WindowsFeature 'Microsoft-Windows-Subsystem-Linux' - Disable-WindowsFeature 'VirtualMachinePlatform' - Write-Host "" - return - } - 'DisableTelemetry' { - # Also re-enable telemetry scheduled tasks - Enable-TelemetryScheduledTasks - return + + # ---- Custom undo features (no registry backing) ---- + switch ($FeatureId) { + 'DisableStoreSearchSuggestions' { + if ($script:Params.ContainsKey('Sysprep')) { + Write-Host "> Re-enabling Microsoft Store search suggestions in the start menu for all users..." + return (Set-StoreSearchSuggestionsEnabledForAllUsers) + } + + Write-Host "> Re-enabling Microsoft Store search suggestions for user $(Get-UserName)..." + $storeDb = Get-StoreAppsDatabasePathForUser -UserName (Get-UserName) + if ($storeDb) { + return (Set-StoreSearchSuggestionsEnabled -StoreAppsDatabase $storeDb) + } + Write-Warning "Unable to re-enable Microsoft Store search suggestions because the Store database for user $(Get-UserName) could not be resolved." + return $false + } + 'EnableWindowsSandbox' { + Write-Host "> $($feature.ApplyUndoText)..." + return (Disable-WindowsFeature 'Containers-DisposableClientVM') + } + 'EnableWindowsSubsystemForLinux' { + Write-Host "> $($feature.ApplyUndoText)..." + if (-not (Disable-WindowsFeature 'Microsoft-Windows-Subsystem-Linux')) { return $false } + return (Disable-WindowsFeature 'VirtualMachinePlatform') + } } } + + catch { + Write-Warning "Failed to undo '$undoText': $($_.Exception.Message)" + return $false + } + + Write-Warning "Feature '$FeatureId' does not support undo." + return $false } @@ -251,7 +271,11 @@ function Invoke-ApplyFeatures { & $script:ApplyProgressCallback $step $TotalSteps $displayName } - Invoke-FeatureApply -FeatureId $featureId + if (-not (Invoke-FeatureApply -FeatureId $featureId)) { + $script:ApplyFeatureFailures++ + $script:FeatureFailures++ + } + Write-Host "" $step++ } } @@ -262,9 +286,8 @@ function Invoke-ApplyFeatures { Undoes a list of features, reporting progress for each. .DESCRIPTION - Iterates through the provided feature IDs. Features with a RegistryUndoKey - are handled by importing the undo .reg file; all others delegate to - Invoke-FeatureUndo for custom undo logic. + Iterates through the provided feature IDs and delegates each to + Invoke-FeatureUndo, which handles registry-backed and custom undo logic. This is called by Invoke-AllChanges during the undo phase. #> function Invoke-UndoFeatures { @@ -291,11 +314,11 @@ function Invoke-UndoFeatures { & $script:ApplyProgressCallback $step $TotalSteps $undoText } - if ($f -and $f.RegistryUndoKey) { - Import-RegistryFile "> $undoText" (Resolve-UndoRegFilePath $f.RegistryUndoKey) + if (-not (Invoke-FeatureUndo -FeatureId $featureId)) { + $script:UndoFeatureFailures++ + $script:FeatureFailures++ } - - Invoke-FeatureUndo -FeatureId $featureId + Write-Host "" $step++ } } @@ -324,8 +347,11 @@ function Invoke-AllChanges { throw "Win11Debloat is running as the SYSTEM account. Use the '-User' or '-Sysprep' parameter to target a specific user." } - $script:RegistryImportFailures = 0 $script:AppRemovalFailures = 0 + $script:FeatureFailures = 0 + $script:ApplyFeatureFailures = 0 + $script:UndoFeatureFailures = 0 + $script:PrerequisiteFailures = 0 $script:AppRemovalVerificationUnavailable = $false # ---- Gather work items ---- @@ -405,7 +431,11 @@ function Invoke-AllChanges { } else { Write-Host "> Creating a system restore point..." - Invoke-SystemRestorePoint + $restorePointSucceeded = Invoke-SystemRestorePoint + if (-not $restorePointSucceeded) { + if ($script:CancelRequested) { return } + $script:PrerequisiteFailures++ + } Write-Host "" } } @@ -429,19 +459,30 @@ function Invoke-AllChanges { } # ================================================================ - # Final: Report registry import and app removal failures + # Final: Report failures # ================================================================ - if ($script:RegistryImportFailures -gt 0) { - Write-Host "" - Write-Warning "$($script:RegistryImportFailures) registry import change(s) failed. See output above for details." - } - if ($script:AppRemovalFailures -gt 0) { Write-Host "" Write-Warning "$($script:AppRemovalFailures) app removal(s) failed. See output above for details." } + if ($script:ApplyFeatureFailures -gt 0) { + Write-Host "" + Write-Warning "$($script:ApplyFeatureFailures) feature change(s) failed to apply. See output above for details." + } + + if ($script:UndoFeatureFailures -gt 0) { + Write-Host "" + Write-Warning "$($script:UndoFeatureFailures) feature change(s) failed to undo. See output above for details." + } + + if ($script:PrerequisiteFailures -gt 0) { + Write-Host "" + Write-Warning "$($script:PrerequisiteFailures) requested prerequisite(s) could not be completed. Changes continued at your request." + } + if ($script:AppRemovalVerificationUnavailable) { + Write-Host "" Write-Warning "Unable to verify if all apps were uninstalled successfully." } diff --git a/Scripts/Features/Invoke-SystemRestorePoint.ps1 b/Scripts/Features/Invoke-SystemRestorePoint.ps1 index 7c0d72d..ed87b9e 100644 --- a/Scripts/Features/Invoke-SystemRestorePoint.ps1 +++ b/Scripts/Features/Invoke-SystemRestorePoint.ps1 @@ -1,10 +1,18 @@ function Invoke-SystemRestorePoint { - $SysRestore = Get-ItemProperty -Path "HKLM:\SOFTWARE\Microsoft\Windows NT\CurrentVersion\SystemRestore" -Name "RPSessionInterval" $failed = $false + $isSilent = ($script:Params -and $script:Params.ContainsKey('Silent')) -or $script:Silent - if ($SysRestore.RPSessionInterval -eq 0) { + try { + $SysRestore = Get-ItemProperty -Path "HKLM:\SOFTWARE\Microsoft\Windows NT\CurrentVersion\SystemRestore" -Name "RPSessionInterval" -ErrorAction Stop + } + catch { + Write-Host "Error: Unable to determine whether System Restore is enabled: $($_.Exception.Message)" -ForegroundColor Red + $failed = $true + } + + if (-not $failed -and $SysRestore.RPSessionInterval -eq 0) { # In GUI mode, skip the prompt and just try to enable it - if ($script:GuiWindow -or $Silent -or $( Read-Host -Prompt "System restore is disabled, would you like to enable it and create a restore point? (y/n)") -eq 'y') { + if ($script:GuiWindow -or $isSilent -or $( Read-Host -Prompt "System restore is disabled, would you like to enable it and create a restore point? (y/n)") -eq 'y') { try { $enableResult = Invoke-NonBlocking -TimeoutSeconds 90 -ScriptBlock { try { @@ -26,7 +34,6 @@ function Invoke-SystemRestorePoint { } } else { - Write-Host "" $failed = $true } } @@ -79,17 +86,20 @@ function Invoke-SystemRestorePoint { if ($result -ne "Yes") { $script:CancelRequested = $true - return + return $false } } - elseif (-not $Silent) { + elseif (-not $isSilent) { Write-Host "Failed to create a system restore point. Do you want to continue without a restore point? (y/n)" -ForegroundColor Yellow if ($( Read-Host ) -ne 'y') { $script:CancelRequested = $true - return + return $false } } Write-Host "Warning: Continuing without restore point" -ForegroundColor Yellow + return $false } + + return $true } diff --git a/Scripts/Features/Replace-StartMenu.ps1 b/Scripts/Features/Replace-StartMenu.ps1 index 3970d9e..accd9bf 100644 --- a/Scripts/Features/Replace-StartMenu.ps1 +++ b/Scripts/Features/Replace-StartMenu.ps1 @@ -29,8 +29,7 @@ function Replace-StartMenuForAllUsers { # Check if template bin file exists if (-not (Test-Path $startMenuTemplate)) { Write-Host "Error: Unable to clear start menu, start2.bin file missing from script folder" -ForegroundColor Red - Write-Host "" - return + return $false } # Get path to start menu file for all users @@ -38,8 +37,11 @@ function Replace-StartMenuForAllUsers { $usersStartMenuPaths = Get-ChildItem -Path $userPathString -ErrorAction SilentlyContinue # Go through all users and replace the start menu file + $success = $true ForEach ($startMenuPath in $usersStartMenuPaths) { - Replace-StartMenu -startMenuBinFile "$($startMenuPath.Fullname)\start2.bin" -startMenuTemplate $startMenuTemplate + if (-not (Replace-StartMenu -startMenuBinFile "$($startMenuPath.Fullname)\start2.bin" -startMenuTemplate $startMenuTemplate)) { + $success = $false + } } # Also replace the start menu file for the default user profile @@ -47,19 +49,29 @@ function Replace-StartMenuForAllUsers { if ($script:Params.ContainsKey("WhatIf")) { Write-Host "[WhatIf] Replace Start Menu for Default user profile with template $startMenuTemplate" -ForegroundColor Cyan - return + return $true } # Create folder if it doesn't exist if (-not (Test-Path $defaultStartMenuPath)) { - new-item $defaultStartMenuPath -ItemType Directory -Force | Out-Null - Write-Host "Created LocalState folder for default user profile" + try { + New-Item $defaultStartMenuPath -ItemType Directory -Force -ErrorAction Stop | Out-Null + Write-Host "Created LocalState folder for default user profile" + } + catch { + Write-Warning "Failed to create the Default profile Start Menu directory: $($_.Exception.Message)" + return $false + } } # Copy template to default profile - Replace-StartMenu -startMenuBinFile "$($defaultStartMenuPath)\start2.bin" -startMenuTemplate $startMenuTemplate - Write-Host "Replaced start menu for the default user profile" - Write-Host "" + if (-not (Replace-StartMenu -startMenuBinFile "$($defaultStartMenuPath)\start2.bin" -startMenuTemplate $startMenuTemplate)) { + $success = $false + } + else { + Write-Host "Replaced start menu for the default user profile" + } + return $success } @@ -98,19 +110,19 @@ function Replace-StartMenu { # Check if template bin file exists if (-not (Test-Path $startMenuTemplate)) { Write-Host "Error: Unable to replace start menu, template file not found" -ForegroundColor Red - return + return $false } if ([IO.Path]::GetExtension($startMenuTemplate) -ne ".bin") { Write-Host "Error: Unable to replace start menu, template file is not a valid .bin file" -ForegroundColor Red - return + return $false } $userName = Get-StartMenuUserNameFromPath -StartMenuBinFile $startMenuBinFile if ($script:Params.ContainsKey("WhatIf")) { Write-Host "[WhatIf] Replace Start Menu for user $userName with template $startMenuTemplate" -ForegroundColor Cyan - return + return $true } $timestamp = Get-Date -Format 'yyyyMMdd_HHmmss' @@ -118,20 +130,27 @@ function Replace-StartMenu { $startMenuDir = Split-Path $startMenuBinFile -Parent $backupBinFile = Join-Path $startMenuDir $backupFileName - if (Test-Path $startMenuBinFile) { - # Backup current start menu file - Copy-Item -Path $startMenuBinFile -Destination $backupBinFile -Force - Write-Verbose "Start menu backup for user $userName saved to $backupFileName" - } - else { - Write-Host "Unable to find original start2.bin file for user $userName, no backup was created for this user" -ForegroundColor Yellow - New-Item -ItemType File -Path $startMenuBinFile -Force - } + try { + if (Test-Path $startMenuBinFile) { + # Backup current start menu file + Copy-Item -Path $startMenuBinFile -Destination $backupBinFile -Force -ErrorAction Stop + Write-Verbose "Start menu backup for user $userName saved to $backupFileName" + } + else { + Write-Host "Unable to find original start2.bin file for user $userName, no backup was created for this user" -ForegroundColor Yellow + New-Item -ItemType File -Path $startMenuBinFile -Force -ErrorAction Stop | Out-Null + } - # Copy template file - Copy-Item -Path $startMenuTemplate -Destination $startMenuBinFile -Force + # Copy template file + Copy-Item -Path $startMenuTemplate -Destination $startMenuBinFile -Force -ErrorAction Stop + } + catch { + Write-Warning "Failed to replace Start Menu for user ${userName}: $($_.Exception.Message)" + return $false + } Write-Host "Replaced start menu for user $userName" + return $true } <# @@ -447,4 +466,4 @@ function Restore-StartMenuForAllUsers { } return $results -} \ No newline at end of file +} diff --git a/Scripts/Features/Set-StoreSearchSuggestions.ps1 b/Scripts/Features/Set-StoreSearchSuggestions.ps1 index 85b3959..44a51e1 100644 --- a/Scripts/Features/Set-StoreSearchSuggestions.ps1 +++ b/Scripts/Features/Set-StoreSearchSuggestions.ps1 @@ -12,20 +12,36 @@ DisableStoreSearchSuggestionsForAllUsers #> function Set-StoreSearchSuggestionsDisabledForAllUsers { + $success = $true + $processedProfiles = 0 + # Get path to Store app database for all users $userPathString = Get-UserDirectory -userName "*" -fileName "AppData\Local\Packages" $usersStoreDbPaths = Get-ChildItem -Path $userPathString -ErrorAction SilentlyContinue # Go through all users and disable start search suggestions foreach ($storeDbPath in $usersStoreDbPaths) { - Set-StoreSearchSuggestionsDisabled -StoreAppsDatabase ($storeDbPath.FullName + "\Microsoft.WindowsStore_8wekyb3d8bbwe\LocalState\store.db") + $processedProfiles++ + if (-not (Set-StoreSearchSuggestionsDisabled -StoreAppsDatabase ($storeDbPath.FullName + "\Microsoft.WindowsStore_8wekyb3d8bbwe\LocalState\store.db"))) { + $success = $false + } } # Also disable start search suggestions for the default user profile $defaultStoreDbPath = Get-StoreAppsDatabasePathForUser -UserName "Default" if ($defaultStoreDbPath) { - Set-StoreSearchSuggestionsDisabled -StoreAppsDatabase $defaultStoreDbPath + $processedProfiles++ + if (-not (Set-StoreSearchSuggestionsDisabled -StoreAppsDatabase $defaultStoreDbPath)) { + $success = $false + } } + + if ($processedProfiles -eq 0) { + Write-Warning 'Unable to disable Microsoft Store search suggestions because no target user profiles could be resolved.' + return $false + } + + return $success } @@ -56,24 +72,22 @@ function Set-StoreSearchSuggestionsDisabled { if ($script:Params.ContainsKey("WhatIf")) { Write-Host "[WhatIf] Disable Microsoft Store search suggestions for user $userName by restricting access to ${StoreAppsDatabase}" -ForegroundColor Cyan - return + return $true } - # This file doesn't exist in EEA (No Store app suggestions). - if (-not (Test-Path -Path $StoreAppsDatabase)) - { - Write-Host "Unable to find Store app database for user $userName, creating it now to prevent Windows from creating it later..." -ForegroundColor Yellow + try { + # This file doesn't exist in EEA (No Store app suggestions). + if (-not (Test-Path -Path $StoreAppsDatabase)) { + Write-Host "Unable to find Store app database for user $userName, creating it now to prevent Windows from creating it later..." -ForegroundColor Yellow - $storeDbDir = Split-Path -Path $StoreAppsDatabase -Parent + $storeDbDir = Split-Path -Path $StoreAppsDatabase -Parent + if (-not (Test-Path -Path $storeDbDir)) { + New-Item -Path $storeDbDir -ItemType Directory -Force -ErrorAction Stop | Out-Null + } - if (-not (Test-Path -Path $storeDbDir)) { - New-Item -Path $storeDbDir -ItemType Directory -Force | Out-Null + New-Item -Path $StoreAppsDatabase -ItemType File -Force -ErrorAction Stop | Out-Null } - New-Item -Path $StoreAppsDatabase -ItemType File -Force | Out-Null - } - - try { $AccountSid = [System.Security.Principal.SecurityIdentifier]::new('S-1-1-0') # 'EVERYONE' group $Acl = Get-Acl -Path $StoreAppsDatabase -ErrorAction Stop $Ace = [System.Security.AccessControl.FileSystemAccessRule]::new($AccountSid, 'FullControl', 'Deny') @@ -82,10 +96,11 @@ function Set-StoreSearchSuggestionsDisabled { } catch { Write-Warning "Failed to restrict ACL for store database '$StoreAppsDatabase': $($_.Exception.Message)" - return + return $false } Write-Host "Disabled Microsoft Store search suggestions for user $userName" + return $true } <# @@ -102,20 +117,36 @@ function Set-StoreSearchSuggestionsDisabled { EnableStoreSearchSuggestionsForAllUsers #> function Set-StoreSearchSuggestionsEnabledForAllUsers { + $success = $true + $processedProfiles = 0 + # Get path to Store app database for all users $userPathString = Get-UserDirectory -userName "*" -fileName "AppData\Local\Packages" $usersStoreDbPaths = Get-ChildItem -Path $userPathString -ErrorAction SilentlyContinue # Go through all users and re-enable start search suggestions foreach ($storeDbPath in $usersStoreDbPaths) { - Set-StoreSearchSuggestionsEnabled -StoreAppsDatabase ($storeDbPath.FullName + "\Microsoft.WindowsStore_8wekyb3d8bbwe\LocalState\store.db") + $processedProfiles++ + if (-not (Set-StoreSearchSuggestionsEnabled -StoreAppsDatabase ($storeDbPath.FullName + "\Microsoft.WindowsStore_8wekyb3d8bbwe\LocalState\store.db"))) { + $success = $false + } } # Also re-enable for the default user profile $defaultStoreDbPath = Get-StoreAppsDatabasePathForUser -UserName "Default" if ($defaultStoreDbPath) { - Set-StoreSearchSuggestionsEnabled -StoreAppsDatabase $defaultStoreDbPath + $processedProfiles++ + if (-not (Set-StoreSearchSuggestionsEnabled -StoreAppsDatabase $defaultStoreDbPath)) { + $success = $false + } } + + if ($processedProfiles -eq 0) { + Write-Warning 'Unable to re-enable Microsoft Store search suggestions because no target user profiles could be resolved.' + return $false + } + + return $success } <# @@ -145,23 +176,31 @@ function Set-StoreSearchSuggestionsEnabled { if ($script:Params.ContainsKey("WhatIf")) { Write-Host "[WhatIf] Re-enable Microsoft Store search suggestions for user $userName by restoring access to ${StoreAppsDatabase}" -ForegroundColor Cyan - return + return $true } if (-not (Test-Path -Path $StoreAppsDatabase)) { Write-Host "Store app database not found for user $userName, nothing to undo" - return + return $true } # Ensure we can modify/delete the file even if restrictive ACLs were set. $global:LASTEXITCODE = 0 takeown /F "$StoreAppsDatabase" /A | Out-Null + if ($LASTEXITCODE -ne 0) { + Write-Warning "Failed to take ownership of store database '$StoreAppsDatabase' while undoing Microsoft Store search suggestions. Exit code: $LASTEXITCODE" + return $false + } icacls "$StoreAppsDatabase" /grant *S-1-5-32-544:F /C | Out-Null + if ($LASTEXITCODE -ne 0) { + Write-Warning "Failed to grant Administrators access to store database '$StoreAppsDatabase' while undoing Microsoft Store search suggestions. Exit code: $LASTEXITCODE" + return $false + } $everyoneSid = [System.Security.Principal.SecurityIdentifier]::new('S-1-1-0') # 'EVERYONE' group try { - $acl = Get-Acl -Path $StoreAppsDatabase + $acl = Get-Acl -Path $StoreAppsDatabase -ErrorAction Stop $denyRules = @( $acl.Access | Where-Object { if ($_.AccessControlType -ne [System.Security.AccessControl.AccessControlType]::Deny) { return $false } @@ -179,7 +218,7 @@ function Set-StoreSearchSuggestionsEnabled { $null = $acl.RemoveAccessRuleSpecific($denyRule) } - Set-Acl -Path $StoreAppsDatabase -AclObject $acl | Out-Null + Set-Acl -Path $StoreAppsDatabase -AclObject $acl -ErrorAction Stop | Out-Null } catch { Write-Warning "Failed to normalize ACL for store database '$StoreAppsDatabase': $($_.Exception.Message)" @@ -188,9 +227,11 @@ function Set-StoreSearchSuggestionsEnabled { try { Remove-Item -Path $StoreAppsDatabase -Force -ErrorAction Stop Write-Host "Re-enabled Microsoft Store search suggestions for user $userName" + return $true } catch { - throw "Failed to remove '$StoreAppsDatabase' while undoing Microsoft Store search suggestions for user $userName. $($_.Exception.Message)" + Write-Warning "Failed to remove '$StoreAppsDatabase' while undoing Microsoft Store search suggestions for user $userName. $($_.Exception.Message)" + return $false } } diff --git a/Scripts/Features/Telemetry-ScheduledTasks.ps1 b/Scripts/Features/Telemetry-ScheduledTasks.ps1 index 41b9eed..3f50ac5 100644 --- a/Scripts/Features/Telemetry-ScheduledTasks.ps1 +++ b/Scripts/Features/Telemetry-ScheduledTasks.ps1 @@ -39,15 +39,17 @@ function Disable-TelemetryScheduledTasks { Write-Host "> Disabling telemetry scheduled tasks..." $tasks = Get-TelemetryScheduledTasks + $success = $true foreach ($task in $tasks) { - if ($script:CancelRequested) { return } + if ($script:CancelRequested) { return $false } if ($script:Params.ContainsKey("WhatIf")) { Write-Host "[WhatIf] Disable Scheduled Task: $($task.Path)$($task.Name)" -ForegroundColor Cyan continue } - $result = Invoke-NonBlocking -ScriptBlock { + try { + $result = Invoke-NonBlocking -ScriptBlock { param($path, $name) Import-Module ScheduledTasks -ErrorAction SilentlyContinue $taskObj = Get-ScheduledTask -TaskPath $path -TaskName $name -ErrorAction SilentlyContinue @@ -64,17 +66,24 @@ function Disable-TelemetryScheduledTasks { } } return @{ Success = $true; Status = 'AlreadyDisabled' } - } -ArgumentList @($task.Path, $task.Name) + } -ArgumentList @($task.Path, $task.Name) + } + catch { + Write-Warning "Failed to disable Scheduled Task: $($task.Path)$($task.Name) - $($_.Exception.Message)" + $success = $false + continue + } switch ($result.Status) { 'Disabled' { Write-Host "Disabled Scheduled Task: $($task.Path)$($task.Name)" } 'AlreadyDisabled' { Write-Host "Scheduled Task $($task.Path)$($task.Name) is already disabled" -ForegroundColor DarkGray } 'NotFound' { Write-Host "Scheduled Task $($task.Path)$($task.Name) not found" -ForegroundColor DarkGray } - 'Error' { Write-Host "Failed to disable Scheduled Task: $($task.Path)$($task.Name) - $($result.Error)" -ForegroundColor Yellow } + 'Error' { Write-Host "Failed to disable Scheduled Task: $($task.Path)$($task.Name) - $($result.Error)" -ForegroundColor Yellow; $success = $false } + default { Write-Warning "Unable to determine the result of disabling Scheduled Task: $($task.Path)$($task.Name)."; $success = $false } } } - Write-Host "" + return $success } <# @@ -93,15 +102,17 @@ function Enable-TelemetryScheduledTasks { Write-Host "> Enabling telemetry scheduled tasks..." $tasks = Get-TelemetryScheduledTasks + $success = $true foreach ($task in $tasks) { - if ($script:CancelRequested) { return } + if ($script:CancelRequested) { return $false } if ($script:Params.ContainsKey("WhatIf")) { Write-Host "[WhatIf] Enable Scheduled Task: $($task.Path)$($task.Name)" -ForegroundColor Cyan continue } - $result = Invoke-NonBlocking -ScriptBlock { + try { + $result = Invoke-NonBlocking -ScriptBlock { param($path, $name) Import-Module ScheduledTasks -ErrorAction SilentlyContinue $taskObj = Get-ScheduledTask -TaskPath $path -TaskName $name -ErrorAction SilentlyContinue @@ -118,15 +129,22 @@ function Enable-TelemetryScheduledTasks { } } return @{ Success = $true; Status = 'AlreadyEnabled' } - } -ArgumentList @($task.Path, $task.Name) + } -ArgumentList @($task.Path, $task.Name) + } + catch { + Write-Warning "Failed to enable Scheduled Task: $($task.Path)$($task.Name) - $($_.Exception.Message)" + $success = $false + continue + } switch ($result.Status) { 'Enabled' { Write-Host "Enabled Scheduled Task: $($task.Path)$($task.Name)" } 'AlreadyEnabled' { Write-Host "Scheduled Task $($task.Path)$($task.Name) is already enabled." -ForegroundColor DarkGray } 'NotFound' { Write-Host "Scheduled Task $($task.Path)$($task.Name) not found." -ForegroundColor DarkGray } - 'Error' { Write-Host "Failed to enable Scheduled Task: $($task.Path)$($task.Name) - $($result.Error)" -ForegroundColor Yellow } + 'Error' { Write-Host "Failed to enable Scheduled Task: $($task.Path)$($task.Name) - $($result.Error)" -ForegroundColor Yellow; $success = $false } + default { Write-Warning "Unable to determine the result of enabling Scheduled Task: $($task.Path)$($task.Name)."; $success = $false } } } - Write-Host "" + return $success } diff --git a/Scripts/Features/Windows-OptionalFeatures.ps1 b/Scripts/Features/Windows-OptionalFeatures.ps1 index e284f79..1949906 100644 --- a/Scripts/Features/Windows-OptionalFeatures.ps1 +++ b/Scripts/Features/Windows-OptionalFeatures.ps1 @@ -6,19 +6,42 @@ function Enable-WindowsFeature { if ($script:Params.ContainsKey("WhatIf")) { Write-Host "[WhatIf] Enable Windows feature: $FeatureName" -ForegroundColor Cyan - Write-Host "" - return + return $true } - $result = Invoke-NonBlocking -ScriptBlock { - param($name) - Enable-WindowsOptionalFeature -Online -FeatureName $name -All -NoRestart - } -ArgumentList $FeatureName - - $dismResult = @($result) | Where-Object { $_ -is [Microsoft.Dism.Commands.ImageObject] } - if ($dismResult) { - Write-Host ($dismResult | Out-String).Trim() + try { + $result = Invoke-NonBlocking -ScriptBlock { + param($name) + try { + $output = Enable-WindowsOptionalFeature -Online -FeatureName $name -All -NoRestart -ErrorAction Stop + return [PSCustomObject]@{ + Success = $true + Output = if ($output) { ($output | Out-String).Trim() } else { $null } + Error = $null + } + } + catch { + return [PSCustomObject]@{ + Success = $false + Output = $null + Error = $_.Exception.Message + } + } + } -ArgumentList $FeatureName } + catch { + Write-Warning "Failed to enable Windows feature '$FeatureName': $($_.Exception.Message)" + return $false + } + + if (-not $result -or -not $result.Success) { + $details = if ($result -and $result.Error) { ": $($result.Error)" } else { '' } + Write-Warning "Failed to enable Windows feature '$FeatureName'$details" + return $false + } + + if ($result.Output) { Write-Host $result.Output } + return $true } # Disables a Windows optional feature and pipes its output to the console @@ -29,19 +52,42 @@ function Disable-WindowsFeature { if ($script:Params.ContainsKey("WhatIf")) { Write-Host "[WhatIf] Disable Windows feature: $FeatureName" -ForegroundColor Cyan - Write-Host "" - return + return $true } - $result = Invoke-NonBlocking -ScriptBlock { - param($name) - Disable-WindowsOptionalFeature -Online -FeatureName $name -NoRestart - } -ArgumentList $FeatureName - - $dismResult = @($result) | Where-Object { $_ -is [Microsoft.Dism.Commands.ImageObject] } - if ($dismResult) { - Write-Host ($dismResult | Out-String).Trim() + try { + $result = Invoke-NonBlocking -ScriptBlock { + param($name) + try { + $output = Disable-WindowsOptionalFeature -Online -FeatureName $name -NoRestart -ErrorAction Stop + return [PSCustomObject]@{ + Success = $true + Output = if ($output) { ($output | Out-String).Trim() } else { $null } + Error = $null + } + } + catch { + return [PSCustomObject]@{ + Success = $false + Output = $null + Error = $_.Exception.Message + } + } + } -ArgumentList $FeatureName } + catch { + Write-Warning "Failed to disable Windows feature '$FeatureName': $($_.Exception.Message)" + return $false + } + + if (-not $result -or -not $result.Success) { + $details = if ($result -and $result.Error) { ": $($result.Error)" } else { '' } + Write-Warning "Failed to disable Windows feature '$FeatureName'$details" + return $false + } + + if ($result.Output) { Write-Host $result.Output } + return $true } function Test-WindowsOptionalFeatureEnabled { @@ -58,4 +104,4 @@ function Test-WindowsOptionalFeatureEnabled { } return ($feature.State -eq 'Enabled') -} \ No newline at end of file +} diff --git a/Scripts/GUI/Show-ApplyModal.ps1 b/Scripts/GUI/Show-ApplyModal.ps1 index a0a9cdd..144d2ec 100644 --- a/Scripts/GUI/Show-ApplyModal.ps1 +++ b/Scripts/GUI/Show-ApplyModal.ps1 @@ -114,9 +114,14 @@ function Show-ApplyModal { try { Invoke-AllChanges - $registryImportFailureCount = [int]$script:RegistryImportFailures $appRemovalFailureCount = [int]$script:AppRemovalFailures - $failureCount = $registryImportFailureCount + $appRemovalFailureCount + $applyFailureCount = [int]$script:ApplyFeatureFailures + $undoFailureCount = [int]$script:UndoFeatureFailures + $featureFailureCount = $applyFailureCount + $undoFailureCount + $prerequisiteFailureCount = [int]$script:PrerequisiteFailures + # App removals are a subset of failed features, so adding both counters + # would report each affected feature twice. + $failureCount = $featureFailureCount $appRemovalVerificationUnavailable = [bool]$script:AppRemovalVerificationUnavailable # Restart explorer if requested @@ -144,7 +149,7 @@ function Show-ApplyModal { $script:ApplyCompletionIconEl.Foreground = [System.Windows.Media.SolidColorBrush]::new([System.Windows.Media.ColorConverter]::ConvertFromString("#e8912d")) $script:ApplyCompletionTitleEl.Text = "Cancelled" $script:ApplyCompletionMessageEl.Text = "Script execution was cancelled by the user." - } elseif ($failureCount -gt 0 -or $appRemovalVerificationUnavailable) { + } elseif ($failureCount -gt 0 -or $prerequisiteFailureCount -gt 0 -or $appRemovalVerificationUnavailable) { if ($failureCount -gt 0) { Write-Host "Script completed with $failureCount error(s)." } @@ -158,8 +163,10 @@ function Show-ApplyModal { else { $script:ApplyCompletionTitleEl.Text = "Changes Applied with Errors" $failureMessages = @() - if ($registryImportFailureCount -gt 0) { $failureMessages += "$registryImportFailureCount registry change(s) failed" } if ($appRemovalFailureCount -gt 0) { $failureMessages += "$appRemovalFailureCount app removal(s) failed" } + if ($applyFailureCount -gt 0) { $failureMessages += "$applyFailureCount feature change(s) failed to apply" } + if ($undoFailureCount -gt 0) { $failureMessages += "$undoFailureCount feature change(s) failed to undo" } + if ($prerequisiteFailureCount -gt 0) { $failureMessages += "$prerequisiteFailureCount requested prerequisite(s) could not be completed" } if ($appRemovalVerificationUnavailable) { $failureMessages += "Unable to verify if all apps were uninstalled successfully" } $script:ApplyCompletionMessageEl.Text = "$($failureMessages -join '; '). See console for details." } diff --git a/Scripts/Helpers/Apply-RegistryRegFile.ps1 b/Scripts/Helpers/Apply-RegistryRegFile.ps1 index f2d48d8..bc89532 100644 --- a/Scripts/Helpers/Apply-RegistryRegFile.ps1 +++ b/Scripts/Helpers/Apply-RegistryRegFile.ps1 @@ -203,7 +203,7 @@ function Invoke-RegistryOperationsFromRegFile { if ($script:Params.ContainsKey("WhatIf")) { Write-Host "[WhatIf] Apply $totalOperations registry changes from '$RegFilePath'" -ForegroundColor Cyan - return + return $true } foreach ($operation in $operations) { @@ -222,5 +222,8 @@ function Invoke-RegistryOperationsFromRegFile { if ($accessDeniedCount -gt 0) { Write-Warning "Registry fallback import completed with $accessDeniedCount access-restricted operation(s) skipped in '$RegFilePath'." + return $false } + + return $true } diff --git a/Tests/Import-RegistryFile.Tests.ps1 b/Tests/Import-RegistryFile.Tests.ps1 index 3ff3de2..5f55ecc 100644 --- a/Tests/Import-RegistryFile.Tests.ps1 +++ b/Tests/Import-RegistryFile.Tests.ps1 @@ -9,27 +9,26 @@ BeforeAll { Describe 'Import-RegistryFile' { BeforeEach { $script:Params = @{} - $script:RegistryImportFailures = 0 $script:regPath = Join-Path $TestDrive 'feature.reg' '' | Set-Content -LiteralPath $script:regPath Mock Get-RegistryFilePathForFeature { $script:regPath } - Mock Invoke-RegistryOperationsFromRegFile {} + Mock Invoke-RegistryOperationsFromRegFile { $true } Mock Invoke-WithTargetUserHive {} Mock Invoke-NonBlocking { [PSCustomObject]@{ Output = @(); ExitCode = 0; Error = $null } } Mock Write-Host {} Mock Write-Warning {} } - It 'throws and increments the failure count when the registry file is missing' { + It 'returns false when the registry file is missing' { Mock Get-RegistryFilePathForFeature { Join-Path $TestDrive 'missing.reg' } - { Import-RegistryFile -message 'Apply' -path 'missing.reg' } | Should -Throw 'Unable to find registry file:*' - $script:RegistryImportFailures | Should -Be 1 + + Import-RegistryFile -message 'Apply' -path 'missing.reg' | Should -BeFalse Should -Invoke Invoke-NonBlocking -Times 0 -Exactly } It 'uses the PowerShell writer only in WhatIf mode' { $script:Params = @{ WhatIf = $true } - Import-RegistryFile -message 'Apply' -path 'feature.reg' + Import-RegistryFile -message 'Apply' -path 'feature.reg' | Should -BeTrue Should -Invoke Invoke-RegistryOperationsFromRegFile -Times 1 -Exactly -ParameterFilter { $RegFilePath -eq $script:regPath } Should -Invoke Invoke-NonBlocking -Times 0 -Exactly } @@ -41,7 +40,7 @@ Describe 'Import-RegistryFile' { & $ScriptBlock $ArgumentObject ([PSCustomObject]@{ WasAlreadyLoaded = $true }) } - Import-RegistryFile -message 'Apply' -path 'feature.reg' + Import-RegistryFile -message 'Apply' -path 'feature.reg' | Should -BeTrue Should -Invoke Invoke-WithTargetUserHive -Times 1 -Exactly -ParameterFilter { $TargetUserName -eq 'Alice' -and $PassHiveContext } Should -Invoke Invoke-RegistryOperationsFromRegFile -Times 1 -Exactly @@ -51,17 +50,22 @@ Describe 'Import-RegistryFile' { It 'falls back to the PowerShell writer when reg import fails' { Mock Invoke-NonBlocking { [PSCustomObject]@{ Output = @('denied'); ExitCode = 5; Error = 'access denied' } } - Import-RegistryFile -message 'Apply' -path 'feature.reg' + Import-RegistryFile -message 'Apply' -path 'feature.reg' | Should -BeTrue Should -Invoke Invoke-RegistryOperationsFromRegFile -Times 1 -Exactly Should -Invoke Write-Warning -Times 1 -Exactly -ParameterFilter { $Message -like "reg import failed*" } - $script:RegistryImportFailures | Should -Be 0 + } + + It 'returns false when the fallback cannot apply every registry operation' { + Mock Invoke-NonBlocking { [PSCustomObject]@{ Output = @('denied'); ExitCode = 5; Error = 'access denied' } } + Mock Invoke-RegistryOperationsFromRegFile { $false } + + Import-RegistryFile -message 'Apply' -path 'feature.reg' | Should -BeFalse } It 'does not invoke the fallback after a successful reg import' { - Import-RegistryFile -message 'Apply' -path 'feature.reg' + Import-RegistryFile -message 'Apply' -path 'feature.reg' | Should -BeTrue Should -Invoke Invoke-NonBlocking -Times 1 -Exactly Should -Invoke Invoke-RegistryOperationsFromRegFile -Times 0 -Exactly - $script:RegistryImportFailures | Should -Be 0 } } diff --git a/Tests/Invoke-Changes.Tests.ps1 b/Tests/Invoke-Changes.Tests.ps1 index 726b3bf..3e24bfe 100644 --- a/Tests/Invoke-Changes.Tests.ps1 +++ b/Tests/Invoke-Changes.Tests.ps1 @@ -1,24 +1,24 @@ BeforeAll { function Import-RegistryFile { param($Message, $path) } - function Remove-SelectedApps { param($Apps) } - function Invoke-ForceRemoveEdge {} - function Disable-TelemetryScheduledTasks {} - function Enable-TelemetryScheduledTasks {} + function Remove-SelectedApps { param($Apps) $true } + function Invoke-ForceRemoveEdge { $true } + function Disable-TelemetryScheduledTasks { $true } + function Enable-TelemetryScheduledTasks { $true } function Generate-AppsList { @() } function Get-FriendlyTargetUserName { 'current user' } - function Set-StoreSearchSuggestionsEnabledForAllUsers {} - function Set-StoreSearchSuggestionsEnabled { param($StoreAppsDatabase) } + function Set-StoreSearchSuggestionsEnabledForAllUsers { $true } + function Set-StoreSearchSuggestionsEnabled { param($StoreAppsDatabase) $true } function Get-StoreAppsDatabasePathForUser { param($UserName) 'store.db' } function Get-UserName { 'Alice' } - function Disable-WindowsFeature { param($FeatureName) } + function Disable-WindowsFeature { param($FeatureName) $true } function New-RegistrySettingsBackup { param($ActionableKeys, $ExtraFeatures) } function Invoke-SystemRestorePoint {} - function Enable-WindowsFeature { param($FeatureName) } + function Enable-WindowsFeature { param($FeatureName) $true } function Get-StartMenuBinPathForUser { param($UserName) 'start.bin' } - function Replace-StartMenu { param($startMenuBinFile, $startMenuTemplate) } - function Replace-StartMenuForAllUsers { param($startMenuTemplate) } - function Set-StoreSearchSuggestionsDisabledForAllUsers {} - function Set-StoreSearchSuggestionsDisabled { param($StoreAppsDatabase) } + function Replace-StartMenu { param($startMenuBinFile, $startMenuTemplate) $true } + function Replace-StartMenuForAllUsers { param($startMenuTemplate) $true } + function Set-StoreSearchSuggestionsDisabledForAllUsers { $true } + function Set-StoreSearchSuggestionsDisabled { param($StoreAppsDatabase) $true } . (Join-Path $PSScriptRoot '..\Scripts\Features\Invoke-Changes.ps1') } @@ -62,19 +62,19 @@ Describe 'Invoke-FeatureApply' { ReplaceStartAllUsers = [PSCustomObject]@{ ApplyText = 'Replace Start all users'; RegistryKey = '' } DisableStoreSearchSuggestions = [PSCustomObject]@{ ApplyText = 'Disable Store suggestions'; RegistryKey = '' } } - Mock Import-RegistryFile {} - Mock Remove-SelectedApps {} - Mock Invoke-ForceRemoveEdge {} - Mock Disable-TelemetryScheduledTasks {} + Mock Import-RegistryFile { $true } + Mock Remove-SelectedApps { $true } + Mock Invoke-ForceRemoveEdge { $true } + Mock Disable-TelemetryScheduledTasks { $true } Mock Generate-AppsList { @() } Mock Get-FriendlyTargetUserName { 'current user' } - Mock Enable-WindowsFeature {} + Mock Enable-WindowsFeature { $true } Mock Get-StartMenuBinPathForUser { 'start.bin' } Mock Get-UserName { 'Alice' } - Mock Replace-StartMenu {} - Mock Replace-StartMenuForAllUsers {} - Mock Set-StoreSearchSuggestionsDisabledForAllUsers {} - Mock Set-StoreSearchSuggestionsDisabled {} + Mock Replace-StartMenu { $true } + Mock Replace-StartMenuForAllUsers { $true } + Mock Set-StoreSearchSuggestionsDisabledForAllUsers { $true } + Mock Set-StoreSearchSuggestionsDisabled { $true } Mock Get-StoreAppsDatabasePathForUser { 'store.db' } Mock Get-Process { @() } Mock Stop-Process { param($InputObject) } @@ -95,6 +95,14 @@ Describe 'Invoke-FeatureApply' { Should -Invoke Disable-TelemetryScheduledTasks -Times 1 -Exactly } + It 'returns false without side effects when a registry import fails' { + Mock Import-RegistryFile { $false } + + Invoke-FeatureApply -FeatureId 'DisableTelemetry' | Should -BeFalse + + Should -Invoke Disable-TelemetryScheduledTasks -Times 0 -Exactly + } + It 'does not call app removal when the generated selection is empty' { Invoke-FeatureApply -FeatureId 'RemoveApps' @@ -127,6 +135,23 @@ Describe 'Invoke-FeatureApply' { Should -Invoke Remove-SelectedApps -Times 0 -Exactly } + It 'returns false when applying a feature throws' { + Mock Invoke-ForceRemoveEdge { throw 'access denied' } + Mock Write-Warning {} + + Invoke-FeatureApply -FeatureId 'ForceRemoveEdge' | Should -BeFalse + + Should -Invoke Write-Warning -Times 1 -Exactly -ParameterFilter { $Message -match "Failed to apply 'Force remove Edge'.*access denied" } + } + + It 'returns false for an unknown feature' { + Mock Write-Warning {} + + Invoke-FeatureApply -FeatureId 'Unknown' | Should -BeFalse + + Should -Invoke Write-Warning -Times 1 -Exactly -ParameterFilter { $Message -match "Unknown feature 'Unknown'.*could not be applied" } + } + It 'uses the expected static app list for ' -ForEach @( @{ FeatureId = 'RemoveGamingApps'; MinimumCount = 3; ExpectedApp = 'Microsoft.GamingApp' } @{ FeatureId = 'RemoveHPApps'; MinimumCount = 10; ExpectedApp = 'AD2F1837.myHP' } @@ -215,7 +240,7 @@ Describe 'Invoke-ApplyFeatures' { } $script:progressCalls = New-Object System.Collections.Generic.List[object] $script:ApplyProgressCallback = { param($Step, $Total, $Text) $script:progressCalls.Add(@($Step, $Total, $Text)) } - Mock Invoke-FeatureApply {} + Mock Invoke-FeatureApply { $true } } It 'reports progress and applies each feature in order' { @@ -235,6 +260,21 @@ Describe 'Invoke-ApplyFeatures' { Should -Invoke Invoke-FeatureApply -Times 0 -Exactly $script:progressCalls | Should -HaveCount 0 } + + It 'counts a failed feature application and continues with later features' { + $script:FeatureFailures = 0 + $script:ApplyFeatureFailures = 0 + Mock Invoke-FeatureApply { + param($FeatureId) + return ($FeatureId -ne 'One') + } + + Invoke-ApplyFeatures -FeatureIds @('One', 'Two') -StartStep 1 -TotalSteps 2 + + $script:FeatureFailures | Should -Be 1 + $script:ApplyFeatureFailures | Should -Be 1 + Should -Invoke Invoke-FeatureApply -Times 2 -Exactly + } } Describe 'Invoke-UndoFeatures' { @@ -246,14 +286,13 @@ Describe 'Invoke-UndoFeatures' { CustomUndo = [PSCustomObject]@{ UndoLabel = 'Undo custom'; ApplyUndoText = ''; RegistryUndoKey = '' } } Mock Resolve-UndoRegFilePath { param($FileName) "Undo\$FileName" } - Mock Import-RegistryFile {} - Mock Invoke-FeatureUndo {} + Mock Import-RegistryFile { $true } + Mock Invoke-FeatureUndo { $true } } - It 'imports registry undo data and still invokes custom undo side effects' { + It 'delegates registry-backed undo work to the feature undo handler' { Invoke-UndoFeatures -FeatureIds @('RegistryUndo') -StartStep 1 -TotalSteps 1 - Should -Invoke Import-RegistryFile -Times 1 -Exactly -ParameterFilter { $path -eq 'Undo\undo.reg' } Should -Invoke Invoke-FeatureUndo -Times 1 -Exactly -ParameterFilter { $FeatureId -eq 'RegistryUndo' } } @@ -264,6 +303,18 @@ Describe 'Invoke-UndoFeatures' { Should -Invoke Invoke-FeatureUndo -Times 2 -Exactly } + It 'counts one failure when a feature undo fails' { + $script:FeatureFailures = 0 + $script:UndoFeatureFailures = 0 + Mock Invoke-FeatureUndo { $false } + + Invoke-UndoFeatures -FeatureIds @('RegistryUndo') -StartStep 1 -TotalSteps 1 + + $script:FeatureFailures | Should -Be 1 + $script:UndoFeatureFailures | Should -Be 1 + Should -Invoke Invoke-FeatureUndo -Times 1 -Exactly + } + It 'stops before undoing when cancellation is requested' { $script:CancelRequested = $true @@ -283,12 +334,14 @@ Describe 'Invoke-FeatureUndo' { DisableTelemetry = [PSCustomObject]@{} DisableStoreSearchSuggestions = [PSCustomObject]@{} } - Mock Set-StoreSearchSuggestionsEnabledForAllUsers {} - Mock Set-StoreSearchSuggestionsEnabled {} + Mock Set-StoreSearchSuggestionsEnabledForAllUsers { $true } + Mock Set-StoreSearchSuggestionsEnabled { $true } Mock Get-StoreAppsDatabasePathForUser { 'store.db' } Mock Get-UserName { 'Alice' } - Mock Disable-WindowsFeature {} - Mock Enable-TelemetryScheduledTasks {} + Mock Disable-WindowsFeature { $true } + Mock Enable-TelemetryScheduledTasks { $true } + Mock Import-RegistryFile { $true } + Mock Resolve-UndoRegFilePath { param($FileName) "Undo\$FileName" } Mock Write-Host {} } @@ -304,16 +357,35 @@ Describe 'Invoke-FeatureUndo' { It 'disables both WSL optional features in dependency-safe order' { $script:disabledFeatures = [System.Collections.Generic.List[string]]::new() - Mock Disable-WindowsFeature { param($FeatureName) $script:disabledFeatures.Add($FeatureName) } + Mock Disable-WindowsFeature { param($FeatureName) $script:disabledFeatures.Add($FeatureName); $true } Invoke-FeatureUndo -FeatureId 'EnableWindowsSubsystemForLinux' $script:disabledFeatures | Should -Be @('Microsoft-Windows-Subsystem-Linux', 'VirtualMachinePlatform') } It 'disables Sandbox and re-enables telemetry tasks' { + $script:Features.DisableTelemetry = [PSCustomObject]@{ ApplyUndoText = 'Enable telemetry'; RegistryUndoKey = 'enable-telemetry.reg' } Invoke-FeatureUndo -FeatureId 'EnableWindowsSandbox' Invoke-FeatureUndo -FeatureId 'DisableTelemetry' Should -Invoke Disable-WindowsFeature -Times 1 -Exactly -ParameterFilter { $FeatureName -eq 'Containers-DisposableClientVM' } Should -Invoke Enable-TelemetryScheduledTasks -Times 1 -Exactly + Should -Invoke Import-RegistryFile -Times 1 -Exactly -ParameterFilter { $path -eq 'Undo\enable-telemetry.reg' } + } + + It 'returns false without side effects when a registry undo import fails' { + $script:Features.DisableTelemetry = [PSCustomObject]@{ ApplyUndoText = 'Enable telemetry'; RegistryUndoKey = 'enable-telemetry.reg' } + Mock Import-RegistryFile { $false } + + Invoke-FeatureUndo -FeatureId 'DisableTelemetry' | Should -BeFalse + + Should -Invoke Enable-TelemetryScheduledTasks -Times 0 -Exactly + } + + It 'warns and returns false for an unknown feature' { + Mock Write-Warning {} + + Invoke-FeatureUndo -FeatureId 'Unknown' | Should -BeFalse + + Should -Invoke Write-Warning -Times 1 -Exactly -ParameterFilter { $Message -match "Unknown feature 'Unknown'.*could not be undone" } } } @@ -332,7 +404,7 @@ Describe 'Invoke-AllChanges' { Mock Test-RunningAsSystem { $false } Mock Resolve-UndoRegFilePath { param($FileName) "Undo\$FileName" } Mock New-RegistrySettingsBackup {} - Mock Invoke-SystemRestorePoint {} + Mock Invoke-SystemRestorePoint { $true } Mock Invoke-ApplyFeatures {} Mock Invoke-UndoFeatures {} Mock Write-Host {} @@ -406,20 +478,21 @@ Describe 'Invoke-AllChanges' { $script:Params = @{ CreateRestorePoint = $true; CustomApply = $true } $script:UndoParams = @{} $script:order = [System.Collections.Generic.List[string]]::new() - Mock Invoke-SystemRestorePoint { $script:order.Add('restore-point') } + Mock Invoke-SystemRestorePoint { $script:order.Add('restore-point'); $true } Mock Invoke-ApplyFeatures { $script:order.Add('apply') } Invoke-AllChanges $script:order | Should -Be @('restore-point', 'apply') } - It 'reports registry import failures after all requested work completes' { - $script:Params = @{ CustomApply = $true } + It 'reports a restore point failure when the user chooses to continue' { + $script:Params = @{ CreateRestorePoint = $true; CustomApply = $true } $script:UndoParams = @{} - Mock Invoke-ApplyFeatures { $script:RegistryImportFailures = 2 } + Mock Invoke-SystemRestorePoint { $false } Invoke-AllChanges - Should -Invoke Write-Warning -Times 1 -Exactly -ParameterFilter { $Message -match '2 registry import change' } + $script:PrerequisiteFailures | Should -Be 1 + Should -Invoke Write-Warning -Times 1 -Exactly -ParameterFilter { $Message -match 'requested prerequisite' } } It 'reports app removal failures after all requested work completes' { diff --git a/Tests/Invoke-SystemRestorePoint.Tests.ps1 b/Tests/Invoke-SystemRestorePoint.Tests.ps1 index 45b5826..1f20a59 100644 --- a/Tests/Invoke-SystemRestorePoint.Tests.ps1 +++ b/Tests/Invoke-SystemRestorePoint.Tests.ps1 @@ -27,6 +27,26 @@ Describe 'Invoke-SystemRestorePoint' { $script:CancelRequested | Should -BeFalse } + It 'returns false through the continuation flow when the System Restore state cannot be read' { + Mock Get-ItemProperty { throw 'registry access denied' } + Mock Read-Host { 'y' } + + Invoke-SystemRestorePoint | Should -BeFalse + + $script:CancelRequested | Should -BeFalse + Should -Invoke Invoke-NonBlocking -Times 0 -Exactly + } + + It 'returns false without prompting when the System Restore state cannot be read in silent mode' { + $script:Silent = $true + Mock Get-ItemProperty { throw 'registry access denied' } + + Invoke-SystemRestorePoint | Should -BeFalse + + $script:CancelRequested | Should -BeFalse + Should -Invoke Read-Host -Times 0 -Exactly + } + It 'is loaded by the main entry point' { $entryPoint = Get-Content -LiteralPath (Join-Path $PSScriptRoot '..\Win11Debloat.ps1') -Raw $expectedImport = [regex]::Escape('Scripts/Features/Invoke-SystemRestorePoint.ps1') diff --git a/Tests/Remove-SelectedApps.Tests.ps1 b/Tests/Remove-SelectedApps.Tests.ps1 index 18435dc..883b0f2 100644 --- a/Tests/Remove-SelectedApps.Tests.ps1 +++ b/Tests/Remove-SelectedApps.Tests.ps1 @@ -11,6 +11,7 @@ BeforeAll { function Resolve-UserProfileContext { param($UserName) $null } . (Join-Path $PSScriptRoot '..\Scripts\AppRemoval\Remove-SelectedApps.ps1') + . (Join-Path $PSScriptRoot '..\Scripts\AppRemoval\Invoke-ForceRemoveEdge.ps1') } Describe 'Remove-SelectedApps' { @@ -71,10 +72,23 @@ Describe 'Remove-SelectedApps' { It 'counts a failed WinGet removal' { Mock Get-AppRemovalMethod { 'WinGet' } Mock Remove-WinGetApp { $false } + Mock Test-AppInWingetList { $true } + Mock Write-Host {} Remove-SelectedApps -appsList @('One.App') $script:AppRemovalFailures | Should -Be 1 + Should -Invoke Write-Host -Times 1 -Exactly -ParameterFilter { $Object -eq 'Unable to uninstall One.App via WinGet' -and $ForegroundColor -eq 'Red' } + } + + It 'does not count a non-zero WinGet command when the app is absent after verification' { + Mock Get-AppRemovalMethod { 'WinGet' } + Mock Remove-WinGetApp { $false } + Mock Test-AppInWingetList { $false } + + Remove-SelectedApps -appsList @('One.App') | Should -BeTrue + + $script:AppRemovalFailures | Should -Be 0 } It 'counts a WinGet removal that remains installed after a successful command' { @@ -132,7 +146,7 @@ Describe 'Remove-WinGetApp' { BeforeEach { $script:Params = @{} $script:WingetInstalled = $true - Mock Invoke-NonBlocking { $true } + Mock Invoke-NonBlocking { [PSCustomObject]@{ Success = $true; ExitCode = 0; Output = @() } } Mock Set-RunOnceWingetTask { $true } Mock Get-UserName { 'Alice' } Mock Write-Host {} @@ -174,14 +188,93 @@ Describe 'Remove-WinGetApp' { It 'reports a timed-out winget uninstall and continues' { $script:Params = @{ User = 'Alice' } Mock Invoke-NonBlocking { throw 'Operation timed out after 120 seconds' } + Mock Write-Verbose {} { Remove-WinGetApp -app 'One.App' } | Should -Not -Throw Should -Invoke Set-RunOnceWingetTask -Times 1 -Exactly - Should -Invoke Write-Error -Times 1 -Exactly -ParameterFilter { + Should -Invoke Write-Verbose -Times 1 -Exactly -ParameterFilter { $Message -like '*did not complete within 120 seconds*' } } + It 'returns false when winget exits unsuccessfully' { + Mock Invoke-NonBlocking { [PSCustomObject]@{ Success = $false; ExitCode = 1; Output = @('failure') } } + Mock Write-Verbose {} + + Remove-WinGetApp -app 'One.App' | Should -BeFalse + + Should -Invoke Write-Verbose -Times 1 -Exactly -ParameterFilter { $Message -match 'exit code 1' } + } + + It 'returns false for a non-zero WinGet exit code without writing an error record' { + Mock Invoke-NonBlocking { [PSCustomObject]@{ Success = $false; ExitCode = -1978335212; Output = @('No installed package found matching input criteria.') } } + Mock Write-Verbose {} + + Remove-WinGetApp -app 'One.App' | Should -BeFalse + + Should -Invoke Write-Verbose -Times 1 -Exactly -ParameterFilter { $Message -match 'post-removal inventory check' } + Should -Invoke Write-Verbose -Times 1 -Exactly -ParameterFilter { $Message -match 'No installed package found' } + } + + It 'writes captured winget output to the verbose stream before returning success' { + Mock Invoke-NonBlocking { [PSCustomObject]@{ Success = $true; ExitCode = 0; Output = @('Successfully uninstalled One.App') } } + Mock Write-Verbose {} + + Remove-WinGetApp -app 'One.App' | Should -BeTrue + + Should -Invoke Write-Verbose -Times 1 -Exactly -ParameterFilter { $Message -eq 'Successfully uninstalled One.App' } + } + + It 'writes captured winget diagnostics to the verbose stream when winget fails' { + Mock Invoke-NonBlocking { [PSCustomObject]@{ Success = $false; ExitCode = 1; Output = @('Package was not found') } } + Mock Write-Verbose {} + + Remove-WinGetApp -app 'One.App' | Should -BeFalse + + Should -Invoke Write-Verbose -Times 1 -Exactly -ParameterFilter { $Message -eq 'Package was not found' } + } + +} + +Describe 'Remove-EdgeAutostartValue' { + BeforeEach { + Mock Write-Warning {} + } + + It 'treats a missing value as already cleaned up' { + Mock Get-ItemProperty { [PSCustomObject]@{} } + Mock Remove-ItemProperty {} + + Remove-EdgeAutostartValue -Path 'HKCU:\Software\Example' -Name 'Microsoft Edge Update' | Should -BeTrue + + Should -Invoke Remove-ItemProperty -Times 0 -Exactly + } + + It 'removes an existing value' { + Mock Get-ItemProperty { [PSCustomObject]@{ 'Microsoft Edge Update' = 'enabled' } } + Mock Remove-ItemProperty {} + + Remove-EdgeAutostartValue -Path 'HKCU:\Software\Example' -Name 'Microsoft Edge Update' | Should -BeTrue + + Should -Invoke Remove-ItemProperty -Times 1 -Exactly -ParameterFilter { $Path -eq 'HKCU:\Software\Example' -and $Name -eq 'Microsoft Edge Update' } + } + + It 'returns false when an existing value cannot be removed' { + Mock Get-ItemProperty { [PSCustomObject]@{ 'Microsoft Edge Update' = 'enabled' } } + Mock Remove-ItemProperty { throw 'access denied' } + + Remove-EdgeAutostartValue -Path 'HKCU:\Software\Example' -Name 'Microsoft Edge Update' | Should -BeFalse + + Should -Invoke Write-Warning -Times 1 -Exactly -ParameterFilter { $Message -match 'access denied' } + } + + It 'returns false when the registry key cannot be inspected' { + Mock Get-ItemProperty { throw 'access denied' } + + Remove-EdgeAutostartValue -Path 'HKCU:\Software\Example' -Name 'Microsoft Edge Update' | Should -BeFalse + + Should -Invoke Write-Warning -Times 1 -Exactly -ParameterFilter { $Message -match 'access denied' } + } } Describe 'Remove-AppxApp' { diff --git a/Tests/Set-StoreSearchSuggestions.Tests.ps1 b/Tests/Set-StoreSearchSuggestions.Tests.ps1 index 468cf8f..a642368 100644 --- a/Tests/Set-StoreSearchSuggestions.Tests.ps1 +++ b/Tests/Set-StoreSearchSuggestions.Tests.ps1 @@ -37,20 +37,20 @@ Describe 'Store-search suggestion all-user operations' { ) } Mock Get-StoreAppsDatabasePathForUser { 'C:\Users\Default\AppData\Local\Packages\Microsoft.WindowsStore_8wekyb3d8bbwe\LocalState\store.db' } - Mock Set-StoreSearchSuggestionsDisabled {} - Mock Set-StoreSearchSuggestionsEnabled {} + Mock Set-StoreSearchSuggestionsDisabled { $true } + Mock Set-StoreSearchSuggestionsEnabled { $true } Mock Write-Warning {} } It 'disables suggestions for every discovered and Default profile' { - Set-StoreSearchSuggestionsDisabledForAllUsers + Set-StoreSearchSuggestionsDisabledForAllUsers | Should -BeTrue Should -Invoke Set-StoreSearchSuggestionsDisabled -Times 3 -Exactly Should -Invoke Set-StoreSearchSuggestionsDisabled -Times 1 -Exactly -ParameterFilter { $StoreAppsDatabase -match 'Users\\Default\\' } } It 'enables suggestions for every discovered and Default profile' { - Set-StoreSearchSuggestionsEnabledForAllUsers + Set-StoreSearchSuggestionsEnabledForAllUsers | Should -BeTrue Should -Invoke Set-StoreSearchSuggestionsEnabled -Times 3 -Exactly Should -Invoke Set-StoreSearchSuggestionsEnabled -Times 1 -Exactly -ParameterFilter { $StoreAppsDatabase -match 'Users\\Default\\' } @@ -72,6 +72,22 @@ Describe 'Store-search suggestion all-user operations' { Should -Invoke Set-StoreSearchSuggestionsEnabled -Times 2 -Exactly } + It 'returns false when any profile cannot be updated' { + Mock Set-StoreSearchSuggestionsDisabled { $false } -ParameterFilter { $StoreAppsDatabase -match 'Users\\Bob\\' } + + Set-StoreSearchSuggestionsDisabledForAllUsers | Should -BeFalse + Should -Invoke Set-StoreSearchSuggestionsDisabled -Times 3 -Exactly + } + + It 'returns false when no target profile can be resolved' { + Mock Get-ChildItem { @() } + Mock Get-StoreAppsDatabasePathForUser { $null } + Mock Write-Warning {} + + Set-StoreSearchSuggestionsDisabledForAllUsers | Should -BeFalse + Should -Invoke Write-Warning -Times 1 -Exactly -ParameterFilter { $Message -match 'no target user profiles' } + } + } Describe 'Set-StoreSearchSuggestionsDisabled' { @@ -84,7 +100,7 @@ Describe 'Set-StoreSearchSuggestionsDisabled' { } It 'does not touch the filesystem in WhatIf mode' { - Set-StoreSearchSuggestionsDisabled -StoreAppsDatabase 'C:\Users\Alice\AppData\Local\Packages\store.db' + Set-StoreSearchSuggestionsDisabled -StoreAppsDatabase 'C:\Users\Alice\AppData\Local\Packages\store.db' | Should -BeTrue Should -Invoke Test-Path -Times 0 -Exactly Should -Invoke Get-Acl -Times 0 -Exactly @@ -99,7 +115,7 @@ Describe 'Set-StoreSearchSuggestionsDisabled' { Mock Get-Acl { $acl } Mock Set-Acl {} - Set-StoreSearchSuggestionsDisabled -StoreAppsDatabase 'C:\Users\Alice\AppData\Local\Packages\store.db' + Set-StoreSearchSuggestionsDisabled -StoreAppsDatabase 'C:\Users\Alice\AppData\Local\Packages\store.db' | Should -BeTrue Should -Invoke New-Item -Times 2 -Exactly Should -Invoke New-Item -Times 1 -Exactly -ParameterFilter { $ItemType -eq 'Directory' } @@ -118,7 +134,7 @@ Describe 'Set-StoreSearchSuggestionsDisabled' { Mock Get-Acl { $acl } Mock Set-Acl {} - Set-StoreSearchSuggestionsDisabled -StoreAppsDatabase 'C:\Users\Alice\AppData\Local\Packages\store.db' + Set-StoreSearchSuggestionsDisabled -StoreAppsDatabase 'C:\Users\Alice\AppData\Local\Packages\store.db' | Should -BeTrue Should -Invoke New-Item -Times 0 -Exactly Should -Invoke Get-Acl -Times 1 -Exactly @@ -133,7 +149,7 @@ Describe 'Set-StoreSearchSuggestionsDisabled' { Mock Set-Acl { throw 'ACL must not be written after a read failure.' } Mock Write-Warning {} - Set-StoreSearchSuggestionsDisabled -StoreAppsDatabase 'C:\Users\Alice\AppData\Local\Packages\store.db' + Set-StoreSearchSuggestionsDisabled -StoreAppsDatabase 'C:\Users\Alice\AppData\Local\Packages\store.db' | Should -BeFalse Should -Invoke Write-Warning -Times 1 -Exactly Should -Invoke Write-Host -Times 0 -Exactly -ParameterFilter { $Object -like 'Disabled Microsoft Store search suggestions*' } @@ -165,7 +181,7 @@ Describe 'Set-StoreSearchSuggestionsEnabled' { } It 'does nothing when the Store database does not exist' { - Set-StoreSearchSuggestionsEnabled -StoreAppsDatabase 'C:\Users\Alice\AppData\Local\Packages\store.db' + Set-StoreSearchSuggestionsEnabled -StoreAppsDatabase 'C:\Users\Alice\AppData\Local\Packages\store.db' | Should -BeTrue Should -Invoke Get-Acl -Times 0 -Exactly Should -Invoke Remove-Item -Times 0 -Exactly @@ -177,7 +193,7 @@ Describe 'Set-StoreSearchSuggestionsEnabled' { Mock takeown { throw 'WhatIf should not take ownership.' } Mock icacls { throw 'WhatIf should not change ACLs.' } - Set-StoreSearchSuggestionsEnabled -StoreAppsDatabase 'C:\Users\Alice\AppData\Local\Packages\store.db' + Set-StoreSearchSuggestionsEnabled -StoreAppsDatabase 'C:\Users\Alice\AppData\Local\Packages\store.db' | Should -BeTrue Should -Invoke Test-Path -Times 0 -Exactly Should -Invoke takeown -Times 0 -Exactly @@ -193,7 +209,7 @@ Describe 'Set-StoreSearchSuggestionsEnabled' { Mock Set-Acl {} Mock Remove-Item {} - Set-StoreSearchSuggestionsEnabled -StoreAppsDatabase 'C:\Users\Alice\AppData\Local\Packages\store.db' + Set-StoreSearchSuggestionsEnabled -StoreAppsDatabase 'C:\Users\Alice\AppData\Local\Packages\store.db' | Should -BeTrue Should -Invoke takeown -Times 1 -Exactly Should -Invoke icacls -Times 1 -Exactly @@ -211,14 +227,14 @@ Describe 'Set-StoreSearchSuggestionsEnabled' { Mock Remove-Item {} Mock Write-Warning {} - Set-StoreSearchSuggestionsEnabled -StoreAppsDatabase 'C:\Users\Alice\AppData\Local\Packages\store.db' + Set-StoreSearchSuggestionsEnabled -StoreAppsDatabase 'C:\Users\Alice\AppData\Local\Packages\store.db' | Should -BeTrue Should -Invoke Write-Warning -Times 1 -Exactly Should -Invoke Set-Acl -Times 0 -Exactly Should -Invoke Remove-Item -Times 1 -Exactly } - It 'throws a contextual error when the database cannot be removed' { + It 'returns false when the database cannot be removed' { $acl = New-TestStoreDatabaseAcl Mock Test-Path { $true } Mock takeown {} @@ -226,9 +242,9 @@ Describe 'Set-StoreSearchSuggestionsEnabled' { Mock Get-Acl { $acl } Mock Set-Acl {} Mock Remove-Item { throw 'database is locked' } + Mock Write-Warning {} - { - Set-StoreSearchSuggestionsEnabled -StoreAppsDatabase 'C:\Users\Alice\AppData\Local\Packages\store.db' - } | Should -Throw '*Failed to remove*database is locked*' + Set-StoreSearchSuggestionsEnabled -StoreAppsDatabase 'C:\Users\Alice\AppData\Local\Packages\store.db' | Should -BeFalse + Should -Invoke Write-Warning -Times 1 -Exactly -ParameterFilter { $Message -match 'Failed to remove.*database is locked' } } } diff --git a/Tests/Telemetry-ScheduledTasks.Tests.ps1 b/Tests/Telemetry-ScheduledTasks.Tests.ps1 index 9b8e70c..2e9b9fd 100644 --- a/Tests/Telemetry-ScheduledTasks.Tests.ps1 +++ b/Tests/Telemetry-ScheduledTasks.Tests.ps1 @@ -108,6 +108,15 @@ Describe 'Disable-TelemetryScheduledTasks' { Should -Invoke Write-Host -Times 1 -Exactly -ParameterFilter { $Object -like "*$Expected*" } } + + It 'returns false for an unknown scheduler result' { + Mock Get-TelemetryScheduledTasks { @(@{ Path = '\Microsoft\Windows\Test\'; Name = 'Telemetry' }) } + Mock Invoke-NonBlocking { $null } + Mock Write-Warning {} + + Disable-TelemetryScheduledTasks | Should -BeFalse + Should -Invoke Write-Warning -Times 1 -Exactly + } } Describe 'Enable-TelemetryScheduledTasks' { @@ -179,4 +188,13 @@ Describe 'Enable-TelemetryScheduledTasks' { Should -Invoke Write-Host -Times 1 -Exactly -ParameterFilter { $Object -like "*$Expected*" } } + + It 'returns false when the scheduler throws' { + Mock Get-TelemetryScheduledTasks { @(@{ Path = '\Microsoft\Windows\Test\'; Name = 'Telemetry' }) } + Mock Invoke-NonBlocking { throw 'scheduler unavailable' } + Mock Write-Warning {} + + Enable-TelemetryScheduledTasks | Should -BeFalse + Should -Invoke Write-Warning -Times 1 -Exactly -ParameterFilter { $Message -match 'scheduler unavailable' } + } } diff --git a/Tests/Windows-OptionalFeatures.Tests.ps1 b/Tests/Windows-OptionalFeatures.Tests.ps1 index f0f1186..551733d 100644 --- a/Tests/Windows-OptionalFeatures.Tests.ps1 +++ b/Tests/Windows-OptionalFeatures.Tests.ps1 @@ -23,12 +23,12 @@ BeforeAll { Describe 'Enable-WindowsFeature' { BeforeEach { $script:Params = @{} - Mock Invoke-NonBlocking { @() } + Mock Invoke-NonBlocking { [PSCustomObject]@{ Success = $true; Output = $null; Error = $null } } Mock Write-Host {} } It 'schedules the requested feature with the non-blocking runner' { - Enable-WindowsFeature -FeatureName 'Feature.One' + Enable-WindowsFeature -FeatureName 'Feature.One' | Should -BeTrue Should -Invoke Invoke-NonBlocking -Times 1 -Exactly -ParameterFilter { $ArgumentList -eq 'Feature.One' } } @@ -40,7 +40,7 @@ Describe 'Enable-WindowsFeature' { $script:optionalFeatureBlock = $ScriptBlock $script:optionalFeatureArguments = $ArgumentList } - Enable-WindowsFeature -FeatureName 'Feature.One' + Enable-WindowsFeature -FeatureName 'Feature.One' | Should -BeFalse & $script:optionalFeatureBlock $script:optionalFeatureArguments $global:OptionalFeatureCalls | Should -HaveCount 1 @@ -51,6 +51,27 @@ Describe 'Enable-WindowsFeature' { $global:OptionalFeatureCalls[0].NoRestart | Should -BeTrue } + It 'writes optional-feature output while returning true' { + Mock Invoke-NonBlocking { + param($ScriptBlock, $ArgumentList) + & $ScriptBlock $ArgumentList + } + Mock Enable-WindowsOptionalFeature { [PSCustomObject]@{ State = 'Enabled'; RestartNeeded = $false } } + + Enable-WindowsFeature -FeatureName 'Feature.One' | Should -BeTrue + + Should -Invoke Write-Host -Times 1 -Exactly -ParameterFilter { $Object -match 'Enabled' } + } + + It 'reports the worker error and returns false' { + Mock Invoke-NonBlocking { [PSCustomObject]@{ Success = $false; Output = $null; Error = 'feature servicing failed' } } + Mock Write-Warning {} + + Enable-WindowsFeature -FeatureName 'Feature.One' | Should -BeFalse + + Should -Invoke Write-Warning -Times 1 -Exactly -ParameterFilter { $Message -match 'feature servicing failed' } + } + It 'does not schedule changes in WhatIf mode' { $script:Params = @{ WhatIf = $true } @@ -69,19 +90,19 @@ Describe 'Enable-WindowsFeature' { Enable-WindowsFeature -FeatureName 'Feature.One' - { & $script:optionalFeatureBlock $script:optionalFeatureArguments } | Should -Throw 'feature servicing failed' + (& $script:optionalFeatureBlock $script:optionalFeatureArguments).Success | Should -BeFalse } } Describe 'Disable-WindowsFeature' { BeforeEach { $script:Params = @{} - Mock Invoke-NonBlocking { @() } + Mock Invoke-NonBlocking { [PSCustomObject]@{ Success = $true; Output = $null; Error = $null } } Mock Write-Host {} } It 'schedules the requested feature with the non-blocking runner' { - Disable-WindowsFeature -FeatureName 'Feature.One' + Disable-WindowsFeature -FeatureName 'Feature.One' | Should -BeTrue Should -Invoke Invoke-NonBlocking -Times 1 -Exactly -ParameterFilter { $ArgumentList -eq 'Feature.One' } } @@ -93,7 +114,7 @@ Describe 'Disable-WindowsFeature' { $script:optionalFeatureBlock = $ScriptBlock $script:optionalFeatureArguments = $ArgumentList } - Disable-WindowsFeature -FeatureName 'Feature.One' + Disable-WindowsFeature -FeatureName 'Feature.One' | Should -BeFalse & $script:optionalFeatureBlock $script:optionalFeatureArguments $global:OptionalFeatureCalls | Should -HaveCount 1 @@ -104,6 +125,27 @@ Describe 'Disable-WindowsFeature' { $global:OptionalFeatureCalls[0].NoRestart | Should -BeTrue } + It 'writes optional-feature output while returning true' { + Mock Invoke-NonBlocking { + param($ScriptBlock, $ArgumentList) + & $ScriptBlock $ArgumentList + } + Mock Disable-WindowsOptionalFeature { [PSCustomObject]@{ State = 'Disabled'; RestartNeeded = $false } } + + Disable-WindowsFeature -FeatureName 'Feature.One' | Should -BeTrue + + Should -Invoke Write-Host -Times 1 -Exactly -ParameterFilter { $Object -match 'Disabled' } + } + + It 'reports the worker error and returns false' { + Mock Invoke-NonBlocking { [PSCustomObject]@{ Success = $false; Output = $null; Error = 'feature servicing failed' } } + Mock Write-Warning {} + + Disable-WindowsFeature -FeatureName 'Feature.One' | Should -BeFalse + + Should -Invoke Write-Warning -Times 1 -Exactly -ParameterFilter { $Message -match 'feature servicing failed' } + } + It 'does not schedule changes in WhatIf mode' { $script:Params = @{ WhatIf = $true } @@ -122,7 +164,7 @@ Describe 'Disable-WindowsFeature' { Disable-WindowsFeature -FeatureName 'Feature.One' - { & $script:optionalFeatureBlock $script:optionalFeatureArguments } | Should -Throw 'feature servicing failed' + (& $script:optionalFeatureBlock $script:optionalFeatureArguments).Success | Should -BeFalse } }