From 05bd5f4c56b389b088ddcfa82fc8057958e736b1 Mon Sep 17 00:00:00 2001 From: Jeffrey <9938813+Raphire@users.noreply.github.com> Date: Sun, 17 May 2026 23:16:32 +0200 Subject: [PATCH] Refactor Invoke-RegistryOperationsFromRegFile to optimize operation handling and improve access restriction reporting --- Scripts/Helpers/ApplyRegistryRegFile.ps1 | 12 +++++++++--- 1 file changed, 9 insertions(+), 3 deletions(-) diff --git a/Scripts/Helpers/ApplyRegistryRegFile.ps1 b/Scripts/Helpers/ApplyRegistryRegFile.ps1 index 68bc88c..f0ba0e0 100644 --- a/Scripts/Helpers/ApplyRegistryRegFile.ps1 +++ b/Scripts/Helpers/ApplyRegistryRegFile.ps1 @@ -135,8 +135,10 @@ function Invoke-RegistryOperationsFromRegFile { ) $accessDeniedCount = 0 + $operations = @(Get-RegFileOperations -regFilePath $RegFilePath) + $totalOperations = $operations.Count - foreach ($operation in @(Get-RegFileOperations -regFilePath $RegFilePath)) { + foreach ($operation in $operations) { try { $keyInfo = Get-RegistryKeyForOperation -RegistryPath $operation.KeyPath -CreateIfMissing:($operation.OperationType -eq 'SetValue') -OpenKey:($operation.OperationType -ne 'DeleteKey') @@ -193,6 +195,10 @@ function Invoke-RegistryOperationsFromRegFile { } } + if ($totalOperations -gt 0 -and $accessDeniedCount -eq $totalOperations) { + throw "Registry fallback import could not apply any operations in '$RegFilePath' because all $accessDeniedCount operation(s) were blocked by access restrictions." + } + if ($accessDeniedCount -gt 0) { Write-Warning "Registry fallback import completed with $accessDeniedCount access-restricted operation(s) skipped in '$RegFilePath'." } @@ -213,7 +219,7 @@ function Invoke-RegistryImportViaPowerShell { } if ($UseOfflineHive) { - if (Get-Command -Name Invoke-WithLoadedBackupHive -ErrorAction SilentlyContinue) { + if ((Get-Command -Name Invoke-WithLoadedBackupHive -ErrorAction SilentlyContinue) -and -not $HiveAlreadyLoaded.IsPresent) { return Invoke-WithLoadedBackupHive -ScriptBlock $applyScript -ArgumentObject $RegFilePath } @@ -221,7 +227,7 @@ function Invoke-RegistryImportViaPowerShell { throw "Offline hive path was not provided for fallback import of '$RegFilePath'" } - if (-not $HiveAlreadyLoaded) { + if (-not $HiveAlreadyLoaded.IsPresent) { $global:LASTEXITCODE = 0 reg load "HKU\Default" $OfflineHiveDatPath | Out-Null $loadExitCode = $LASTEXITCODE