mirror of
https://github.com/Raphire/Win11Debloat.git
synced 2026-05-18 11:46:18 +00:00
Fix registry backup validation for sysprep keys
This commit is contained in:
@@ -1,12 +1,13 @@
|
|||||||
function Get-RegistryBackupCapturePlans {
|
function Get-RegistryBackupCapturePlans {
|
||||||
param(
|
param(
|
||||||
[Parameter(Mandatory)]
|
[Parameter(Mandatory)]
|
||||||
[object[]]$SelectedRegistryFeatures
|
[object[]]$SelectedRegistryFeatures,
|
||||||
|
[switch]$UseSysprepRegFiles
|
||||||
)
|
)
|
||||||
|
|
||||||
$planMap = @{}
|
$planMap = @{}
|
||||||
foreach ($feature in $SelectedRegistryFeatures) {
|
foreach ($feature in $SelectedRegistryFeatures) {
|
||||||
$regFilePath = Get-RegistryFilePathForFeature -Feature $feature
|
$regFilePath = Get-RegistryFilePathForFeature -Feature $feature -UseSysprepRegFiles:$UseSysprepRegFiles
|
||||||
if (-not (Test-Path $regFilePath)) {
|
if (-not (Test-Path $regFilePath)) {
|
||||||
throw "Unable to find registry file for backup: $($feature.RegistryKey) ($regFilePath)"
|
throw "Unable to find registry file for backup: $($feature.RegistryKey) ($regFilePath)"
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -96,6 +96,8 @@ function Test-RegistryBackupMatchesSelectedFeatures {
|
|||||||
[AllowEmptyCollection()]
|
[AllowEmptyCollection()]
|
||||||
[string[]]$SelectedFeatureIds,
|
[string[]]$SelectedFeatureIds,
|
||||||
[Parameter(Mandatory)]
|
[Parameter(Mandatory)]
|
||||||
|
[string]$Target,
|
||||||
|
[Parameter(Mandatory)]
|
||||||
[AllowEmptyCollection()]
|
[AllowEmptyCollection()]
|
||||||
[object[]]$RegistryKeys
|
[object[]]$RegistryKeys
|
||||||
)
|
)
|
||||||
@@ -108,10 +110,11 @@ function Test-RegistryBackupMatchesSelectedFeatures {
|
|||||||
}
|
}
|
||||||
|
|
||||||
$selectedRegistryFeatures = @(Get-SelectedRegistryFeaturesForBackupValidation -SelectedFeatureIds @($SelectedFeatureIds) -Errors $errors)
|
$selectedRegistryFeatures = @(Get-SelectedRegistryFeaturesForBackupValidation -SelectedFeatureIds @($SelectedFeatureIds) -Errors $errors)
|
||||||
|
$useSysprepRegFiles = ($Target -eq 'DefaultUserProfile') -or ($Target -like 'User:*')
|
||||||
|
|
||||||
$capturePlans = @()
|
$capturePlans = @()
|
||||||
if ($errors.Count -eq 0 -and $selectedRegistryFeatures.Count -gt 0) {
|
if ($errors.Count -eq 0 -and $selectedRegistryFeatures.Count -gt 0) {
|
||||||
$capturePlans = @(Get-RegistryBackupCapturePlans -SelectedRegistryFeatures @($selectedRegistryFeatures))
|
$capturePlans = @(Get-RegistryBackupCapturePlans -SelectedRegistryFeatures @($selectedRegistryFeatures) -UseSysprepRegFiles:$useSysprepRegFiles)
|
||||||
}
|
}
|
||||||
|
|
||||||
$planMap = New-RegistryBackupAllowListPlanMap -CapturePlans @($capturePlans)
|
$planMap = New-RegistryBackupAllowListPlanMap -CapturePlans @($capturePlans)
|
||||||
|
|||||||
@@ -87,7 +87,7 @@ function Normalize-RegistryBackup {
|
|||||||
$errors.Add([string]$selectedFeatureParseError)
|
$errors.Add([string]$selectedFeatureParseError)
|
||||||
}
|
}
|
||||||
|
|
||||||
$allowListValidationErrors = @(Test-RegistryBackupMatchesSelectedFeatures -SelectedFeatureIds @($selectedFeatures) -RegistryKeys @($normalizedKeys))
|
$allowListValidationErrors = @(Test-RegistryBackupMatchesSelectedFeatures -SelectedFeatureIds @($selectedFeatures) -Target $normalizedTarget -RegistryKeys @($normalizedKeys))
|
||||||
foreach ($allowListValidationError in $allowListValidationErrors) {
|
foreach ($allowListValidationError in $allowListValidationErrors) {
|
||||||
$errors.Add([string]$allowListValidationError)
|
$errors.Add([string]$allowListValidationError)
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -4,13 +4,47 @@ function Split-RegistryPath {
|
|||||||
[string]$path
|
[string]$path
|
||||||
)
|
)
|
||||||
|
|
||||||
if ($path -notmatch '^(?<hive>HKEY_[^\\]+)(?:\\(?<subKey>.*))?$') {
|
$normalizedPath = [string]$path
|
||||||
|
if ([string]::IsNullOrWhiteSpace($normalizedPath)) {
|
||||||
return $null
|
return $null
|
||||||
}
|
}
|
||||||
|
|
||||||
|
$normalizedPath = $normalizedPath.Trim().Replace('/', '\')
|
||||||
|
if ([string]::IsNullOrWhiteSpace($normalizedPath)) {
|
||||||
|
return $null
|
||||||
|
}
|
||||||
|
|
||||||
|
if ($normalizedPath -notmatch '^(?<hive>HKEY_[^\\]+)(?:\\(?<subKey>.*))?$') {
|
||||||
|
return $null
|
||||||
|
}
|
||||||
|
|
||||||
|
$hiveName = [string]$matches.hive
|
||||||
|
|
||||||
|
$normalizedSubKey = if ($null -ne $matches.subKey) {
|
||||||
|
([string]$matches.subKey).Trim('\\')
|
||||||
|
}
|
||||||
|
else {
|
||||||
|
$null
|
||||||
|
}
|
||||||
|
|
||||||
|
if ($hiveName.Equals('HKEY_USERS', [System.StringComparison]::OrdinalIgnoreCase) -and -not [string]::IsNullOrWhiteSpace($normalizedSubKey)) {
|
||||||
|
if ($normalizedSubKey -match '^(?<mount>[^\\]+)(?:\\(?<rest>.*))?$') {
|
||||||
|
$mountName = [string]$matches.mount
|
||||||
|
if ($mountName.Equals('.DEFAULT', [System.StringComparison]::OrdinalIgnoreCase)) {
|
||||||
|
$remainingSubKey = if ($matches.rest) { [string]$matches.rest } else { '' }
|
||||||
|
if ([string]::IsNullOrWhiteSpace($remainingSubKey)) {
|
||||||
|
$normalizedSubKey = 'Default'
|
||||||
|
}
|
||||||
|
else {
|
||||||
|
$normalizedSubKey = "Default\$remainingSubKey"
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
return [PSCustomObject]@{
|
return [PSCustomObject]@{
|
||||||
Hive = $matches.hive
|
Hive = $hiveName
|
||||||
SubKey = $matches.subKey
|
SubKey = $normalizedSubKey
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -33,10 +67,12 @@ function Get-RegistryRootKey {
|
|||||||
function Get-RegistryFilePathForFeature {
|
function Get-RegistryFilePathForFeature {
|
||||||
param(
|
param(
|
||||||
[Parameter(Mandatory)]
|
[Parameter(Mandatory)]
|
||||||
$Feature
|
$Feature,
|
||||||
|
[switch]$UseSysprepRegFiles
|
||||||
)
|
)
|
||||||
|
|
||||||
if ($script:Params.ContainsKey('Sysprep') -or $script:Params.ContainsKey('User')) {
|
$useSysprepLayout = $UseSysprepRegFiles -or $script:Params.ContainsKey('Sysprep') -or $script:Params.ContainsKey('User')
|
||||||
|
if ($useSysprepLayout) {
|
||||||
return Join-Path (Join-Path $script:RegfilesPath 'Sysprep') $Feature.RegistryKey
|
return Join-Path (Join-Path $script:RegfilesPath 'Sysprep') $Feature.RegistryKey
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user