From 6797d6e4621efe13fbddda11956b1affa393f73c Mon Sep 17 00:00:00 2001 From: Marco Carvalho Date: Tue, 10 Nov 2026 19:21:59 -0300 Subject: [PATCH] Fix false reg import failure from success stderr banner (#892) --- Scripts/Features/Import-RegistryFile.ps1 | 9 +++- Tests/Import-RegistryFile.Tests.ps1 | 67 ++++++++++++++++++++++++ 2 files changed, 75 insertions(+), 1 deletion(-) diff --git a/Scripts/Features/Import-RegistryFile.ps1 b/Scripts/Features/Import-RegistryFile.ps1 index b856c7f..8379395 100644 --- a/Scripts/Features/Import-RegistryFile.ps1 +++ b/Scripts/Features/Import-RegistryFile.ps1 @@ -53,7 +53,14 @@ function Import-RegistryFile { try { $global:LASTEXITCODE = 0 - $output = reg import $targetRegFilePath 2>&1 + $previousEap = $ErrorActionPreference + $ErrorActionPreference = 'Continue' + try { + $output = reg import $targetRegFilePath 2>&1 + } + finally { + $ErrorActionPreference = $previousEap + } $importExitCode = $LASTEXITCODE if ($output) { diff --git a/Tests/Import-RegistryFile.Tests.ps1 b/Tests/Import-RegistryFile.Tests.ps1 index c1bbf9a..7e297c4 100644 --- a/Tests/Import-RegistryFile.Tests.ps1 +++ b/Tests/Import-RegistryFile.Tests.ps1 @@ -75,4 +75,71 @@ Describe 'Import-RegistryFile' { Should -Invoke Invoke-NonBlocking -Times 1 -Exactly Should -Invoke Invoke-RegistryOperationsFromRegFile -Times 0 -Exactly } + + It 'does not treat the reg import success banner as a failure' { + Mock Invoke-NonBlocking { + param($ScriptBlock, $ArgumentList) + & $ScriptBlock @ArgumentList + } + + function global:reg { + <# + .SYNOPSIS + Test stand-in for reg.exe that emits a localized success banner on the error stream. + + .DESCRIPTION + Mimics a successful Windows reg import under Windows PowerShell 5.1 by writing a + non-English success banner as a NativeCommandError and setting LASTEXITCODE to 0. + Import-RegistryFile must treat exit code 0 as success without matching banner text. + #> + [CmdletBinding()] + param( + [Parameter(ValueFromRemainingArguments = $true)] + [object[]]$Arguments + ) + + $exception = [System.Management.Automation.RemoteException]::new('A operação foi concluída com êxito.') + $errorRecord = [System.Management.Automation.ErrorRecord]::new( + $exception, + 'NativeCommandError', + [System.Management.Automation.ErrorCategory]::NotSpecified, + 'reg.exe' + ) + $PSCmdlet.WriteError($errorRecord) + $global:LASTEXITCODE = 0 + } + + $previousEap = $ErrorActionPreference + $ErrorActionPreference = 'Stop' + try { + Import-RegistryFile -message 'Apply' -path 'feature.reg' | Should -BeTrue + } + finally { + $ErrorActionPreference = $previousEap + Remove-Item -Path 'Function:\reg' + } + + Should -Invoke Invoke-NonBlocking -Times 1 -Exactly + Should -Invoke Invoke-RegistryOperationsFromRegFile -Times 0 -Exactly + Should -Invoke Write-Warning -Times 0 -Exactly + } + + It 'falls back to the PowerShell writer when reg cannot start' { + Mock Invoke-NonBlocking { + param($ScriptBlock, $ArgumentList) + & $ScriptBlock @ArgumentList + } + + $previousPath = $env:PATH + $env:PATH = '' + try { + Import-RegistryFile -message 'Apply' -path 'feature.reg' | Should -BeTrue + } + finally { + $env:PATH = $previousPath + } + + Should -Invoke Invoke-RegistryOperationsFromRegFile -Times 1 -Exactly + Should -Invoke Write-Warning -Times 1 -Exactly -ParameterFilter { $Message -like "reg import failed*" } + } }