From a2d63c7c1b9c7cb6554d4fce3aa7f377567f7f9e Mon Sep 17 00:00:00 2001 From: Jeffrey <9938813+Raphire@users.noreply.github.com> Date: Sun, 18 Oct 2026 23:07:42 +0200 Subject: [PATCH] fix: ignore 0x80070002 uninstall errors in Windows 10 (#835) --- Scripts/AppRemoval/Remove-SelectedApps.ps1 | 77 +++++++- Tests/Remove-SelectedApps.Tests.ps1 | 195 ++++++++++++++++++++- 2 files changed, 264 insertions(+), 8 deletions(-) diff --git a/Scripts/AppRemoval/Remove-SelectedApps.ps1 b/Scripts/AppRemoval/Remove-SelectedApps.ps1 index 7ed5b7d..897719c 100644 --- a/Scripts/AppRemoval/Remove-SelectedApps.ps1 +++ b/Scripts/AppRemoval/Remove-SelectedApps.ps1 @@ -253,14 +253,47 @@ function Remove-AppxApp { param([string]$app, [string]$targetUser) $appPattern = '*' + $app + '*' + $isWindows10 = $script:WinVersion -ge 10240 -and $script:WinVersion -lt 22000 try { $removalResult = Invoke-NonBlocking -ScriptBlock { - param($pattern, $target) + param($pattern, $target, $isWindows10) $removalErrors = @() + $ignoredRemovalMessages = [System.Collections.Generic.List[string]]::new() + $verificationWarnings = [System.Collections.Generic.List[string]]::new() $getPackageParams = @{ Name = $pattern; ErrorAction = 'Continue'; ErrorVariable = '+removalErrors' } - $removePackageParams = @{ ErrorAction = 'Continue'; ErrorVariable = '+removalErrors' } + $removePackageParams = @{ ErrorAction = 'Stop' } + + # Windows 10 can report file-not-found after successfully removing an Appx package. + # Suppress it only after confirming the exact package is absent in the same scope. + function Test-IgnorableAppxRemovalError { + param($RemovalError, [string]$PackageName, [switch]$Provisioned) + if (-not ($isWindows10 -and ($RemovalError.Exception.HResult -eq -2147024894 -or + $RemovalError.ToString() -match '\b0x80070002\b'))) { + return $false + } + + try { + if ($Provisioned) { + $remainingPackages = @(Get-AppxProvisionedPackage -Online -ErrorAction Stop | + Where-Object { $_.PackageName -eq $PackageName }) + } + else { + $verificationParams = $getPackageParams.Clone() + $verificationParams.Remove('ErrorVariable') + $verificationParams.ErrorAction = 'Stop' + $remainingPackages = @(Get-AppxPackage @verificationParams | + Where-Object { $_.PackageFullName -eq $PackageName }) + } + return ($remainingPackages.Count -eq 0) + } + catch { + # An unsuccessful query cannot confirm removal; preserve the original error. + $verificationWarnings.Add("Unable to verify removal of $PackageName (target: $target, provisioned: $Provisioned): $_") + return $false + } + } switch ($target) { 'AllUsers' { @@ -278,24 +311,56 @@ function Remove-AppxApp { foreach ($package in @(Get-AppxPackage @getPackageParams)) { $removePackageParams.Package = $package.PackageFullName - $null = Remove-AppxPackage @removePackageParams + try { + $null = Remove-AppxPackage @removePackageParams + } + catch { + if (Test-IgnorableAppxRemovalError $_ -PackageName $package.PackageFullName) { + $ignoredRemovalMessages.Add("Windows 10 Appx removal reported error 0x80070002 for $($package.PackageFullName), but the removal succeeded.") + } + else { + Write-Error -ErrorRecord $_ -ErrorAction Continue -ErrorVariable +removalErrors + } + } } if ($target -eq 'AllUsers') { $provisionedPackages = @(Get-AppxProvisionedPackage -Online -ErrorAction Continue -ErrorVariable +removalErrors | Where-Object { $_.PackageName -like $pattern }) foreach ($package in $provisionedPackages) { - $null = Remove-ProvisionedAppxPackage -Online -AllUsers -PackageName $package.PackageName -ErrorAction Continue -ErrorVariable +removalErrors + try { + $null = Remove-ProvisionedAppxPackage -Online -AllUsers -PackageName $package.PackageName -ErrorAction Stop + } + catch { + if (Test-IgnorableAppxRemovalError $_ -PackageName $package.PackageName -Provisioned) { + $ignoredRemovalMessages.Add("Windows 10 Appx removal reported error 0x80070002 for $($package.PackageName), but the removal succeeded.") + } + else { + Write-Error -ErrorRecord $_ -ErrorAction Continue -ErrorVariable +removalErrors + } + } } } - return [PSCustomObject]@{ Success = ($removalErrors.Count -eq 0) } - } -ArgumentList @($appPattern, $targetUser) + return [PSCustomObject]@{ + Success = ($removalErrors.Count -eq 0) + IgnoredRemovalMessages = $ignoredRemovalMessages.ToArray() + VerificationWarnings = $verificationWarnings.ToArray() + } + } -ArgumentList @($appPattern, $targetUser, $isWindows10) } catch { Write-Error "Unable to remove $app via Appx: $_" return $false } + # Write in the caller's runspace so the active transcript captures these messages in GUI mode too. + foreach ($message in @($removalResult.IgnoredRemovalMessages)) { + if ($message) { Write-Host $message } + } + foreach ($message in @($removalResult.VerificationWarnings)) { + if ($message) { Write-Warning $message } + } + return [bool]($removalResult -and $removalResult.Success) } diff --git a/Tests/Remove-SelectedApps.Tests.ps1 b/Tests/Remove-SelectedApps.Tests.ps1 index 25a7692..6ad7d96 100644 --- a/Tests/Remove-SelectedApps.Tests.ps1 +++ b/Tests/Remove-SelectedApps.Tests.ps1 @@ -340,7 +340,12 @@ Describe 'Remove-EdgeAutostartValue' { } Describe 'Remove-AppxApp' { - BeforeEach { Mock Invoke-NonBlocking { [PSCustomObject]@{ Success = $true } } } + BeforeEach { + $script:WinVersion = 26100 + Mock Invoke-NonBlocking { [PSCustomObject]@{ Success = $true } } + Mock Write-Host {} + Mock Write-Warning {} + } It 'passes the wildcard and target user data for ' -ForEach @( @{ Target = 'AllUsers' } @@ -349,7 +354,7 @@ Describe 'Remove-AppxApp' { ) { Remove-AppxApp -app 'One.App' -targetUser $Target Should -Invoke Invoke-NonBlocking -Times 1 -Exactly -ParameterFilter { - @($ArgumentList).Count -eq 2 -and @($ArgumentList)[0] -eq '*One.App*' -and @($ArgumentList)[1] -eq $Target + @($ArgumentList).Count -eq 3 -and @($ArgumentList)[0] -eq '*One.App*' -and @($ArgumentList)[1] -eq $Target -and @($ArgumentList)[2] -eq $false } } @@ -378,6 +383,192 @@ Describe 'Remove-AppxApp' { } } + It 'handles error on build with success ' -ForEach @( + @{ Operation = 'Appx'; Code = '0x80070002'; Build = 19045; Expected = $true } + @{ Operation = 'Provisioned'; Code = '0x80070002'; Build = 19045; Expected = $true } + @{ Operation = 'Appx'; Code = '0x80070002'; Build = 22000; Expected = $false } + @{ Operation = 'Provisioned'; Code = '0x80070002'; Build = 26100; Expected = $false } + @{ Operation = 'Appx'; Code = '0x80070005'; Build = 19045; Expected = $false } + @{ Operation = 'Provisioned'; Code = '0x80070005'; Build = 19045; Expected = $false } + @{ Operation = 'Appx'; Code = '0x80070002'; Build = $null; Expected = $false } + ) { + $script:WinVersion = $Build + Mock Invoke-NonBlocking { param($ScriptBlock, $ArgumentList) & $ScriptBlock @ArgumentList } + Mock Get-AppxPackage { [PSCustomObject]@{ PackageFullName = 'One.App_1.0' } } + Mock Get-AppxProvisionedPackage { [PSCustomObject]@{ PackageName = 'One.App_1.0' } } + Mock Get-AppxPackage {} -ParameterFilter { $ErrorAction -eq 'Stop' } + Mock Get-AppxProvisionedPackage {} -ParameterFilter { $ErrorAction -eq 'Stop' } + Mock Remove-AppxPackage {} + Mock Remove-ProvisionedAppxPackage {} + if ($Operation -eq 'Appx') { + Mock Remove-AppxPackage { Write-Error "Deployment failed with HRESULT: $Code" } + } + else { + Mock Remove-ProvisionedAppxPackage { Write-Error "Deployment failed with HRESULT: $Code" } + } + + $output = @(Remove-AppxApp -app 'One.App' -targetUser 'AllUsers' 2>&1) + + $output[-1] | Should -Be $Expected + @($output | Where-Object { $_ -is [System.Management.Automation.ErrorRecord] }).Count | Should -Be $(if ($Expected) { 0 } else { 1 }) + Should -Invoke Write-Host -Times $(if ($Expected) { 1 } else { 0 }) -Exactly -ParameterFilter { + $Object -eq 'Windows 10 Appx removal reported error 0x80070002 for One.App_1.0, but the removal succeeded.' + } + } + + It 'recognizes a Windows 10 removal HRESULT without relying on English error text' { + $script:WinVersion = 19045 + Mock Invoke-NonBlocking { param($ScriptBlock, $ArgumentList) & $ScriptBlock @ArgumentList } + Mock Get-AppxPackage { [PSCustomObject]@{ PackageFullName = 'One.App_1.0' } } + Mock Get-AppxPackage {} -ParameterFilter { $ErrorAction -eq 'Stop' } + Mock Remove-AppxPackage { throw [System.Runtime.InteropServices.COMException]::new('Localized message', -2147024894) } + + $output = @(Remove-AppxApp -app 'One.App' -targetUser 'CurrentUser' 2>&1) + + $output | Should -HaveCount 1 + $output[0] | Should -BeTrue + } + + It 'verifies removal for when the query reports ' -ForEach @( + @{ Operation = 'Appx'; Target = 'CurrentUser'; State = 'Absent' } + @{ Operation = 'Appx'; Target = 'CurrentUser'; State = 'Present' } + @{ Operation = 'Appx'; Target = 'CurrentUser'; State = 'QueryError' } + @{ Operation = 'Appx'; Target = 'AllUsers'; State = 'Absent' } + @{ Operation = 'Appx'; Target = 'AllUsers'; State = 'Present' } + @{ Operation = 'Appx'; Target = 'AllUsers'; State = 'QueryError' } + @{ Operation = 'Appx'; Target = 'NamedUser'; State = 'Absent' } + @{ Operation = 'Appx'; Target = 'NamedUser'; State = 'Present' } + @{ Operation = 'Appx'; Target = 'NamedUser'; State = 'QueryError' } + @{ Operation = 'Provisioned'; Target = 'AllUsers'; State = 'Absent' } + @{ Operation = 'Provisioned'; Target = 'AllUsers'; State = 'Present' } + @{ Operation = 'Provisioned'; Target = 'AllUsers'; State = 'QueryError' } + ) { + $script:WinVersion = 19045 + Mock Invoke-NonBlocking { param($ScriptBlock, $ArgumentList) & $ScriptBlock @ArgumentList } + Mock Get-AppxPackage { + if ($Operation -eq 'Appx') { [PSCustomObject]@{ PackageFullName = 'One.App_1.0' } } + } + Mock Get-AppxProvisionedPackage { + if ($Operation -eq 'Provisioned') { [PSCustomObject]@{ PackageName = 'One.App_1.0' } } + } + Mock Get-AppxPackage { + if ($State -eq 'QueryError') { Write-Error 'Verification failed' } + if ($State -eq 'Present') { [PSCustomObject]@{ PackageFullName = 'One.App_1.0' } } + [PSCustomObject]@{ PackageFullName = 'One.App_2.0' } + } -ParameterFilter { $ErrorAction -eq 'Stop' } + Mock Get-AppxProvisionedPackage { + if ($State -eq 'QueryError') { Write-Error 'Verification failed' } + if ($State -eq 'Present') { [PSCustomObject]@{ PackageName = 'One.App_1.0' } } + [PSCustomObject]@{ PackageName = 'One.App_2.0' } + } -ParameterFilter { $ErrorAction -eq 'Stop' } + Mock Remove-AppxPackage { Write-Error 'Deployment failed with HRESULT: 0x80070002' } + Mock Remove-ProvisionedAppxPackage { Write-Error 'Deployment failed with HRESULT: 0x80070002' } + + $targetUser = $Target + if ($Target -eq 'NamedUser') { + $identity = [System.Security.Principal.WindowsIdentity]::GetCurrent() + $targetUser = $identity.Name + $expectedSid = $identity.User.Value + } + $output = @(Remove-AppxApp -app 'One.App' -targetUser $targetUser 2>&1) + + $output[-1] | Should -Be ($State -eq 'Absent') + $errors = @($output | Where-Object { $_ -is [System.Management.Automation.ErrorRecord] }) + $errors.Count | Should -Be $(if ($State -eq 'Absent') { 0 } else { 1 }) + if ($errors.Count) { $errors[0].ToString() | Should -Match '0x80070002' } + Should -Invoke Write-Warning -Times $(if ($State -eq 'QueryError') { 1 } else { 0 }) -Exactly + if ($State -eq 'QueryError') { + Should -Invoke Write-Warning -Times 1 -Exactly -ParameterFilter { + $Message -like '*Unable to verify removal of One.App_1.0*Verification failed*' + } + } + Should -Invoke Write-Host -Times $(if ($State -eq 'Absent') { 1 } else { 0 }) -Exactly + if ($Operation -eq 'Provisioned') { + Should -Invoke Get-AppxProvisionedPackage -Times 1 -Exactly -ParameterFilter { + $Online -and $ErrorAction -eq 'Stop' + } + } + else { + Should -Invoke Get-AppxPackage -Times 1 -Exactly -ParameterFilter { + $Name -eq '*One.App*' -and $ErrorAction -eq 'Stop' -and -not $ErrorVariable -and + (($Target -eq 'AllUsers' -and $AllUsers -and -not $User) -or + ($Target -eq 'CurrentUser' -and -not $AllUsers -and -not $User) -or + ($Target -eq 'NamedUser' -and -not $AllUsers -and $User -eq $expectedSid)) + } + } + } + + It 'does not ignore Windows 10 package discovery errors with the same code' { + $script:WinVersion = 19045 + Mock Invoke-NonBlocking { param($ScriptBlock, $ArgumentList) & $ScriptBlock @ArgumentList } + Mock Get-AppxPackage { Write-Error 'Discovery failed: 0x80070002' } + + $output = @(Remove-AppxApp -app 'One.App' -targetUser 'CurrentUser' 2>&1) + + $output[-1] | Should -BeFalse + @($output | Where-Object { $_ -is [System.Management.Automation.ErrorRecord] }) | Should -HaveCount 1 + } + + It 'preserves the error handling in a background runspace on build ' -ForEach @( + @{ Build = 19045; Expected = $true } + @{ Build = 26100; Expected = $false } + @{ Build = 19045; Expected = $false; VerificationFails = $true } + ) { + $script:WinVersion = $Build + Mock Invoke-NonBlocking { + param($ScriptBlock, $ArgumentList) + $script:appxWorker = $ScriptBlock.ToString() + $script:appxWorkerArguments = $ArgumentList + [PSCustomObject]@{ Success = $true } + } + $null = Remove-AppxApp -app 'One.App' -targetUser 'CurrentUser' + $workerPowerShell = [powershell]::Create() + try { + $workerSetup = { + param($worker, $workerArguments, $verificationFails) + function Get-AppxPackage { + [CmdletBinding()] param($Name) + if ($script:packageRemoved -and $verificationFails) { throw 'Verification failed' } + if (-not $script:packageRemoved) { [PSCustomObject]@{ PackageFullName = 'One.App_1.0' } } + } + function Remove-AppxPackage { + [CmdletBinding()] param($Package) + $script:packageRemoved = $true + Write-Error 'Deployment failed with HRESULT: 0x80070002' + } + & ([scriptblock]::Create($worker)) @workerArguments + } + $null = $workerPowerShell.AddScript($workerSetup.ToString()).AddArgument($script:appxWorker).AddArgument($script:appxWorkerArguments).AddArgument([bool]$VerificationFails) + $output = @($workerPowerShell.Invoke()) + + $output[-1].Success | Should -Be $Expected + $workerPowerShell.Streams.Error.Count | Should -Be $(if ($Expected) { 0 } else { 1 }) + @($output[-1].IgnoredRemovalMessages).Count | Should -Be $(if ($Expected) { 1 } else { 0 }) + @($output[-1].VerificationWarnings).Count | Should -Be $(if ($VerificationFails) { 1 } else { 0 }) + if ($VerificationFails) { + $workerPowerShell.Streams.Error[0].ToString() | Should -Match '0x80070002' + $script:appxWorkerResult = $output[-1] + Mock Invoke-NonBlocking { $script:appxWorkerResult } + Remove-AppxApp -app 'One.App' -targetUser 'CurrentUser' | Should -BeFalse + Should -Invoke Write-Warning -Times 1 -Exactly -ParameterFilter { + $Message -like '*Unable to verify removal of One.App_1.0*Verification failed*' + } + } + if ($Expected) { + # Replay the actual worker result through the caller, where transcription runs. + $script:appxWorkerResult = $output[-1] + Mock Invoke-NonBlocking { $script:appxWorkerResult } + Remove-AppxApp -app 'One.App' -targetUser 'CurrentUser' | Should -BeTrue + Should -Invoke Write-Host -Times 1 -Exactly -ParameterFilter { + $Object -eq 'Windows 10 Appx removal reported error 0x80070002 for One.App_1.0, but the removal succeeded.' + } + } + } + finally { + $workerPowerShell.Dispose() + } + } + } Describe 'Set-RunOnceWingetTask' {