mirror of
https://github.com/Raphire/Win11Debloat.git
synced 2026-11-25 23:16:15 +01:00
fix: read boolean deployment values from a config file as booleans (#833)
This commit is contained in:
@@ -305,9 +305,11 @@ function Get-DeploymentCategoryDetailString {
|
||||
}
|
||||
|
||||
$options = @()
|
||||
if ($lookup.ContainsKey('CreateRestorePoint') -and [bool]$lookup['CreateRestorePoint']) { $options += Get-Translation -Key 'ImportExportOptionRestorePoint' }
|
||||
if (-not ($lookup.ContainsKey('SkipRegistryBackup') -and [bool]$lookup['SkipRegistryBackup'])) { $options += Get-Translation -Key 'ImportExportOptionRegistryBackup' }
|
||||
if ($lookup.ContainsKey('RestartExplorer') -and [bool]$lookup['RestartExplorer']) { $options += Get-Translation -Key 'ImportExportOptionRestartExplorer' }
|
||||
if ((ConvertTo-ConfigBoolean $lookup['CreateRestorePoint']) -eq $true) { $options += Get-Translation -Key 'ImportExportOptionRestorePoint' }
|
||||
# Registry backup is on unless it is skipped; a value that is not a boolean is ignored by the import, so it is not listed
|
||||
$skipRegistryBackup = ConvertTo-ConfigBoolean $lookup['SkipRegistryBackup']
|
||||
if ($skipRegistryBackup -eq $false -or -not $lookup.ContainsKey('SkipRegistryBackup')) { $options += Get-Translation -Key 'ImportExportOptionRegistryBackup' }
|
||||
if ((ConvertTo-ConfigBoolean $lookup['RestartExplorer']) -eq $true) { $options += Get-Translation -Key 'ImportExportOptionRestartExplorer' }
|
||||
|
||||
$lines = @()
|
||||
if ($line1.Count -gt 0) { $lines += $line1 -join ', ' }
|
||||
@@ -413,21 +415,23 @@ function Set-ImportedDeploymentSettings {
|
||||
$appRemovalScopeCombo.SelectedIndex = [int]$lookup['AppRemovalScopeIndex']
|
||||
}
|
||||
|
||||
# A value that is not a boolean (see ConvertTo-ConfigBoolean) leaves the checkbox as it is, after a warning
|
||||
$restorePointCheckBox = $Owner.FindName('RestorePointCheckBox')
|
||||
if ($lookup.ContainsKey('CreateRestorePoint') -and $restorePointCheckBox) {
|
||||
$restorePointCheckBox.IsChecked = [bool]$lookup['CreateRestorePoint']
|
||||
$createRestorePoint = Get-ConfigDeploymentFlag -Lookup $lookup -Name 'CreateRestorePoint'
|
||||
if ($null -ne $createRestorePoint -and $restorePointCheckBox) {
|
||||
$restorePointCheckBox.IsChecked = $createRestorePoint
|
||||
}
|
||||
|
||||
$registryBackupCheckBox = $Owner.FindName('RegistryBackupCheckBox')
|
||||
if ($registryBackupCheckBox) {
|
||||
if ($lookup.ContainsKey('SkipRegistryBackup')) {
|
||||
$registryBackupCheckBox.IsChecked = -not [bool]$lookup['SkipRegistryBackup']
|
||||
}
|
||||
$skipRegistryBackup = Get-ConfigDeploymentFlag -Lookup $lookup -Name 'SkipRegistryBackup'
|
||||
if ($null -ne $skipRegistryBackup -and $registryBackupCheckBox) {
|
||||
$registryBackupCheckBox.IsChecked = -not $skipRegistryBackup
|
||||
}
|
||||
|
||||
$restartExplorerCheckBox = $Owner.FindName('RestartExplorerCheckBox')
|
||||
if ($lookup.ContainsKey('RestartExplorer') -and $restartExplorerCheckBox) {
|
||||
$restartExplorerCheckBox.IsChecked = [bool]$lookup['RestartExplorer']
|
||||
$restartExplorer = Get-ConfigDeploymentFlag -Lookup $lookup -Name 'RestartExplorer'
|
||||
if ($null -ne $restartExplorer -and $restartExplorerCheckBox) {
|
||||
$restartExplorerCheckBox.IsChecked = $restartExplorer
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -1,3 +1,53 @@
|
||||
<#
|
||||
.SYNOPSIS
|
||||
Reads a boolean deployment value from a configuration file.
|
||||
|
||||
.DESCRIPTION
|
||||
Returns $true or $false for a JSON boolean, for the text true or false (any case) and for the numbers 1 and 0
|
||||
in any JSON notation (1, 1.0 and 1e0 are read as different number types by ConvertFrom-Json).
|
||||
Returns $null for anything else. A plain [bool] cast cannot be used because it turns any non-empty string,
|
||||
such as "false", into $true.
|
||||
#>
|
||||
function ConvertTo-ConfigBoolean {
|
||||
param($Value)
|
||||
|
||||
if ($Value -is [bool]) { return $Value }
|
||||
if ($Value -is [string]) {
|
||||
$text = $Value.Trim()
|
||||
if ($text -ieq 'true') { return $true }
|
||||
if ($text -ieq 'false') { return $false }
|
||||
return $null
|
||||
}
|
||||
if ($Value -is [int] -or $Value -is [long] -or $Value -is [decimal] -or $Value -is [double]) {
|
||||
if ($Value -eq 1) { return $true }
|
||||
if ($Value -eq 0) { return $false }
|
||||
}
|
||||
return $null
|
||||
}
|
||||
|
||||
<#
|
||||
.SYNOPSIS
|
||||
Reads one boolean deployment setting from the deployment values of a configuration file.
|
||||
|
||||
.DESCRIPTION
|
||||
Returns $null when the setting is missing. A setting that is present but is not a boolean (see ConvertTo-ConfigBoolean)
|
||||
also gives $null, after a warning that names the setting and the value, so it is never silently dropped.
|
||||
#>
|
||||
function Get-ConfigDeploymentFlag {
|
||||
param(
|
||||
[hashtable]$Lookup,
|
||||
[string]$Name
|
||||
)
|
||||
|
||||
if (-not $Lookup.ContainsKey($Name)) { return $null }
|
||||
|
||||
$flagValue = ConvertTo-ConfigBoolean $Lookup[$Name]
|
||||
if ($null -eq $flagValue) {
|
||||
Write-Warning "Ignoring deployment setting '$Name' in the config file: expected true or false, found '$($Lookup[$Name])'."
|
||||
}
|
||||
return $flagValue
|
||||
}
|
||||
|
||||
<#
|
||||
.SYNOPSIS
|
||||
Imports valid application, tweak, and deployment selections from a configuration JSON file into active parameters.
|
||||
@@ -81,17 +131,23 @@ function Import-ConfigToParams {
|
||||
}
|
||||
}
|
||||
|
||||
if ($deploymentLookup.ContainsKey('CreateRestorePoint') -and [bool]$deploymentLookup['CreateRestorePoint']) {
|
||||
$deploymentFlags = @{}
|
||||
foreach ($flagName in 'CreateRestorePoint', 'SkipRegistryBackup', 'RestartExplorer') {
|
||||
$flagValue = Get-ConfigDeploymentFlag -Lookup $deploymentLookup -Name $flagName
|
||||
if ($null -ne $flagValue) { $deploymentFlags[$flagName] = $flagValue }
|
||||
}
|
||||
|
||||
if ($deploymentFlags['CreateRestorePoint'] -eq $true) {
|
||||
Add-Parameter 'CreateRestorePoint'
|
||||
$importedItems++
|
||||
}
|
||||
|
||||
if ($deploymentLookup.ContainsKey('SkipRegistryBackup') -and [bool]$deploymentLookup['SkipRegistryBackup']) {
|
||||
if ($deploymentFlags['SkipRegistryBackup'] -eq $true) {
|
||||
Add-Parameter 'SkipRegistryBackup'
|
||||
$importedItems++
|
||||
}
|
||||
|
||||
if ($deploymentLookup.ContainsKey('RestartExplorer') -and -not [bool]$deploymentLookup['RestartExplorer']) {
|
||||
if ($deploymentFlags['RestartExplorer'] -eq $false) {
|
||||
Add-Parameter 'SkipExplorerRestart'
|
||||
$importedItems++
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user