Improve backup validation (#849)

This commit is contained in:
Jeffrey
2026-10-21 22:47:01 +02:00
committed by GitHub
parent 97f3f805ef
commit 039ef80a8b
18 changed files with 1504 additions and 46 deletions
+43
View File
@@ -104,6 +104,49 @@ function Remove-RegistrySubKeyTreeIfExists {
}
}
<#
.SYNOPSIS
Calls Windows' empty-only registry deletion API.
#>
function Invoke-RegistryEmptyKeyDeletion {
param($RootKey, [string]$SubKeyPath)
if (-not ('Win11Debloat.RegistryEmptyKey' -as [type])) {
Add-Type -Namespace Win11Debloat -Name RegistryEmptyKey -MemberDefinition @'
[System.Runtime.InteropServices.DllImport("shlwapi.dll", CharSet = System.Runtime.InteropServices.CharSet.Unicode, ExactSpelling = true)]
public static extern int SHDeleteEmptyKeyW(Microsoft.Win32.SafeHandles.SafeRegistryHandle hkey, string subKey);
'@
}
return [Win11Debloat.RegistryEmptyKey]::SHDeleteEmptyKeyW($RootKey.Handle, $SubKeyPath)
}
<#
.SYNOPSIS
Removes an empty key, retaining keys containing values or subkeys.
#>
function Remove-RegistryEmptyKeyIfExists {
param(
[Parameter(Mandatory)]$RootKey,
[Parameter(Mandatory)][string]$SubKeyPath
)
$status = Invoke-RegistryEmptyKeyDeletion -RootKey $RootKey -SubKeyPath $SubKeyPath
# Success, file not found, and path not found all satisfy cleanup.
if ($status -in @(0, 2, 3)) { return }
# A concurrent writer may have populated the key after the restore's initial check.
$key = $RootKey.OpenSubKey($SubKeyPath, $false)
if ($null -eq $key) { return }
try {
if (@($key.GetValueNames()).Count -gt 0 -or @($key.GetSubKeyNames()).Count -gt 0) { return }
}
finally {
$key.Close()
}
throw (New-Object System.ComponentModel.Win32Exception -ArgumentList ([int]$status))
}
<#
.SYNOPSIS
Returns a feature's registry-file path, using the Sysprep layout when targeting another profile.