2026-03-07 20:28:48 +01:00
|
|
|
# Import & execute regfile
|
|
|
|
|
function ImportRegistryFile {
|
|
|
|
|
param (
|
|
|
|
|
$message,
|
|
|
|
|
$path
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
Write-Host $message
|
|
|
|
|
|
2026-03-18 22:49:44 +01:00
|
|
|
$usesOfflineHive = $script:Params.ContainsKey("Sysprep") -or $script:Params.ContainsKey("User")
|
2026-05-18 21:14:20 +02:00
|
|
|
$regFileDirectory = if ($usesOfflineHive) {
|
|
|
|
|
Join-Path $script:RegfilesPath "Sysprep"
|
2026-03-18 22:49:44 +01:00
|
|
|
}
|
|
|
|
|
else {
|
2026-05-18 21:14:20 +02:00
|
|
|
$script:RegfilesPath
|
2026-03-18 22:49:44 +01:00
|
|
|
}
|
2026-05-18 21:14:20 +02:00
|
|
|
$regFilePath = Join-Path $regFileDirectory $path
|
2026-03-18 22:49:44 +01:00
|
|
|
|
|
|
|
|
if (-not (Test-Path $regFilePath)) {
|
|
|
|
|
$errorMessage = "Unable to find registry file: $path ($regFilePath)"
|
|
|
|
|
Write-Host "Error: $errorMessage" -ForegroundColor Red
|
2026-03-07 20:28:48 +01:00
|
|
|
Write-Host ""
|
2026-03-18 22:49:44 +01:00
|
|
|
throw $errorMessage
|
2026-03-07 20:28:48 +01:00
|
|
|
}
|
|
|
|
|
|
2026-05-18 21:14:20 +02:00
|
|
|
$regResult = $null
|
|
|
|
|
$offlineHiveLoaded = $false
|
|
|
|
|
|
|
|
|
|
try {
|
|
|
|
|
if ($usesOfflineHive) {
|
|
|
|
|
# Sysprep targets Default user, User targets the specified user
|
|
|
|
|
$targetUserName = if ($script:Params.ContainsKey("Sysprep")) { "Default" } else { $script:Params.Item("User") }
|
|
|
|
|
$hiveDatPath = GetUserDirectory -userName $targetUserName -fileName "NTUSER.DAT"
|
|
|
|
|
|
|
|
|
|
$global:LASTEXITCODE = 0
|
|
|
|
|
reg load "HKU\Default" $hiveDatPath | Out-Null
|
|
|
|
|
$loadExitCode = $LASTEXITCODE
|
|
|
|
|
|
|
|
|
|
if ($loadExitCode -ne 0) {
|
|
|
|
|
throw "Failed importing registry file '$path'. Offline hive load failed: Failed to load user hive at '$hiveDatPath' (exit code: $loadExitCode)"
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
$offlineHiveLoaded = $true
|
2026-03-07 20:28:48 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
$regResult = Invoke-NonBlocking -ScriptBlock {
|
2026-05-18 21:14:20 +02:00
|
|
|
param($targetRegFilePath)
|
2026-03-18 22:49:44 +01:00
|
|
|
$result = @{
|
|
|
|
|
Output = @()
|
|
|
|
|
ExitCode = 0
|
|
|
|
|
Error = $null
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
try {
|
|
|
|
|
$global:LASTEXITCODE = 0
|
|
|
|
|
$output = reg import $targetRegFilePath 2>&1
|
|
|
|
|
$importExitCode = $LASTEXITCODE
|
|
|
|
|
|
|
|
|
|
if ($output) {
|
|
|
|
|
$result.Output = @($output)
|
|
|
|
|
}
|
|
|
|
|
$result.ExitCode = $importExitCode
|
|
|
|
|
|
|
|
|
|
if ($importExitCode -ne 0) {
|
|
|
|
|
throw "Registry import failed with exit code $importExitCode for '$targetRegFilePath'"
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
catch {
|
|
|
|
|
$result.Error = $_.Exception.Message
|
|
|
|
|
$result.ExitCode = if ($LASTEXITCODE -ne 0) { $LASTEXITCODE } else { 1 }
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return $result
|
|
|
|
|
} -ArgumentList $regFilePath
|
2026-03-07 20:28:48 +01:00
|
|
|
|
2026-05-18 21:14:20 +02:00
|
|
|
$regOutput = @($regResult.Output)
|
|
|
|
|
$hasSuccess = ($regResult.ExitCode -eq 0) -and -not $regResult.Error
|
|
|
|
|
|
|
|
|
|
if ($regOutput) {
|
|
|
|
|
foreach ($line in $regOutput) {
|
|
|
|
|
$lineText = if ($line -is [System.Management.Automation.ErrorRecord]) { $line.Exception.Message } else { $line.ToString() }
|
|
|
|
|
if ($lineText -and $lineText.Length -gt 0) {
|
|
|
|
|
if ($hasSuccess) {
|
|
|
|
|
Write-Host $lineText
|
|
|
|
|
}
|
|
|
|
|
else {
|
|
|
|
|
Write-Host $lineText -ForegroundColor Red
|
|
|
|
|
}
|
2026-03-07 20:28:48 +01:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
2026-05-17 18:27:29 +02:00
|
|
|
|
2026-05-18 21:14:20 +02:00
|
|
|
if (-not $hasSuccess) {
|
|
|
|
|
$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"
|
2026-05-18 21:50:11 +02:00
|
|
|
Invoke-RegistryOperationsFromRegFile -RegFilePath $regFilePath
|
|
|
|
|
Write-Host "Fallback import succeeded for '$path'." -ForegroundColor Yellow
|
2026-05-17 17:24:08 +02:00
|
|
|
}
|
2026-05-18 21:50:11 +02:00
|
|
|
|
|
|
|
|
Write-Host ""
|
2026-05-18 21:14:20 +02:00
|
|
|
}
|
|
|
|
|
catch {
|
2026-05-18 21:50:11 +02:00
|
|
|
Write-Host $_.Exception.Message -ForegroundColor Red
|
|
|
|
|
Write-Host ""
|
|
|
|
|
throw
|
2026-05-18 21:14:20 +02:00
|
|
|
}
|
|
|
|
|
finally {
|
|
|
|
|
if ($offlineHiveLoaded) {
|
|
|
|
|
$global:LASTEXITCODE = 0
|
|
|
|
|
reg unload "HKU\Default" | Out-Null
|
|
|
|
|
$unloadExitCode = $LASTEXITCODE
|
|
|
|
|
|
|
|
|
|
if ($unloadExitCode -ne 0) {
|
2026-05-18 21:50:11 +02:00
|
|
|
throw "Failed to unload registry hive HKU\Default after importing '$path' (exit code: $unloadExitCode)"
|
2026-05-17 23:25:03 +02:00
|
|
|
}
|
2026-05-18 21:14:20 +02:00
|
|
|
}
|
|
|
|
|
}
|
2026-03-07 20:28:48 +01:00
|
|
|
}
|