mirror of
https://github.com/Raphire/Win11Debloat.git
synced 2026-05-19 12:16:17 +00:00
Refactor ImportRegistryFile function to improve offline hive handling and error reporting
This commit is contained in:
@@ -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
|
||||
}
|
||||
Reference in New Issue
Block a user