mirror of
https://github.com/Raphire/Win11Debloat.git
synced 2026-05-22 21:56:17 +00:00
Refactor registry key handling to improve key opening logic and streamline deletion operations
This commit is contained in:
@@ -64,13 +64,6 @@ function Remove-RegistrySubKeyTreeIfExists {
|
|||||||
[string]$SubKeyPath
|
[string]$SubKeyPath
|
||||||
)
|
)
|
||||||
|
|
||||||
$existingKey = $RootKey.OpenSubKey($SubKeyPath, $true)
|
|
||||||
if ($null -eq $existingKey) {
|
|
||||||
return
|
|
||||||
}
|
|
||||||
|
|
||||||
$existingKey.Close()
|
|
||||||
|
|
||||||
try {
|
try {
|
||||||
$RootKey.DeleteSubKeyTree($SubKeyPath, $false)
|
$RootKey.DeleteSubKeyTree($SubKeyPath, $false)
|
||||||
}
|
}
|
||||||
@@ -86,7 +79,8 @@ function Get-RegistryKeyForOperation {
|
|||||||
param(
|
param(
|
||||||
[Parameter(Mandatory)]
|
[Parameter(Mandatory)]
|
||||||
[string]$RegistryPath,
|
[string]$RegistryPath,
|
||||||
[switch]$CreateIfMissing
|
[switch]$CreateIfMissing,
|
||||||
|
[bool]$OpenKey = $true
|
||||||
)
|
)
|
||||||
|
|
||||||
$parts = Split-RegistryPath -path $RegistryPath
|
$parts = Split-RegistryPath -path $RegistryPath
|
||||||
@@ -104,6 +98,10 @@ function Get-RegistryKeyForOperation {
|
|||||||
return [PSCustomObject]@{ RootKey = $rootKey; SubKeyPath = $null; Key = $rootKey }
|
return [PSCustomObject]@{ RootKey = $rootKey; SubKeyPath = $null; Key = $rootKey }
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if (-not $OpenKey) {
|
||||||
|
return [PSCustomObject]@{ RootKey = $rootKey; SubKeyPath = $subKeyPath; Key = $null }
|
||||||
|
}
|
||||||
|
|
||||||
$key = if ($CreateIfMissing) {
|
$key = if ($CreateIfMissing) {
|
||||||
$rootKey.CreateSubKey($subKeyPath)
|
$rootKey.CreateSubKey($subKeyPath)
|
||||||
}
|
}
|
||||||
@@ -124,19 +122,12 @@ function Invoke-RegistryOperationsFromRegFile {
|
|||||||
|
|
||||||
foreach ($operation in @(Get-RegFileOperations -regFilePath $RegFilePath)) {
|
foreach ($operation in @(Get-RegFileOperations -regFilePath $RegFilePath)) {
|
||||||
try {
|
try {
|
||||||
$keyInfo = Get-RegistryKeyForOperation -RegistryPath $operation.KeyPath -CreateIfMissing:($operation.OperationType -eq 'SetValue')
|
$keyInfo = Get-RegistryKeyForOperation -RegistryPath $operation.KeyPath -CreateIfMissing:($operation.OperationType -eq 'SetValue') -OpenKey:($operation.OperationType -ne 'DeleteKey')
|
||||||
|
|
||||||
switch ($operation.OperationType) {
|
switch ($operation.OperationType) {
|
||||||
'DeleteKey' {
|
'DeleteKey' {
|
||||||
if ($null -ne $keyInfo.Key) {
|
if ($null -ne $keyInfo.SubKeyPath) {
|
||||||
try {
|
Remove-RegistrySubKeyTreeIfExists -RootKey $keyInfo.RootKey -SubKeyPath $keyInfo.SubKeyPath
|
||||||
if ($null -ne $keyInfo.SubKeyPath) {
|
|
||||||
Remove-RegistrySubKeyTreeIfExists -RootKey $keyInfo.RootKey -SubKeyPath $keyInfo.SubKeyPath
|
|
||||||
}
|
|
||||||
}
|
|
||||||
finally {
|
|
||||||
$keyInfo.Key.Close()
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
'DeleteValue' {
|
'DeleteValue' {
|
||||||
|
|||||||
Reference in New Issue
Block a user