From 73834839ffb0373bad993c3e11e7122c9b6bcb11 Mon Sep 17 00:00:00 2001 From: HetCreep <239326951+HetCreep@users.noreply.github.com> Date: Mon, 19 Oct 2026 03:23:18 +0700 Subject: [PATCH] fix: skip features that the running Windows build does not support (#832) --- Scripts/Features/Invoke-Changes.ps1 | 15 +++ Scripts/FileIO/Import-Settings.ps1 | 2 +- Scripts/GUI/MainWindow-TweaksBuilder.ps1 | 4 +- Scripts/Helpers/Import-ConfigToParams.ps1 | 2 +- Scripts/Helpers/Windows-BuildHelpers.ps1 | 28 +++++ Tests/Import-ConfigToParams.Tests.ps1 | 1 + Tests/Invoke-Changes.Tests.ps1 | 119 ++++++++++++++++++++++ Tests/MainWindow-AppSelection.Tests.ps1 | 1 + Tests/MainWindow-Localization.Tests.ps1 | 2 +- Tests/Registry-BackupCreation.Tests.ps1 | 1 + Tests/Settings-And-Apps-FileIO.Tests.ps1 | 1 + Tests/Windows-BuildHelpers.Tests.ps1 | 68 +++++++++++++ Win11Debloat.ps1 | 1 + 13 files changed, 240 insertions(+), 5 deletions(-) create mode 100644 Scripts/Helpers/Windows-BuildHelpers.ps1 create mode 100644 Tests/Windows-BuildHelpers.Tests.ps1 diff --git a/Scripts/Features/Invoke-Changes.ps1 b/Scripts/Features/Invoke-Changes.ps1 index f892a7a..38709d9 100644 --- a/Scripts/Features/Invoke-Changes.ps1 +++ b/Scripts/Features/Invoke-Changes.ps1 @@ -363,6 +363,21 @@ function Invoke-AllChanges { } $undoIds = @($script:UndoParams.Keys) + # Features passed as switches are not filtered by the GUI or the config import, so skip the ones this Windows build does not support + $currentBuild = Get-CurrentWindowsBuild + $unsupportedIds = @($applyIds | Where-Object { -not (Test-FeatureSupportedOnBuild -Feature $script:Features[$_] -Build $currentBuild) }) + foreach ($id in $unsupportedIds) { + $feature = $script:Features[$id] + $range = @() + if ($feature.MinVersion) { $range += "build $($feature.MinVersion) or newer" } + if ($feature.MaxVersion) { $range += "build $($feature.MaxVersion) or older" } + Write-Warning "Skipping ${id}: it is only supported on Windows $($range -join ' and '), this system runs build $currentBuild." + + # Also drop it from the parameters, so that later steps (such as the reboot advice) only see what was applied + [void]$script:Params.Remove($id) + } + $applyIds = @($applyIds | Where-Object { $unsupportedIds -notcontains $_ }) + # ---- Determine if registry backup is needed ---- $needsBackup = $false foreach ($id in $applyIds) { diff --git a/Scripts/FileIO/Import-Settings.ps1 b/Scripts/FileIO/Import-Settings.ps1 index 0c83268..b163326 100644 --- a/Scripts/FileIO/Import-Settings.ps1 +++ b/Scripts/FileIO/Import-Settings.ps1 @@ -28,7 +28,7 @@ function Import-Settings { if (-not $feature) { continue } # Check version and feature compatibility using Features.json - if (($feature.MinVersion -and $WinVersion -lt $feature.MinVersion) -or ($feature.MaxVersion -and $WinVersion -gt $feature.MaxVersion) -or ($feature.FeatureId -eq 'DisableModernStandbyNetworking' -and (-not $script:ModernStandbySupported))) { + if (-not (Test-FeatureSupportedOnBuild -Feature $feature -Build $WinVersion) -or ($feature.FeatureId -eq 'DisableModernStandbyNetworking' -and (-not $script:ModernStandbySupported))) { continue } diff --git a/Scripts/GUI/MainWindow-TweaksBuilder.ps1 b/Scripts/GUI/MainWindow-TweaksBuilder.ps1 index 1000248..2f75d44 100644 --- a/Scripts/GUI/MainWindow-TweaksBuilder.ps1 +++ b/Scripts/GUI/MainWindow-TweaksBuilder.ps1 @@ -418,7 +418,7 @@ function New-DynamicTweakControls { if ($feature.Category -ne $categoryName) { $featureIndex++; continue } # Check version and feature compatibility using Features.json - if (($feature.MinVersion -and $WinVersion -lt $feature.MinVersion) -or ($feature.MaxVersion -and $WinVersion -gt $feature.MaxVersion) -or ($feature.FeatureId -eq 'DisableModernStandbyNetworking' -and (-not $script:ModernStandbySupported))) { + if (-not (Test-FeatureSupportedOnBuild -Feature $feature -Build $WinVersion) -or ($feature.FeatureId -eq 'DisableModernStandbyNetworking' -and (-not $script:ModernStandbySupported))) { $featureIndex++; continue } @@ -451,7 +451,7 @@ function New-DynamicTweakControls { foreach ($fid in $_.FeatureIds) { if ($featureMap.ContainsKey($fid)) { $f = $featureMap[$fid] - if (($f.MinVersion -and $WinVersion -lt $f.MinVersion) -or ($f.MaxVersion -and $WinVersion -gt $f.MaxVersion)) { + if (-not (Test-FeatureSupportedOnBuild -Feature $f -Build $WinVersion)) { $allCompatible = $false break } diff --git a/Scripts/Helpers/Import-ConfigToParams.ps1 b/Scripts/Helpers/Import-ConfigToParams.ps1 index cdd23e2..10363e3 100644 --- a/Scripts/Helpers/Import-ConfigToParams.ps1 +++ b/Scripts/Helpers/Import-ConfigToParams.ps1 @@ -114,7 +114,7 @@ function Import-ConfigToParams { continue } - if (($feature.MinVersion -and $CurrentBuild -lt $feature.MinVersion) -or ($feature.MaxVersion -and $CurrentBuild -gt $feature.MaxVersion) -or ($feature.FeatureId -eq 'DisableModernStandbyNetworking' -and (-not $script:ModernStandbySupported))) { + if (-not (Test-FeatureSupportedOnBuild -Feature $feature -Build $CurrentBuild) -or ($feature.FeatureId -eq 'DisableModernStandbyNetworking' -and (-not $script:ModernStandbySupported))) { continue } diff --git a/Scripts/Helpers/Windows-BuildHelpers.ps1 b/Scripts/Helpers/Windows-BuildHelpers.ps1 new file mode 100644 index 0000000..85cd839 --- /dev/null +++ b/Scripts/Helpers/Windows-BuildHelpers.ps1 @@ -0,0 +1,28 @@ +# Returns the build number of the running Windows version, or $null when it cannot be read. +function Get-CurrentWindowsBuild { + $build = 0 + try { + # Get-ItemPropertyValue reports a missing value as a terminating error, which -ErrorAction SilentlyContinue does not suppress + $currentBuild = Get-ItemPropertyValue 'HKLM:\SOFTWARE\Microsoft\Windows NT\CurrentVersion' CurrentBuild -ErrorAction Stop + } + catch { + Write-Verbose "Unable to read the Windows build number: $($_.Exception.Message)" + return $null + } + if ([int]::TryParse([string]$currentBuild, [ref]$build)) { return $build } + return $null +} + +# Checks a feature's MinVersion / MaxVersion (Windows build numbers from Features.json) against a build number. +# A feature without limits, or a build that is not known, is treated as supported. +function Test-FeatureSupportedOnBuild { + param( + $Feature, + $Build + ) + + if ($null -eq $Feature -or $null -eq $Build) { return $true } + if ($Feature.MinVersion -and [int]$Build -lt [int]$Feature.MinVersion) { return $false } + if ($Feature.MaxVersion -and [int]$Build -gt [int]$Feature.MaxVersion) { return $false } + return $true +} diff --git a/Tests/Import-ConfigToParams.Tests.ps1 b/Tests/Import-ConfigToParams.Tests.ps1 index cd1f8ba..51f147e 100644 --- a/Tests/Import-ConfigToParams.Tests.ps1 +++ b/Tests/Import-ConfigToParams.Tests.ps1 @@ -2,6 +2,7 @@ BeforeAll { . (Join-Path $PSScriptRoot '..\Scripts\FileIO\Import-JsonFile.ps1') . (Join-Path $PSScriptRoot '..\Scripts\FileIO\Import-LanguageFile.ps1') . (Join-Path $PSScriptRoot '..\Scripts\Helpers\Add-Parameter.ps1') + . (Join-Path $PSScriptRoot '..\Scripts\Helpers\Windows-BuildHelpers.ps1') . (Join-Path $PSScriptRoot '..\Scripts\Helpers\Import-ConfigToParams.ps1') . (Join-Path $PSScriptRoot '..\Scripts\Helpers\Test-ConfigConsistency.ps1') $script:ConfigFixturePath = Join-Path $PSScriptRoot 'TestData\JsonFileLoading\ExportedConfig.WithSettings.json' diff --git a/Tests/Invoke-Changes.Tests.ps1 b/Tests/Invoke-Changes.Tests.ps1 index 3a0059e..c20c9e1 100644 --- a/Tests/Invoke-Changes.Tests.ps1 +++ b/Tests/Invoke-Changes.Tests.ps1 @@ -21,6 +21,8 @@ BeforeAll { function Set-StoreSearchSuggestionsDisabled { param($StoreAppsDatabase) $true } . (Join-Path $PSScriptRoot '..\Scripts\FileIO\Import-LanguageFile.ps1') + . (Join-Path $PSScriptRoot '..\Scripts\Helpers\Windows-BuildHelpers.ps1') + . (Join-Path $PSScriptRoot '..\Scripts\Helpers\Get-RebootFeatureLabels.ps1') . (Join-Path $PSScriptRoot '..\Scripts\Features\Invoke-Changes.ps1') # Builds a $script:Lang fixture whose Features section mirrors $script:Features exactly, @@ -425,6 +427,7 @@ Describe 'Invoke-AllChanges' { $script:CancelRequested = $false $script:ApplyProgressCallback = $null Mock Test-RunningAsSystem { $false } + Mock Get-CurrentWindowsBuild { 26100 } Mock Resolve-UndoRegFilePath { param($FileName) "Undo\$FileName" } Mock New-RegistrySettingsBackup {} Mock Invoke-SystemRestorePoint { $true } @@ -540,3 +543,119 @@ Describe 'Invoke-AllChanges' { } } + +Describe 'Invoke-AllChanges build gate' { + BeforeEach { + $script:Params = @{ Modern = $true; Legacy = $true; Anyone = $true } + $script:UndoParams = @{ ModernUndo = $true } + $script:ControlParams = @('WhatIf', 'Silent', 'User', 'Sysprep') + $script:Features = @{ + Modern = [PSCustomObject]@{ RegistryKey = 'modern.reg'; MinVersion = 22621; MaxVersion = $null } + Legacy = [PSCustomObject]@{ RegistryKey = 'legacy.reg'; MinVersion = $null; MaxVersion = 21999 } + Anyone = [PSCustomObject]@{ RegistryKey = 'anyone.reg' } + ModernUndo = [PSCustomObject]@{ RegistryUndoKey = 'undo.reg'; MinVersion = 22621; MaxVersion = $null } + } + $script:CancelRequested = $false + $script:ApplyProgressCallback = $null + $warnings = [System.Collections.Generic.List[string]]::new() + Mock Test-RunningAsSystem { $false } + Mock Resolve-UndoRegFilePath { param($FileName) "Undo\$FileName" } + Mock New-RegistrySettingsBackup {} + Mock Invoke-SystemRestorePoint { $true } + Mock Invoke-ApplyFeatures {} + Mock Invoke-UndoFeatures {} + Mock Write-Host {} + Mock Write-Warning { $warnings.Add([string]$Message) } + } + + It 'skips features that need a newer build and keeps the rest' { + Mock Get-CurrentWindowsBuild { 19045 } + + Invoke-AllChanges + + Should -Invoke Invoke-ApplyFeatures -Times 1 -Exactly -ParameterFilter { + @($FeatureIds).Count -eq 2 -and $FeatureIds -contains 'Legacy' -and $FeatureIds -contains 'Anyone' + } + $warnings | Should -HaveCount 1 + $warnings[0] | Should -BeLike 'Skipping Modern: * build 22621 or newer*build 19045*' + } + + It 'skips features that are only supported on an older build' { + Mock Get-CurrentWindowsBuild { 26100 } + + Invoke-AllChanges + + Should -Invoke Invoke-ApplyFeatures -Times 1 -Exactly -ParameterFilter { + @($FeatureIds).Count -eq 2 -and $FeatureIds -contains 'Modern' -and $FeatureIds -contains 'Anyone' + } + $warnings | Should -HaveCount 1 + $warnings[0] | Should -BeLike 'Skipping Legacy: * build 21999 or older*build 26100*' + } + + It 'does not back up the registry keys of skipped features' { + Mock Get-CurrentWindowsBuild { 19045 } + + Invoke-AllChanges + + Should -Invoke New-RegistrySettingsBackup -Times 1 -Exactly -ParameterFilter { + $ActionableKeys -notcontains 'Modern' -and $ActionableKeys -contains 'Legacy' + } + } + + It 'does not apply anything gated when every selected feature is skipped' { + Mock Get-CurrentWindowsBuild { 19045 } + $script:Params = @{ Modern = $true } + $script:UndoParams = @{} + + Invoke-AllChanges + + Should -Invoke Invoke-ApplyFeatures -Times 0 -Exactly + Should -Invoke New-RegistrySettingsBackup -Times 0 -Exactly + $warnings | Should -HaveCount 1 + } + + It 'applies every feature when the build cannot be read' { + Mock Get-CurrentWindowsBuild { $null } + + Invoke-AllChanges + + Should -Invoke Invoke-ApplyFeatures -Times 1 -Exactly -ParameterFilter { @($FeatureIds).Count -eq 3 } + $warnings | Should -HaveCount 0 + } + + It 'drops skipped features from the parameters so that they are not part of the reboot advice' { + Mock Get-CurrentWindowsBuild { 19045 } + Mock Get-Translation { $Key } + $script:Features['Modern'] | Add-Member -NotePropertyName RequiresReboot -NotePropertyValue $true + $script:Features['Legacy'] | Add-Member -NotePropertyName RequiresReboot -NotePropertyValue $true + + Invoke-AllChanges + + $script:Params.ContainsKey('Modern') | Should -BeFalse + $script:Params.ContainsKey('Legacy') | Should -BeTrue + $labels = @(Get-RebootFeatureLabels) + $labels | Should -Contain 'Legacy' + $labels | Should -Not -Contain 'Modern' + } + + It 'does not write anything to the output when it skips a feature' { + Mock Get-CurrentWindowsBuild { 19045 } + # $PSBoundParameters is a generic dictionary whose Remove() returns a Boolean, unlike Hashtable.Remove() + $script:Params = [System.Collections.Generic.Dictionary[string, object]]::new() + $script:Params['Modern'] = $true + $script:Params['Anyone'] = $true + + @(Invoke-AllChanges) | Should -HaveCount 0 + $script:Params.ContainsKey('Modern') | Should -BeFalse + } + + It 'leaves undo selections alone' { + Mock Get-CurrentWindowsBuild { 19045 } + + Invoke-AllChanges + + Should -Invoke Invoke-UndoFeatures -Times 1 -Exactly -ParameterFilter { + @($FeatureIds).Count -eq 1 -and $FeatureIds[0] -eq 'ModernUndo' + } + } +} diff --git a/Tests/MainWindow-AppSelection.Tests.ps1 b/Tests/MainWindow-AppSelection.Tests.ps1 index 7d58a4f..f8ed537 100644 --- a/Tests/MainWindow-AppSelection.Tests.ps1 +++ b/Tests/MainWindow-AppSelection.Tests.ps1 @@ -8,6 +8,7 @@ BeforeAll { . (Join-Path $PSScriptRoot '..\Scripts\GUI\MainWindow-AppSelection.ps1') . (Join-Path $PSScriptRoot '..\Scripts\GUI\MainWindow-Deployment.ps1') . (Join-Path $PSScriptRoot '..\Scripts\GUI\MainWindow-Navigation.ps1') + . (Join-Path $PSScriptRoot '..\Scripts\Helpers\Windows-BuildHelpers.ps1') . (Join-Path $PSScriptRoot '..\Scripts\GUI\MainWindow-TweaksBuilder.ps1') . (Join-Path $PSScriptRoot '..\Scripts\GUI\Set-WindowThemeResources.ps1') $script:LanguagesPath = Join-Path $PSScriptRoot '..\Config\Languages' diff --git a/Tests/MainWindow-Localization.Tests.ps1 b/Tests/MainWindow-Localization.Tests.ps1 index 6956cd7..2bdfe4d 100644 --- a/Tests/MainWindow-Localization.Tests.ps1 +++ b/Tests/MainWindow-Localization.Tests.ps1 @@ -5,7 +5,7 @@ BeforeAll { foreach ($file in @('FileIO/Import-JsonFile', 'FileIO/Import-LanguageFile', 'GUI/Ui-Preferences', 'GUI/Show-SettingsDialog', 'GUI/Set-WindowThemeResources', 'GUI/MainWindow-Localization', 'GUI/MainWindow-TweaksBuilder', 'GUI/MainWindow-AppSelection', - 'GUI/MainWindow-Deployment', 'GUI/Show-Bubble', 'Helpers/Get-UserName')) { + 'GUI/MainWindow-Deployment', 'GUI/Show-Bubble', 'Helpers/Get-UserName', 'Helpers/Windows-BuildHelpers')) { . "$script:RepoRoot/Scripts/$file.ps1" } $script:LanguagesPath = Join-Path $script:RepoRoot 'Config/Languages' diff --git a/Tests/Registry-BackupCreation.Tests.ps1 b/Tests/Registry-BackupCreation.Tests.ps1 index 22d59aa..47d72c2 100644 --- a/Tests/Registry-BackupCreation.Tests.ps1 +++ b/Tests/Registry-BackupCreation.Tests.ps1 @@ -2,6 +2,7 @@ BeforeAll { . (Join-Path $PSScriptRoot '..\Scripts\Helpers\Registry-PathHelpers.ps1') . (Join-Path $PSScriptRoot '..\Scripts\Helpers\Get-RegFileOperations.ps1') . (Join-Path $PSScriptRoot '..\Scripts\Helpers\User-HiveHelpers.ps1') + . (Join-Path $PSScriptRoot '..\Scripts\Helpers\Windows-BuildHelpers.ps1') . (Join-Path $PSScriptRoot '..\Scripts\Features\Invoke-Changes.ps1') . (Join-Path $PSScriptRoot '..\Scripts\Features\Backup-RegistryFeatureSelection.ps1') . (Join-Path $PSScriptRoot '..\Scripts\FileIO\Save-ToFile.ps1') diff --git a/Tests/Settings-And-Apps-FileIO.Tests.ps1 b/Tests/Settings-And-Apps-FileIO.Tests.ps1 index d2f97d3..eed011a 100644 --- a/Tests/Settings-And-Apps-FileIO.Tests.ps1 +++ b/Tests/Settings-And-Apps-FileIO.Tests.ps1 @@ -6,6 +6,7 @@ BeforeAll { . (Join-Path $PSScriptRoot '..\Scripts\FileIO\Import-LanguageFile.ps1') . (Join-Path $PSScriptRoot '..\Scripts\Helpers\Add-Parameter.ps1') . (Join-Path $PSScriptRoot '..\Scripts\FileIO\Save-ToFile.ps1') + . (Join-Path $PSScriptRoot '..\Scripts\Helpers\Windows-BuildHelpers.ps1') . (Join-Path $PSScriptRoot '..\Scripts\FileIO\Import-Settings.ps1') . (Join-Path $PSScriptRoot '..\Scripts\FileIO\Save-Settings.ps1') . (Join-Path $PSScriptRoot '..\Scripts\FileIO\Import-AppsFromFile.ps1') diff --git a/Tests/Windows-BuildHelpers.Tests.ps1 b/Tests/Windows-BuildHelpers.Tests.ps1 new file mode 100644 index 0000000..b1b3c1f --- /dev/null +++ b/Tests/Windows-BuildHelpers.Tests.ps1 @@ -0,0 +1,68 @@ +BeforeAll { + . (Join-Path $PSScriptRoot '..\Scripts\Helpers\Windows-BuildHelpers.ps1') +} + +Describe 'Get-CurrentWindowsBuild' { + It 'returns the build number as a number' { + Mock Get-ItemPropertyValue { '26100' } + + Get-CurrentWindowsBuild | Should -Be 26100 + Get-CurrentWindowsBuild | Should -BeOfType [int] + } + + It 'returns nothing when the value is not a number' { + Mock Get-ItemPropertyValue { 'unknown' } + + Get-CurrentWindowsBuild | Should -BeNullOrEmpty + } + + It 'returns nothing instead of failing when reading the registry throws' { + Mock Get-ItemPropertyValue { throw 'Property CurrentBuild does not exist' } + + { Get-CurrentWindowsBuild } | Should -Not -Throw + Get-CurrentWindowsBuild | Should -BeNullOrEmpty + } + + It 'asks for a terminating error so that a missing value is caught' { + Mock Get-ItemPropertyValue { '19045' } + + Get-CurrentWindowsBuild | Out-Null + + Should -Invoke Get-ItemPropertyValue -Times 1 -Exactly -ParameterFilter { $ErrorAction -eq 'Stop' } + } +} + +Describe 'Test-FeatureSupportedOnBuild' { + It 'returns for MinVersion , MaxVersion on build ' -ForEach @( + @{ Min = 22621; Max = $null; Build = 19045; Expected = $false } + @{ Min = 22621; Max = $null; Build = 22620; Expected = $false } + @{ Min = 22621; Max = $null; Build = 22621; Expected = $true } + @{ Min = 22621; Max = $null; Build = 26100; Expected = $true } + @{ Min = $null; Max = 21999; Build = 19045; Expected = $true } + @{ Min = $null; Max = 21999; Build = 21999; Expected = $true } + @{ Min = $null; Max = 21999; Build = 22000; Expected = $false } + @{ Min = 22000; Max = 22631; Build = 22631; Expected = $true } + @{ Min = 22000; Max = 22631; Build = 26100; Expected = $false } + @{ Min = $null; Max = $null; Build = 10240; Expected = $true } + ) { + $feature = [PSCustomObject]@{ MinVersion = $Min; MaxVersion = $Max } + + Test-FeatureSupportedOnBuild -Feature $feature -Build $Build | Should -Be $Expected + } + + It 'does not limit a feature when the build is not known' { + $feature = [PSCustomObject]@{ MinVersion = 22621; MaxVersion = $null } + + Test-FeatureSupportedOnBuild -Feature $feature -Build $null | Should -BeTrue + } + + It 'does not limit an unknown feature' { + Test-FeatureSupportedOnBuild -Feature $null -Build 19045 | Should -BeTrue + } + + It 'compares build numbers as numbers when the build is a string' { + $feature = [PSCustomObject]@{ MinVersion = 22621; MaxVersion = $null } + + Test-FeatureSupportedOnBuild -Feature $feature -Build '9600' | Should -BeFalse + } +} diff --git a/Win11Debloat.ps1 b/Win11Debloat.ps1 index 6b980eb..bd5db98 100644 --- a/Win11Debloat.ps1 +++ b/Win11Debloat.ps1 @@ -437,6 +437,7 @@ if (-not $script:WingetInstalled -and -not $Silent) { . "$PSScriptRoot/Scripts/Helpers/Get-FriendlyRegistryBackupTarget.ps1" . "$PSScriptRoot/Scripts/Helpers/Get-FriendlyTargetUserName.ps1" . "$PSScriptRoot/Scripts/Helpers/Get-RebootFeatureLabels.ps1" +. "$PSScriptRoot/Scripts/Helpers/Windows-BuildHelpers.ps1" . "$PSScriptRoot/Scripts/Helpers/Import-ConfigToParams.ps1" . "$PSScriptRoot/Scripts/Helpers/Test-ConfigConsistency.ps1" . "$PSScriptRoot/Scripts/Helpers/Get-TargetUserForAppRemoval.ps1"