fix: ignore 0x80070002 uninstall errors in Windows 10 (#835)

This commit is contained in:
Jeffrey
2026-10-18 23:07:42 +02:00
committed by GitHub
parent a454bf92a3
commit a2d63c7c1b
2 changed files with 264 additions and 8 deletions
+193 -2
View File
@@ -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 <Target>' -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 <Operation> error <Code> on build <Build> with success <Expected>' -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 <Operation> removal for <Target> when the query reports <State>' -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 <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' {