mirror of
https://github.com/Raphire/Win11Debloat.git
synced 2026-05-22 13:46:17 +00:00
Refactor ImportRegistryFile function to simplify error handling and improve fallback logic
This commit is contained in:
@@ -24,8 +24,6 @@ function ImportRegistryFile {
|
|||||||
}
|
}
|
||||||
|
|
||||||
$regResult = $null
|
$regResult = $null
|
||||||
$operationErrorMessage = $null
|
|
||||||
$cleanupErrorMessage = $null
|
|
||||||
$offlineHiveLoaded = $false
|
$offlineHiveLoaded = $false
|
||||||
|
|
||||||
try {
|
try {
|
||||||
@@ -94,20 +92,17 @@ function ImportRegistryFile {
|
|||||||
|
|
||||||
if (-not $hasSuccess) {
|
if (-not $hasSuccess) {
|
||||||
$details = if ($regResult.Error) { $regResult.Error } else { "Exit code: $($regResult.ExitCode)" }
|
$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"
|
Write-Warning "reg import failed for '$path'. Falling back to PowerShell registry writer. Details: $details"
|
||||||
|
Invoke-RegistryOperationsFromRegFile -RegFilePath $regFilePath
|
||||||
try {
|
Write-Host "Fallback import succeeded for '$path'." -ForegroundColor Yellow
|
||||||
Invoke-RegistryOperationsFromRegFile -RegFilePath $regFilePath
|
|
||||||
Write-Host "Fallback import succeeded for '$path'." -ForegroundColor Yellow
|
|
||||||
}
|
|
||||||
catch {
|
|
||||||
throw "Failed importing registry file '$path'. reg import error: $details. PowerShell fallback error: $($_.Exception.Message)"
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Write-Host ""
|
||||||
}
|
}
|
||||||
catch {
|
catch {
|
||||||
$operationErrorMessage = $_.Exception.Message
|
Write-Host $_.Exception.Message -ForegroundColor Red
|
||||||
|
Write-Host ""
|
||||||
|
throw
|
||||||
}
|
}
|
||||||
finally {
|
finally {
|
||||||
if ($offlineHiveLoaded) {
|
if ($offlineHiveLoaded) {
|
||||||
@@ -116,29 +111,8 @@ function ImportRegistryFile {
|
|||||||
$unloadExitCode = $LASTEXITCODE
|
$unloadExitCode = $LASTEXITCODE
|
||||||
|
|
||||||
if ($unloadExitCode -ne 0) {
|
if ($unloadExitCode -ne 0) {
|
||||||
$cleanupErrorMessage = "Failed to unload registry hive HKU\Default after importing '$path' (exit code: $unloadExitCode)"
|
throw "Failed to unload registry hive HKU\Default after importing '$path' (exit code: $unloadExitCode)"
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
if ($cleanupErrorMessage) {
|
|
||||||
$errorMessage = if ($operationErrorMessage) {
|
|
||||||
"$operationErrorMessage Cleanup error: $cleanupErrorMessage"
|
|
||||||
}
|
|
||||||
else {
|
|
||||||
$cleanupErrorMessage
|
|
||||||
}
|
|
||||||
|
|
||||||
Write-Host $errorMessage -ForegroundColor Red
|
|
||||||
Write-Host ""
|
|
||||||
throw $errorMessage
|
|
||||||
}
|
|
||||||
|
|
||||||
if ($operationErrorMessage) {
|
|
||||||
Write-Host $operationErrorMessage -ForegroundColor Red
|
|
||||||
Write-Host ""
|
|
||||||
throw $operationErrorMessage
|
|
||||||
}
|
|
||||||
|
|
||||||
Write-Host ""
|
|
||||||
}
|
}
|
||||||
Reference in New Issue
Block a user