2026-09-10 21:18:43 +05:30
|
|
|
<#
|
|
|
|
|
.SYNOPSIS
|
|
|
|
|
Extracts and deduplicates the SelectedFeatures list from a backup, validating each entry.
|
|
|
|
|
|
|
|
|
|
.DESCRIPTION
|
|
|
|
|
Returns an empty SelectedFeatures array with an error when the property is missing
|
|
|
|
|
entirely, and flags (without stopping) any entry that isn't a non-empty string.
|
|
|
|
|
#>
|
2026-05-08 21:19:52 +02:00
|
|
|
function Get-NormalizedSelectedFeatureIdsFromBackup {
|
|
|
|
|
param(
|
|
|
|
|
[Parameter(Mandatory)]
|
|
|
|
|
$Backup
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
$selectedFeatures = New-Object System.Collections.Generic.List[string]
|
|
|
|
|
$selectedFeatureIds = New-Object 'System.Collections.Generic.HashSet[string]' ([System.StringComparer]::OrdinalIgnoreCase)
|
|
|
|
|
$errors = New-Object System.Collections.Generic.List[string]
|
|
|
|
|
$hasInvalidSelectedFeatureId = $false
|
|
|
|
|
|
|
|
|
|
if (-not $Backup.PSObject.Properties['SelectedFeatures']) {
|
2026-09-10 21:18:43 +05:30
|
|
|
$errors.Add((Get-Translation -Key 'BackupMissingProperty' -FormatArgs @('SelectedFeatures')))
|
2026-05-08 21:19:52 +02:00
|
|
|
return [PSCustomObject]@{
|
|
|
|
|
SelectedFeatures = $selectedFeatures.ToArray()
|
|
|
|
|
Errors = $errors.ToArray()
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
foreach ($featureId in @($Backup.SelectedFeatures)) {
|
|
|
|
|
if ($featureId -isnot [string] -or [string]::IsNullOrWhiteSpace([string]$featureId)) {
|
|
|
|
|
$hasInvalidSelectedFeatureId = $true
|
|
|
|
|
continue
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
$normalizedFeatureId = [string]$featureId
|
|
|
|
|
if ($selectedFeatureIds.Add($normalizedFeatureId)) {
|
|
|
|
|
$selectedFeatures.Add($normalizedFeatureId)
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if ($hasInvalidSelectedFeatureId) {
|
2026-09-10 21:18:43 +05:30
|
|
|
$errors.Add((Get-Translation -Key 'BackupSelectedFeaturesMustBeStrings'))
|
2026-05-08 21:19:52 +02:00
|
|
|
}
|
|
|
|
|
|
2026-06-10 17:40:31 +02:00
|
|
|
return [PSCustomObject]@{
|
|
|
|
|
SelectedFeatures = $selectedFeatures.ToArray()
|
|
|
|
|
Errors = $errors.ToArray()
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2026-09-10 21:18:43 +05:30
|
|
|
<#
|
|
|
|
|
.SYNOPSIS
|
|
|
|
|
Extracts and deduplicates the SelectedUndoFeatures list from a backup, validating each entry.
|
|
|
|
|
|
|
|
|
|
.DESCRIPTION
|
|
|
|
|
Unlike SelectedFeatures, this property is optional: a missing property returns an
|
|
|
|
|
empty array with no error, since not every backup has undone features to restore.
|
|
|
|
|
#>
|
2026-06-10 17:40:31 +02:00
|
|
|
function Get-NormalizedSelectedUndoFeatureIdsFromBackup {
|
|
|
|
|
param(
|
|
|
|
|
[Parameter(Mandatory)]
|
|
|
|
|
$Backup
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
$selectedUndoFeatures = New-Object System.Collections.Generic.List[string]
|
|
|
|
|
$selectedUndoFeatureIds = New-Object 'System.Collections.Generic.HashSet[string]' ([System.StringComparer]::OrdinalIgnoreCase)
|
|
|
|
|
$errors = New-Object System.Collections.Generic.List[string]
|
|
|
|
|
|
|
|
|
|
# SelectedUndoFeatures is optional - only process if present
|
|
|
|
|
if (-not $Backup.PSObject.Properties['SelectedUndoFeatures']) {
|
|
|
|
|
return [PSCustomObject]@{
|
|
|
|
|
SelectedUndoFeatures = $selectedUndoFeatures.ToArray()
|
|
|
|
|
Errors = $errors.ToArray()
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
$hasInvalidSelectedUndoFeatureId = $false
|
|
|
|
|
foreach ($featureId in @($Backup.SelectedUndoFeatures)) {
|
|
|
|
|
if ($featureId -isnot [string] -or [string]::IsNullOrWhiteSpace([string]$featureId)) {
|
|
|
|
|
$hasInvalidSelectedUndoFeatureId = $true
|
|
|
|
|
continue
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
$normalizedFeatureId = [string]$featureId
|
|
|
|
|
if ($selectedUndoFeatureIds.Add($normalizedFeatureId)) {
|
|
|
|
|
$selectedUndoFeatures.Add($normalizedFeatureId)
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if ($hasInvalidSelectedUndoFeatureId) {
|
2026-09-10 21:18:43 +05:30
|
|
|
$errors.Add((Get-Translation -Key 'BackupSelectedUndoFeaturesMustBeStrings'))
|
2026-05-08 21:19:52 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return [PSCustomObject]@{
|
2026-06-10 17:40:31 +02:00
|
|
|
SelectedUndoFeatures = $selectedUndoFeatures.ToArray()
|
2026-05-08 21:19:52 +02:00
|
|
|
Errors = $errors.ToArray()
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2026-09-10 21:18:43 +05:30
|
|
|
<#
|
|
|
|
|
.SYNOPSIS
|
|
|
|
|
Normalizes a raw registry key snapshot into a consistent shape, recursing into sub-keys.
|
|
|
|
|
|
|
|
|
|
.DESCRIPTION
|
2026-10-21 22:47:01 +02:00
|
|
|
Fills in defaults for optional properties (Values, SubKeys) so downstream
|
2026-09-10 21:18:43 +05:30
|
|
|
validation can read a predictable shape regardless of what the backup file omitted.
|
2026-10-21 22:47:01 +02:00
|
|
|
Requires Path and a Boolean Exists to avoid interpreting missing data as deletion.
|
2026-09-10 21:18:43 +05:30
|
|
|
#>
|
2026-05-08 21:19:52 +02:00
|
|
|
function Normalize-RegistryKeySnapshot {
|
|
|
|
|
param(
|
|
|
|
|
[Parameter(Mandatory)]
|
|
|
|
|
$Snapshot
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
if (-not $Snapshot.PSObject.Properties['Path'] -or [string]::IsNullOrWhiteSpace([string]$Snapshot.Path)) {
|
2026-09-10 21:18:43 +05:30
|
|
|
throw (Get-Translation -Key 'BackupSnapshotMissingPath')
|
2026-05-08 21:19:52 +02:00
|
|
|
}
|
|
|
|
|
|
2026-10-21 22:47:01 +02:00
|
|
|
if (-not $Snapshot.PSObject.Properties['Exists'] -or $Snapshot.Exists -isnot [bool]) {
|
|
|
|
|
throw (Get-Translation -Key 'BackupSnapshotInvalidExists' -FormatArgs @($Snapshot.Path))
|
2026-05-08 21:19:52 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
$values = @()
|
|
|
|
|
if ($Snapshot.PSObject.Properties['Values']) {
|
|
|
|
|
foreach ($valueSnapshot in @($Snapshot.Values)) {
|
|
|
|
|
$valueExists = $true
|
|
|
|
|
if ($valueSnapshot.PSObject.Properties['Exists']) {
|
|
|
|
|
$valueExists = [bool]$valueSnapshot.Exists
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
$values += [PSCustomObject]@{
|
|
|
|
|
Name = [string]$valueSnapshot.Name
|
|
|
|
|
Exists = $valueExists
|
|
|
|
|
Kind = if ($valueSnapshot.PSObject.Properties['Kind']) { [string]$valueSnapshot.Kind } else { $null }
|
|
|
|
|
Data = if ($valueSnapshot.PSObject.Properties['Data']) { $valueSnapshot.Data } else { $null }
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
$subKeys = @()
|
|
|
|
|
if ($Snapshot.PSObject.Properties['SubKeys']) {
|
|
|
|
|
foreach ($subKeySnapshot in @($Snapshot.SubKeys)) {
|
|
|
|
|
$subKeys += @(Normalize-RegistryKeySnapshot -Snapshot $subKeySnapshot)
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return [PSCustomObject]@{
|
|
|
|
|
Path = [string]$Snapshot.Path
|
2026-10-21 22:47:01 +02:00
|
|
|
Exists = $Snapshot.Exists
|
|
|
|
|
OriginallyAbsent = ($Snapshot.OriginallyAbsent -is [bool] -and $Snapshot.OriginallyAbsent)
|
2026-05-08 21:19:52 +02:00
|
|
|
Values = @($values)
|
|
|
|
|
SubKeys = @($subKeys)
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2026-09-10 21:18:43 +05:30
|
|
|
<#
|
|
|
|
|
.SYNOPSIS
|
|
|
|
|
Validates that a backup's registry snapshots only contain paths and values the
|
|
|
|
|
selected (and undo) features are actually allowed to touch.
|
|
|
|
|
|
|
|
|
|
.DESCRIPTION
|
|
|
|
|
Builds an allow-list from the capture plans of the selected features, then checks
|
|
|
|
|
every snapshot against it. Returns an array of translated error strings; an empty
|
|
|
|
|
array means the backup is valid.
|
2026-10-21 22:47:01 +02:00
|
|
|
|
2026-09-10 21:18:43 +05:30
|
|
|
#>
|
2026-05-08 21:19:52 +02:00
|
|
|
function Test-RegistryBackupMatchesSelectedFeatures {
|
|
|
|
|
param(
|
|
|
|
|
[Parameter(Mandatory)]
|
|
|
|
|
[AllowEmptyCollection()]
|
|
|
|
|
[string[]]$SelectedFeatureIds,
|
|
|
|
|
[Parameter(Mandatory)]
|
2026-06-10 17:40:31 +02:00
|
|
|
[AllowEmptyCollection()]
|
|
|
|
|
[string[]]$SelectedUndoFeatureIds,
|
|
|
|
|
[Parameter(Mandatory)]
|
2026-05-09 21:56:58 +02:00
|
|
|
[string]$Target,
|
|
|
|
|
[Parameter(Mandatory)]
|
2026-05-08 21:19:52 +02:00
|
|
|
[AllowEmptyCollection()]
|
|
|
|
|
[object[]]$RegistryKeys
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
$errors = New-Object System.Collections.Generic.List[string]
|
|
|
|
|
|
2026-10-21 22:47:01 +02:00
|
|
|
$planMap = Get-RegistryBackupValidationPlanMap -SelectedFeatureIds $SelectedFeatureIds -SelectedUndoFeatureIds $SelectedUndoFeatureIds -Target $Target -Errors $errors
|
|
|
|
|
|
|
|
|
|
if ($errors.Count -gt 0) {
|
2026-05-08 21:19:52 +02:00
|
|
|
return $errors.ToArray()
|
|
|
|
|
}
|
|
|
|
|
|
2026-10-21 22:47:01 +02:00
|
|
|
return Test-RegistryBackupAgainstAllowList -RegistryKeys $RegistryKeys -PlanMap $planMap
|
|
|
|
|
}
|
2026-05-08 21:19:52 +02:00
|
|
|
|
2026-10-21 22:47:01 +02:00
|
|
|
<#
|
|
|
|
|
.SYNOPSIS
|
|
|
|
|
Validates registry snapshots against an already derived trusted allow-list.
|
2026-05-08 21:19:52 +02:00
|
|
|
|
2026-10-21 22:47:01 +02:00
|
|
|
.DESCRIPTION
|
|
|
|
|
Internal helper shared by feature-based validation and the import workflow.
|
|
|
|
|
The plan map must come from Get-RegistryBackupValidationPlanMap, never backup data.
|
|
|
|
|
When ExecutableValueWarnings is supplied, executable content mismatches are collected
|
|
|
|
|
for confirmation instead of blocking import. All other validation errors remain blocking.
|
|
|
|
|
#>
|
|
|
|
|
function Test-RegistryBackupAgainstAllowList {
|
|
|
|
|
param(
|
|
|
|
|
[Parameter(Mandatory)]
|
|
|
|
|
[AllowEmptyCollection()]
|
|
|
|
|
[object[]]$RegistryKeys,
|
|
|
|
|
[Parameter(Mandatory)]
|
|
|
|
|
[hashtable]$PlanMap,
|
|
|
|
|
[AllowEmptyCollection()]
|
|
|
|
|
[System.Collections.Generic.List[object]]$ExecutableValueWarnings
|
|
|
|
|
)
|
2026-05-08 21:19:52 +02:00
|
|
|
|
2026-10-21 22:47:01 +02:00
|
|
|
$errors = New-Object System.Collections.Generic.List[string]
|
|
|
|
|
|
|
|
|
|
if ($PlanMap.Count -eq 0 -and @($RegistryKeys).Count -gt 0) {
|
2026-09-10 21:18:43 +05:30
|
|
|
$errors.Add((Get-Translation -Key 'BackupNoAllowedRegistryPaths'))
|
2026-05-08 21:19:52 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
foreach ($rootSnapshot in @($RegistryKeys)) {
|
2026-10-21 22:47:01 +02:00
|
|
|
Test-RegistrySnapshotAgainstAllowList -Snapshot $rootSnapshot -PlanMap $PlanMap -Errors $errors -ExecutableValueWarnings $ExecutableValueWarnings
|
2026-05-08 21:19:52 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return $errors.ToArray()
|
|
|
|
|
}
|
|
|
|
|
|
2026-10-21 22:47:01 +02:00
|
|
|
<#
|
|
|
|
|
.SYNOPSIS
|
|
|
|
|
Derives trusted registry permissions once for backup conversion and validation.
|
|
|
|
|
#>
|
|
|
|
|
function Get-RegistryBackupValidationPlanMap {
|
|
|
|
|
param(
|
|
|
|
|
[Parameter(Mandatory)]
|
|
|
|
|
[AllowEmptyCollection()]
|
|
|
|
|
[string[]]$SelectedFeatureIds,
|
|
|
|
|
[Parameter(Mandatory)]
|
|
|
|
|
[AllowEmptyCollection()]
|
|
|
|
|
[string[]]$SelectedUndoFeatureIds,
|
|
|
|
|
[Parameter(Mandatory)]
|
|
|
|
|
[string]$Target,
|
|
|
|
|
[Parameter(Mandatory)]
|
|
|
|
|
[AllowEmptyCollection()]
|
|
|
|
|
[System.Collections.Generic.List[string]]$Errors
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
if (-not $script:Features -or $script:Features.Count -eq 0) {
|
|
|
|
|
$Errors.Add((Get-Translation -Key 'BackupFeatureCatalogNotLoaded'))
|
|
|
|
|
return @{}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
$selectedRegistryFeatures = @(Get-SelectedRegistryFeaturesForBackupValidation -SelectedFeatureIds @($SelectedFeatureIds) -IsUndoFeature:$false -Errors $Errors)
|
|
|
|
|
$undoRegistryFeatures = @(Get-SelectedRegistryFeaturesForBackupValidation -SelectedFeatureIds @($SelectedUndoFeatureIds) -IsUndoFeature:$true -Errors $Errors)
|
|
|
|
|
$useSysprepRegFiles = ($Target -eq 'DefaultUserProfile') -or ($Target -like 'User:*')
|
|
|
|
|
|
|
|
|
|
$capturePlans = @()
|
|
|
|
|
if ($Errors.Count -eq 0 -and ($selectedRegistryFeatures.Count -gt 0 -or $undoRegistryFeatures.Count -gt 0)) {
|
|
|
|
|
$capturePlans = @(Get-RegistryBackupCapturePlans -SelectedRegistryFeatures @($selectedRegistryFeatures) -UndoRegistryFeatures @($undoRegistryFeatures) -UseSysprepRegFiles:$useSysprepRegFiles)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
$planMap = New-RegistryBackupAllowListPlanMap -CapturePlans @($capturePlans)
|
|
|
|
|
if ($planMap.Count -gt 0) {
|
|
|
|
|
# Catalog data supplies trusted content only; selected plans still authorize locations and operations.
|
|
|
|
|
$trustedValueMap = Get-RegistryBackupTrustedValueMap -Features @($script:Features.Values) -UseSysprepRegFiles:$useSysprepRegFiles
|
|
|
|
|
foreach ($plan in $planMap.Values) {
|
|
|
|
|
$plan | Add-Member -MemberType NoteProperty -Name TrustedValueMap -Value $trustedValueMap
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
return $planMap
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
<#
|
|
|
|
|
.SYNOPSIS
|
|
|
|
|
Reads trusted executable value data from both directions of catalog features.
|
|
|
|
|
|
|
|
|
|
.DESCRIPTION
|
|
|
|
|
This supplies data comparisons only; the selected operation's capture plan still
|
|
|
|
|
controls which paths and value names may be restored. Backup fields never supply trust.
|
|
|
|
|
#>
|
|
|
|
|
function Get-RegistryBackupTrustedValueMap {
|
|
|
|
|
param(
|
|
|
|
|
[object[]]$Features = @(),
|
|
|
|
|
[switch]$UseSysprepRegFiles
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
$trustedValueMap = @{}
|
|
|
|
|
$filePaths = New-Object 'System.Collections.Generic.HashSet[string]' ([System.StringComparer]::OrdinalIgnoreCase)
|
|
|
|
|
foreach ($feature in $Features) {
|
|
|
|
|
if (-not [string]::IsNullOrWhiteSpace([string]$feature.RegistryKey)) {
|
|
|
|
|
$null = $filePaths.Add((Get-RegistryFilePathForFeature -RegistryKey $feature.RegistryKey -UseSysprepRegFiles:$UseSysprepRegFiles))
|
|
|
|
|
}
|
|
|
|
|
$undoPath = Resolve-RegistryBackupUndoFilePath -Feature $feature
|
|
|
|
|
if (-not [string]::IsNullOrWhiteSpace($undoPath)) { $null = $filePaths.Add($undoPath) }
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
foreach ($filePath in $filePaths) {
|
|
|
|
|
# A missing counterpart cannot authorize data; the active direction is checked by capture planning.
|
|
|
|
|
if (-not (Test-Path -LiteralPath $filePath)) { continue }
|
|
|
|
|
foreach ($operation in @(Get-RegFileOperations -regFilePath $filePath)) {
|
|
|
|
|
if ($operation.OperationType -ne 'SetValue' -or
|
|
|
|
|
-not (Test-RegistryValueMayExecuteCode -Path $operation.KeyPath -ValueName $operation.ValueName -KindName $operation.ValueType)) { continue }
|
|
|
|
|
$pathKey = Get-NormalizedRegistryPathKey -Path $operation.KeyPath
|
|
|
|
|
if (-not $trustedValueMap.ContainsKey($pathKey)) { $trustedValueMap[$pathKey] = @{} }
|
|
|
|
|
$name = [string]$operation.ValueName
|
|
|
|
|
$trustedValueMap[$pathKey][$name] = @($trustedValueMap[$pathKey][$name]) + @($operation)
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
return $trustedValueMap
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
<#
|
|
|
|
|
.SYNOPSIS
|
|
|
|
|
Identifies command lines, COM server registrations, and executable indirections.
|
|
|
|
|
#>
|
|
|
|
|
function Test-RegistryValueMayExecuteCode {
|
|
|
|
|
param([string]$Path, [AllowEmptyString()][string]$ValueName, [string]$KindName = 'String')
|
|
|
|
|
|
|
|
|
|
$parts = Split-RegistryPath -path $Path
|
|
|
|
|
if (-not $parts) { return $false }
|
|
|
|
|
return $parts.SubKey -match '(?i)(?:^|\\)(?:Run|RunOnce|RunOnceEx|InprocServer(?:32)?|InprocHandler(?:32)?|LocalServer(?:32)?|TreatAs|AutoTreatAs|AutoConvertTo|ScriptletURL)(?:\\|$)' -or
|
|
|
|
|
$parts.SubKey -match '(?i)\\shell\\[^\\]+\\(?:command|ddeexec)(?:\\|$)' -or
|
|
|
|
|
($ValueName -eq '' -and $parts.SubKey -match '(?i)\\shellex\\ContextMenuHandlers\\[^\\]+$') -or
|
|
|
|
|
# COM activation can use string indirections outside InprocServer32. Numeric settings are not server paths.
|
|
|
|
|
($KindName -notin @('DWord', 'QWord') -and $parts.SubKey -match '(?i)(?:^|\\)CLSID\\[^\\]+(?:\\|$)') -or
|
|
|
|
|
$ValueName -match '^(?i:DelegateExecute|ExplorerCommandHandler|ScriptletURL|DllSurrogate|DllSurrogateExecutable)$'
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
function Test-RegistryExecutableValueDataAllowed {
|
|
|
|
|
param($Snapshot, [string]$Path, [hashtable]$TrustedValueMap)
|
|
|
|
|
|
|
|
|
|
$kind = [System.Enum]::Parse([Microsoft.Win32.RegistryValueKind], [string]$Snapshot.Kind, $true)
|
|
|
|
|
if (-not (Test-RegistryValueMayExecuteCode -Path $Path -ValueName $Snapshot.Name -KindName $kind.ToString())) { return $true }
|
|
|
|
|
$stringKinds = @([Microsoft.Win32.RegistryValueKind]::String, [Microsoft.Win32.RegistryValueKind]::ExpandString)
|
|
|
|
|
if ($kind -notin ($stringKinds + @([Microsoft.Win32.RegistryValueKind]::DWord, [Microsoft.Win32.RegistryValueKind]::QWord))) { return $false }
|
|
|
|
|
# Empty executable registrations cannot supply a command or DLL path.
|
|
|
|
|
if ($kind -in $stringKinds -and ($null -eq $Snapshot.Data -or [string]$Snapshot.Data -ceq '')) { return $true }
|
|
|
|
|
$pathKey = Get-NormalizedRegistryPathKey -Path $Path
|
|
|
|
|
if ($null -eq $TrustedValueMap -or -not $TrustedValueMap.ContainsKey($pathKey)) { return $false }
|
|
|
|
|
foreach ($operation in @($TrustedValueMap[$pathKey][[string]$Snapshot.Name])) {
|
|
|
|
|
if ($null -eq $operation) { continue }
|
|
|
|
|
$trustedKind = if ($operation.ValueType -eq 'Hex2') { 'ExpandString' } else { $operation.ValueType }
|
|
|
|
|
if ($kind.ToString() -eq $trustedKind -and [string]$Snapshot.Data -ceq [string]$operation.ValueData) { return $true }
|
|
|
|
|
}
|
|
|
|
|
return $false
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
<#
|
|
|
|
|
.SYNOPSIS
|
|
|
|
|
Converts legacy absent value-only keys into scoped missing-value snapshots.
|
|
|
|
|
|
|
|
|
|
.DESCRIPTION
|
|
|
|
|
Uses only trusted feature plans. Empty absent-key snapshots at exact value-only
|
|
|
|
|
paths are converted in memory; authorized tree deletions and invalid snapshots
|
|
|
|
|
remain unchanged for subsequent validation. The input snapshots are not modified.
|
|
|
|
|
#>
|
|
|
|
|
function ConvertTo-CompatibleRegistryKeySnapshots {
|
|
|
|
|
param(
|
|
|
|
|
[Parameter(Mandatory)]
|
|
|
|
|
[AllowEmptyCollection()]
|
|
|
|
|
[object[]]$RegistryKeys,
|
|
|
|
|
[Parameter(Mandatory)]
|
|
|
|
|
[hashtable]$PlanMap
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
foreach ($snapshot in $RegistryKeys) {
|
|
|
|
|
$normalizedPath = Get-NormalizedRegistryPathKey -Path $snapshot.Path
|
|
|
|
|
$plan = if ($normalizedPath) { $PlanMap[$normalizedPath] } else { $null }
|
|
|
|
|
$convertAbsentKey = $snapshot.Exists -is [bool] -and -not $snapshot.Exists -and
|
|
|
|
|
@($snapshot.Values).Count -eq 0 -and @($snapshot.SubKeys).Count -eq 0 -and
|
|
|
|
|
$null -ne $plan -and -not $plan.CaptureAllValues -and -not $plan.IncludeSubKeys -and
|
|
|
|
|
$plan.ValueNames.Count -gt 0 -and
|
|
|
|
|
-not (Test-RegistryKeyDeletionAllowed -NormalizedPath $normalizedPath -PlanMap $PlanMap)
|
|
|
|
|
|
|
|
|
|
$originallyAbsent = $convertAbsentKey -or ($snapshot.OriginallyAbsent -is [bool] -and $snapshot.OriginallyAbsent)
|
|
|
|
|
# Absence is backup data; permission to clean up comes from an exact, nonrecursive plan.
|
|
|
|
|
# RemoveEmptyKey is derived here and is deliberately not read by normalization.
|
|
|
|
|
$removeEmptyKey = $originallyAbsent -and ($snapshot.Exists -or $convertAbsentKey) -and
|
|
|
|
|
$null -ne $plan -and -not $plan.CaptureAllValues -and -not $plan.IncludeSubKeys -and
|
|
|
|
|
@($snapshot.SubKeys).Count -eq 0 -and
|
|
|
|
|
@($snapshot.Values | Where-Object { [bool]$_.Exists }).Count -eq 0
|
|
|
|
|
|
|
|
|
|
[PSCustomObject]@{
|
|
|
|
|
Path = $snapshot.Path
|
|
|
|
|
Exists = if ($convertAbsentKey) { $true } else { $snapshot.Exists }
|
|
|
|
|
OriginallyAbsent = $originallyAbsent
|
|
|
|
|
RemoveEmptyKey = $removeEmptyKey
|
|
|
|
|
Values = @(
|
|
|
|
|
if ($convertAbsentKey) {
|
|
|
|
|
foreach ($valueName in @($plan.ValueNames | Sort-Object)) {
|
|
|
|
|
[PSCustomObject]@{ Name = $valueName; Exists = $false; Kind = $null; Data = $null }
|
|
|
|
|
}
|
|
|
|
|
} else { $snapshot.Values }
|
|
|
|
|
)
|
|
|
|
|
SubKeys = @(ConvertTo-CompatibleRegistryKeySnapshots -RegistryKeys @($snapshot.SubKeys) -PlanMap $PlanMap)
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2026-09-10 21:18:43 +05:30
|
|
|
<#
|
|
|
|
|
.SYNOPSIS
|
|
|
|
|
Resolves selected feature IDs to their catalog entries, keeping only the ones with a
|
|
|
|
|
registry-backed apply or undo key relevant to the requested direction.
|
|
|
|
|
|
|
|
|
|
.DESCRIPTION
|
|
|
|
|
Appends a translated error to the caller-supplied Errors list for any feature ID that
|
|
|
|
|
isn't in the current catalog, rather than failing the whole validation outright.
|
|
|
|
|
#>
|
2026-05-08 21:19:52 +02:00
|
|
|
function Get-SelectedRegistryFeaturesForBackupValidation {
|
|
|
|
|
param(
|
|
|
|
|
[Parameter(Mandatory)]
|
|
|
|
|
[AllowEmptyCollection()]
|
|
|
|
|
[string[]]$SelectedFeatureIds,
|
|
|
|
|
[Parameter(Mandatory)]
|
2026-06-10 17:40:31 +02:00
|
|
|
[bool]$IsUndoFeature,
|
|
|
|
|
[Parameter(Mandatory)]
|
2026-05-08 21:19:52 +02:00
|
|
|
[AllowEmptyCollection()]
|
|
|
|
|
$Errors
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
if ($null -eq $Errors -or -not ($Errors -is [System.Collections.IList])) {
|
|
|
|
|
throw 'Get-SelectedRegistryFeaturesForBackupValidation requires Errors to be a mutable list collection.'
|
|
|
|
|
}
|
2026-09-10 21:18:43 +05:30
|
|
|
# Intentionally not localized: this throw signals a programming error (a caller passing the
|
|
|
|
|
# wrong collection type), not a condition a user's backup file can trigger.
|
2026-05-08 21:19:52 +02:00
|
|
|
|
|
|
|
|
$selectedRegistryFeatures = New-Object System.Collections.Generic.List[object]
|
|
|
|
|
foreach ($featureId in @($SelectedFeatureIds)) {
|
|
|
|
|
if (-not $script:Features.ContainsKey($featureId)) {
|
2026-09-10 21:18:43 +05:30
|
|
|
$Errors.Add((Get-Translation -Key 'BackupFeatureNotInCatalog' -FormatArgs @($featureId)))
|
2026-05-08 21:19:52 +02:00
|
|
|
continue
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
$feature = $script:Features[$featureId]
|
2026-06-10 17:40:31 +02:00
|
|
|
if (-not $feature) {
|
|
|
|
|
continue
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
# For undo features, check RegistryUndoKey if present (real features)
|
|
|
|
|
# Otherwise check RegistryKey (for synthetic features from backup capture)
|
|
|
|
|
$registryKeyToUse = if ($IsUndoFeature) {
|
|
|
|
|
$key = [string]$feature.RegistryUndoKey
|
|
|
|
|
if (-not [string]::IsNullOrWhiteSpace($key)) {
|
|
|
|
|
$key
|
|
|
|
|
}
|
|
|
|
|
else {
|
|
|
|
|
[string]$feature.RegistryKey
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
else {
|
|
|
|
|
[string]$feature.RegistryKey
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (-not [string]::IsNullOrWhiteSpace($registryKeyToUse)) {
|
2026-05-08 21:19:52 +02:00
|
|
|
$selectedRegistryFeatures.Add($feature)
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return $selectedRegistryFeatures.ToArray()
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
function New-RegistryBackupAllowListPlanMap {
|
|
|
|
|
param(
|
|
|
|
|
[Parameter(Mandatory)]
|
|
|
|
|
[AllowEmptyCollection()]
|
|
|
|
|
[object[]]$CapturePlans
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
$planMap = @{}
|
|
|
|
|
foreach ($plan in @($CapturePlans)) {
|
|
|
|
|
$normalizedPath = Get-NormalizedRegistryPathKey -Path $plan.Path
|
|
|
|
|
if ([string]::IsNullOrWhiteSpace($normalizedPath)) {
|
|
|
|
|
continue
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
$planMap[$normalizedPath] = [PSCustomObject]@{
|
|
|
|
|
Path = $plan.Path
|
|
|
|
|
NormalizedPath = $normalizedPath
|
|
|
|
|
IncludeSubKeys = [bool]$plan.IncludeSubKeys
|
|
|
|
|
CaptureAllValues = [bool]$plan.CaptureAllValues
|
2026-10-21 22:47:01 +02:00
|
|
|
AllowDeleteKey = [bool]$plan.AllowDeleteKey
|
2026-05-08 21:19:52 +02:00
|
|
|
ValueNames = ConvertTo-RegistryValueNameSet -ValueNames @($plan.ValueNames)
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return $planMap
|
|
|
|
|
}
|
|
|
|
|
|
2026-07-19 22:06:07 +02:00
|
|
|
<#
|
|
|
|
|
.SYNOPSIS
|
|
|
|
|
Converts registry value names into a case-insensitive set.
|
|
|
|
|
|
|
|
|
|
.DESCRIPTION
|
|
|
|
|
Preserves empty names and prevents PowerShell from enumerating the returned
|
|
|
|
|
HashSet.
|
|
|
|
|
#>
|
2026-05-08 21:19:52 +02:00
|
|
|
function ConvertTo-RegistryValueNameSet {
|
|
|
|
|
param(
|
|
|
|
|
[AllowEmptyCollection()]
|
|
|
|
|
[string[]]$ValueNames
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
$valueNameSet = New-Object 'System.Collections.Generic.HashSet[string]' ([System.StringComparer]::OrdinalIgnoreCase)
|
|
|
|
|
foreach ($valueName in @($ValueNames)) {
|
|
|
|
|
$null = $valueNameSet.Add([string]$valueName)
|
|
|
|
|
}
|
|
|
|
|
|
2026-07-19 22:06:07 +02:00
|
|
|
# Prevent PowerShell from enumerating the HashSet into an array or single string
|
|
|
|
|
return ,$valueNameSet
|
2026-05-08 21:19:52 +02:00
|
|
|
}
|
|
|
|
|
|
2026-07-19 22:06:07 +02:00
|
|
|
<#
|
|
|
|
|
.SYNOPSIS
|
|
|
|
|
Validates a registry snapshot against the selected-feature allow list.
|
|
|
|
|
|
|
|
|
|
.DESCRIPTION
|
|
|
|
|
Recursively validates snapshot paths, value names, value kinds, and value
|
|
|
|
|
data, appending validation errors to the supplied list.
|
|
|
|
|
#>
|
2026-05-08 21:19:52 +02:00
|
|
|
function Test-RegistrySnapshotAgainstAllowList {
|
|
|
|
|
param(
|
|
|
|
|
[Parameter(Mandatory)]
|
|
|
|
|
$Snapshot,
|
|
|
|
|
[Parameter(Mandatory)]
|
|
|
|
|
[hashtable]$PlanMap,
|
|
|
|
|
[Parameter(Mandatory)]
|
|
|
|
|
[AllowEmptyCollection()]
|
2026-10-21 22:47:01 +02:00
|
|
|
[System.Collections.Generic.List[string]]$Errors,
|
|
|
|
|
[AllowEmptyCollection()]
|
|
|
|
|
[System.Collections.Generic.List[object]]$ExecutableValueWarnings
|
2026-05-08 21:19:52 +02:00
|
|
|
)
|
|
|
|
|
|
|
|
|
|
$snapshotPath = [string]$Snapshot.Path
|
|
|
|
|
$normalizedPath = Get-NormalizedRegistryPathKey -Path $snapshotPath
|
2026-10-21 22:47:01 +02:00
|
|
|
if ([string]::IsNullOrWhiteSpace($normalizedPath) -or $snapshotPath.IndexOf([char]0) -ge 0) {
|
2026-09-10 21:18:43 +05:30
|
|
|
$Errors.Add((Get-Translation -Key 'BackupUnsupportedRegistryPath' -FormatArgs @($snapshotPath)))
|
2026-05-08 21:19:52 +02:00
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
$planMatch = Find-RegistryAllowListPlanMatch -NormalizedPath $normalizedPath -PlanMap $PlanMap
|
|
|
|
|
if ($null -eq $planMatch) {
|
2026-09-10 21:18:43 +05:30
|
|
|
$Errors.Add((Get-Translation -Key 'BackupUnexpectedRegistryPath' -FormatArgs @($snapshotPath)))
|
2026-05-08 21:19:52 +02:00
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
|
2026-10-21 22:47:01 +02:00
|
|
|
if (-not $Snapshot.PSObject.Properties['Exists'] -or $Snapshot.Exists -isnot [bool]) {
|
|
|
|
|
$Errors.Add((Get-Translation -Key 'BackupSnapshotInvalidExists' -FormatArgs @($snapshotPath)))
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (-not $Snapshot.Exists -and -not (Test-RegistryKeyDeletionAllowed -NormalizedPath $normalizedPath -PlanMap $PlanMap)) {
|
|
|
|
|
$Errors.Add((Get-Translation -Key 'BackupUnexpectedKeyDeletion' -FormatArgs @($snapshotPath)))
|
|
|
|
|
}
|
|
|
|
|
|
2026-05-08 21:19:52 +02:00
|
|
|
foreach ($valueSnapshot in @($Snapshot.Values)) {
|
|
|
|
|
$valueName = Get-NormalizedRegistryValueName -ValueName $valueSnapshot.Name
|
|
|
|
|
$valueExists = [bool]$valueSnapshot.Exists
|
|
|
|
|
|
2026-10-21 22:47:01 +02:00
|
|
|
if ($valueName.IndexOf([char]0) -ge 0 -or -not (Test-RegistryValueAllowedByPlan -PlanMatch $planMatch -ValueName $valueName)) {
|
2026-09-10 21:18:43 +05:30
|
|
|
$Errors.Add((Get-Translation -Key 'BackupUnexpectedValue' -FormatArgs @($valueName, $snapshotPath)))
|
2026-05-08 21:19:52 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
$kindName = if ($valueSnapshot.PSObject.Properties['Kind']) { [string]$valueSnapshot.Kind } else { '' }
|
|
|
|
|
$valueReference = Get-RegistryValueReferenceForError -SnapshotPath $snapshotPath -ValueName $valueName
|
|
|
|
|
if ($valueExists) {
|
|
|
|
|
if (-not (Test-RegistryValueKindNameSupported -KindName $kindName)) {
|
2026-09-10 21:18:43 +05:30
|
|
|
$Errors.Add((Get-Translation -Key 'BackupUnsupportedValueKind' -FormatArgs @($kindName, $valueReference)))
|
2026-05-08 21:19:52 +02:00
|
|
|
}
|
2026-07-19 05:23:04 +05:30
|
|
|
elseif (-not (Test-RegistryValueDataMatchesKind -KindName $kindName -Data $valueSnapshot.Data)) {
|
2026-09-10 21:18:43 +05:30
|
|
|
$Errors.Add((Get-Translation -Key 'BackupInvalidValueData' -FormatArgs @($kindName, $valueReference)))
|
2026-07-19 05:23:04 +05:30
|
|
|
}
|
2026-10-21 22:47:01 +02:00
|
|
|
elseif (-not (Test-RegistryExecutableValueDataAllowed -Snapshot $valueSnapshot -Path $snapshotPath -TrustedValueMap $planMatch.TrustedValueMap)) {
|
|
|
|
|
if ($null -eq $ExecutableValueWarnings) {
|
|
|
|
|
$Errors.Add((Get-Translation -Key 'BackupUnsafeExecutableValue' -FormatArgs @($valueReference)))
|
|
|
|
|
}
|
|
|
|
|
else {
|
|
|
|
|
$ExecutableValueWarnings.Add([PSCustomObject]@{
|
|
|
|
|
Path = $snapshotPath
|
|
|
|
|
Name = $valueName
|
|
|
|
|
Kind = $kindName
|
|
|
|
|
Data = $valueSnapshot.Data
|
|
|
|
|
})
|
|
|
|
|
}
|
|
|
|
|
}
|
2026-05-08 21:19:52 +02:00
|
|
|
}
|
|
|
|
|
elseif (-not [string]::IsNullOrWhiteSpace($kindName)) {
|
2026-09-10 21:18:43 +05:30
|
|
|
$Errors.Add((Get-Translation -Key 'BackupValueMustNotDefineKind' -FormatArgs @($valueReference)))
|
2026-05-08 21:19:52 +02:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
foreach ($subKeySnapshot in @($Snapshot.SubKeys)) {
|
2026-10-21 22:47:01 +02:00
|
|
|
Test-RegistrySnapshotAgainstAllowList -Snapshot $subKeySnapshot -PlanMap $PlanMap -Errors $Errors -ExecutableValueWarnings $ExecutableValueWarnings
|
2026-05-08 21:19:52 +02:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2026-07-19 22:06:07 +02:00
|
|
|
<#
|
|
|
|
|
.SYNOPSIS
|
|
|
|
|
Tests whether backed-up registry data is valid for its declared value kind.
|
|
|
|
|
|
|
|
|
|
.DESCRIPTION
|
|
|
|
|
Rejects corrupted or hand-edited backup data that cannot be restored safely,
|
|
|
|
|
such as a DWord that overflows UInt32 or binary data containing an invalid byte.
|
|
|
|
|
This validation runs before Restore-RegistryKeySnapshot mutates the live
|
|
|
|
|
registry, preventing a failed conversion from leaving a partially restored key.
|
|
|
|
|
|
|
|
|
|
.PARAMETER KindName
|
|
|
|
|
The declared registry value kind name, such as DWord, QWord, or Binary.
|
|
|
|
|
|
|
|
|
|
.PARAMETER Data
|
|
|
|
|
The backed-up value data to validate against the declared kind.
|
|
|
|
|
|
|
|
|
|
.OUTPUTS
|
|
|
|
|
System.Boolean
|
|
|
|
|
#>
|
|
|
|
|
function Test-RegistryValueDataMatchesKind {
|
|
|
|
|
param(
|
|
|
|
|
[Parameter(Mandatory)]
|
|
|
|
|
[string]$KindName,
|
|
|
|
|
[AllowNull()]
|
|
|
|
|
$Data
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
$kind = [System.Enum]::Parse([Microsoft.Win32.RegistryValueKind], $KindName, $true)
|
|
|
|
|
switch ($kind) {
|
|
|
|
|
([Microsoft.Win32.RegistryValueKind]::DWord) {
|
|
|
|
|
$parsed = [uint32]0
|
|
|
|
|
return [uint32]::TryParse([string]$Data, [System.Globalization.NumberStyles]::Integer, [System.Globalization.CultureInfo]::InvariantCulture, [ref]$parsed)
|
|
|
|
|
}
|
|
|
|
|
([Microsoft.Win32.RegistryValueKind]::QWord) {
|
|
|
|
|
$parsed = [uint64]0
|
|
|
|
|
return [uint64]::TryParse([string]$Data, [System.Globalization.NumberStyles]::Integer, [System.Globalization.CultureInfo]::InvariantCulture, [ref]$parsed)
|
|
|
|
|
}
|
|
|
|
|
([Microsoft.Win32.RegistryValueKind]::Binary) {
|
|
|
|
|
if ($null -eq $Data -or $Data -isnot [array]) { return $false }
|
|
|
|
|
foreach ($item in @($Data)) {
|
|
|
|
|
if ($item -isnot [ValueType] -and $item -isnot [string]) { return $false }
|
|
|
|
|
$parsed = 0
|
|
|
|
|
if (-not [int]::TryParse([string]$item, [ref]$parsed) -or $parsed -lt 0 -or $parsed -gt 255) {
|
|
|
|
|
return $false
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
return $true
|
|
|
|
|
}
|
|
|
|
|
([Microsoft.Win32.RegistryValueKind]::MultiString) {
|
|
|
|
|
foreach ($item in @($Data)) {
|
|
|
|
|
if ($item -isnot [string]) { return $false }
|
|
|
|
|
}
|
|
|
|
|
return $true
|
|
|
|
|
}
|
|
|
|
|
default { return ($null -eq $Data -or $Data -is [string]) }
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2026-05-08 21:19:52 +02:00
|
|
|
function Test-RegistryValueAllowedByPlan {
|
|
|
|
|
param(
|
|
|
|
|
[Parameter(Mandatory)]
|
|
|
|
|
$PlanMatch,
|
|
|
|
|
[Parameter(Mandatory)]
|
|
|
|
|
[AllowNull()]
|
|
|
|
|
[AllowEmptyString()]
|
|
|
|
|
[string]$ValueName
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
$ValueName = Get-NormalizedRegistryValueName -ValueName $ValueName
|
|
|
|
|
|
|
|
|
|
if ($PlanMatch.CaptureAllValues -or $PlanMatch.IsDescendant) {
|
|
|
|
|
return $true
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return $PlanMatch.ValueNames.Contains($ValueName)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
function Get-RegistryValueReferenceForError {
|
|
|
|
|
param(
|
|
|
|
|
[Parameter(Mandatory)]
|
|
|
|
|
[string]$SnapshotPath,
|
|
|
|
|
[Parameter(Mandatory)]
|
|
|
|
|
[AllowNull()]
|
|
|
|
|
[AllowEmptyString()]
|
|
|
|
|
[string]$ValueName
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
$ValueName = Get-NormalizedRegistryValueName -ValueName $ValueName
|
|
|
|
|
|
|
|
|
|
if ([string]::IsNullOrWhiteSpace($ValueName)) {
|
|
|
|
|
return "$SnapshotPath\\(Default)"
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return "$SnapshotPath\\$ValueName"
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
function Get-NormalizedRegistryValueName {
|
|
|
|
|
param(
|
|
|
|
|
[AllowNull()]
|
|
|
|
|
[AllowEmptyString()]
|
|
|
|
|
[object]$ValueName
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
if ($null -eq $ValueName) {
|
|
|
|
|
return ''
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return [string]$ValueName
|
|
|
|
|
}
|
|
|
|
|
|
2026-10-21 22:47:01 +02:00
|
|
|
<#
|
|
|
|
|
.SYNOPSIS
|
|
|
|
|
Checks whether selected feature operations authorize deletion of a key tree.
|
|
|
|
|
|
|
|
|
|
.DESCRIPTION
|
|
|
|
|
Only DeleteKey operations grant this permission. Checks all plans so an exact
|
|
|
|
|
value-only plan cannot hide a recursive deletion allowed by an ancestor.
|
|
|
|
|
#>
|
|
|
|
|
function Test-RegistryKeyDeletionAllowed {
|
|
|
|
|
param(
|
|
|
|
|
[Parameter(Mandatory)]
|
|
|
|
|
[string]$NormalizedPath,
|
|
|
|
|
[Parameter(Mandatory)]
|
|
|
|
|
[hashtable]$PlanMap
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
foreach ($plan in $PlanMap.Values) {
|
|
|
|
|
if (-not $plan.AllowDeleteKey) { continue }
|
|
|
|
|
|
|
|
|
|
if ($NormalizedPath.Equals($plan.NormalizedPath, [System.StringComparison]::OrdinalIgnoreCase) -or
|
|
|
|
|
($plan.IncludeSubKeys -and $NormalizedPath.StartsWith("$($plan.NormalizedPath)\", [System.StringComparison]::OrdinalIgnoreCase))) {
|
|
|
|
|
return $true
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return $false
|
|
|
|
|
}
|
|
|
|
|
|
2026-05-08 21:19:52 +02:00
|
|
|
function Find-RegistryAllowListPlanMatch {
|
|
|
|
|
param(
|
|
|
|
|
[Parameter(Mandatory)]
|
|
|
|
|
[string]$NormalizedPath,
|
|
|
|
|
[Parameter(Mandatory)]
|
|
|
|
|
[hashtable]$PlanMap
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
if ($PlanMap.ContainsKey($NormalizedPath)) {
|
|
|
|
|
$plan = $PlanMap[$NormalizedPath]
|
|
|
|
|
return [PSCustomObject]@{
|
|
|
|
|
IsDescendant = $false
|
|
|
|
|
CaptureAllValues = [bool]$plan.CaptureAllValues
|
|
|
|
|
ValueNames = $plan.ValueNames
|
2026-10-21 22:47:01 +02:00
|
|
|
TrustedValueMap = $plan.TrustedValueMap
|
2026-05-08 21:19:52 +02:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
foreach ($plan in @($PlanMap.Values)) {
|
|
|
|
|
if (-not [bool]$plan.IncludeSubKeys) {
|
|
|
|
|
continue
|
|
|
|
|
}
|
|
|
|
|
|
2026-06-22 01:37:29 +07:00
|
|
|
$subKeyPrefix = "$($plan.NormalizedPath)\"
|
2026-05-08 21:19:52 +02:00
|
|
|
if ($NormalizedPath.StartsWith($subKeyPrefix, [System.StringComparison]::OrdinalIgnoreCase)) {
|
|
|
|
|
return [PSCustomObject]@{
|
|
|
|
|
IsDescendant = $true
|
|
|
|
|
CaptureAllValues = $true
|
|
|
|
|
ValueNames = $plan.ValueNames
|
2026-10-21 22:47:01 +02:00
|
|
|
TrustedValueMap = $plan.TrustedValueMap
|
2026-05-08 21:19:52 +02:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return $null
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
function Get-NormalizedRegistryPathKey {
|
|
|
|
|
param(
|
|
|
|
|
[Parameter(Mandatory)]
|
|
|
|
|
[string]$Path
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
$parts = Split-RegistryPath -path $Path
|
|
|
|
|
if (-not $parts) {
|
|
|
|
|
return $null
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
$hiveName = [string]$parts.Hive
|
|
|
|
|
if ([string]::IsNullOrWhiteSpace($hiveName)) {
|
|
|
|
|
return $null
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
$normalizedHive = $hiveName.ToUpperInvariant()
|
|
|
|
|
$subKey = [string]$parts.SubKey
|
|
|
|
|
if ([string]::IsNullOrWhiteSpace($subKey)) {
|
|
|
|
|
return $normalizedHive
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
$normalizedSubKey = ($subKey -replace '/', '\\').Trim('\')
|
|
|
|
|
if ([string]::IsNullOrWhiteSpace($normalizedSubKey)) {
|
|
|
|
|
return $normalizedHive
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return "$normalizedHive\\$normalizedSubKey"
|
|
|
|
|
}
|
|
|
|
|
|
2026-07-19 22:06:07 +02:00
|
|
|
<#
|
|
|
|
|
.SYNOPSIS
|
|
|
|
|
Tests whether a registry value-kind name is supported in backups.
|
|
|
|
|
|
|
|
|
|
.DESCRIPTION
|
|
|
|
|
Parses kind names case-insensitively and rejects empty, invalid, Unknown,
|
|
|
|
|
and None values.
|
|
|
|
|
#>
|
2026-05-08 21:19:52 +02:00
|
|
|
function Test-RegistryValueKindNameSupported {
|
|
|
|
|
param(
|
|
|
|
|
[string]$KindName
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
if ([string]::IsNullOrWhiteSpace($KindName)) {
|
|
|
|
|
return $false
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
try {
|
|
|
|
|
$kind = [System.Enum]::Parse([Microsoft.Win32.RegistryValueKind], $KindName, $true)
|
2026-10-21 22:47:01 +02:00
|
|
|
return [System.Enum]::IsDefined([Microsoft.Win32.RegistryValueKind], $kind) -and
|
|
|
|
|
$kind -notin @([Microsoft.Win32.RegistryValueKind]::Unknown, [Microsoft.Win32.RegistryValueKind]::None)
|
2026-05-08 21:19:52 +02:00
|
|
|
}
|
|
|
|
|
catch {
|
|
|
|
|
return $false
|
|
|
|
|
}
|
|
|
|
|
}
|
2026-07-19 05:23:04 +05:30
|
|
|
|