Refactor ImportRegistryFile function to improve offline hive handling and error reporting

This commit is contained in:
Jeffrey
2026-05-18 21:14:20 +02:00
parent b0b4615718
commit d60e52bdff
2 changed files with 85 additions and 151 deletions

View File

@@ -268,53 +268,3 @@ function Invoke-RegistryOperationsFromRegFile {
Write-Warning "Registry fallback import completed with $accessDeniedCount access-restricted operation(s) skipped in '$RegFilePath'."
}
}
function Invoke-RegistryImportViaPowerShell {
param(
[Parameter(Mandatory)]
[string]$RegFilePath,
[switch]$UseOfflineHive,
[string]$OfflineHiveDatPath,
[switch]$HiveAlreadyLoaded
)
$applyScript = {
param($targetRegFilePath)
Invoke-RegistryOperationsFromRegFile -RegFilePath $targetRegFilePath
}
$hiveAlreadyLoaded = $HiveAlreadyLoaded.IsPresent
if ($UseOfflineHive) {
if ((Get-Command -Name Invoke-WithLoadedBackupHive -ErrorAction SilentlyContinue) -and -not $hiveAlreadyLoaded) {
return Invoke-WithLoadedBackupHive -ScriptBlock $applyScript -ArgumentObject $RegFilePath
}
if ([string]::IsNullOrWhiteSpace($OfflineHiveDatPath)) {
throw "Offline hive path was not provided for fallback import of '$RegFilePath'"
}
if (-not $hiveAlreadyLoaded) {
$global:LASTEXITCODE = 0
reg load "HKU\Default" $OfflineHiveDatPath | Out-Null
$loadExitCode = $LASTEXITCODE
if ($loadExitCode -ne 0) {
throw "Failed to load user hive at '$OfflineHiveDatPath' for fallback import (exit code: $loadExitCode)"
}
}
try {
return & $applyScript $RegFilePath
}
finally {
$global:LASTEXITCODE = 0
reg unload "HKU\Default" | Out-Null
$unloadExitCode = $LASTEXITCODE
if ($unloadExitCode -ne 0) {
Write-Warning "Fallback import completed, but unloading HKU\Default failed (exit code: $unloadExitCode)."
}
}
}
return & $applyScript $RegFilePath
}