From 3b7a6cd03a61fa997fa100d46b85aeb5afd8f8e0 Mon Sep 17 00:00:00 2001 From: Christoph Date: Thu, 21 Mar 2024 06:26:06 +0100 Subject: [PATCH] Remove optional Windows features --- Main.cs | 5 ++ modifier/Bloatware.cs | 186 ++++++++++++++++++++++------------------ resource/Bloatware.json | 7 ++ 3 files changed, 115 insertions(+), 83 deletions(-) diff --git a/Main.cs b/Main.cs index a22fea2..959992b 100644 --- a/Main.cs +++ b/Main.cs @@ -361,6 +361,11 @@ public class CapabilityBloatwareStep( string selector ) : SelectorBloatwareStep(versions, selector); +public class OptionalFeatureBloatwareStep( + byte[] versions, + string selector +) : SelectorBloatwareStep(versions, selector); + public class CustomBloatwareStep( byte[] versions ) : BloatwareStep(versions); diff --git a/modifier/Bloatware.cs b/modifier/Bloatware.cs index 1469c79..3e0d95e 100644 --- a/modifier/Bloatware.cs +++ b/modifier/Bloatware.cs @@ -3,17 +3,74 @@ namespace Schneegans.Unattend; -class ContentInfo(string baseName) +abstract class Remover where T : SelectorBloatwareStep { - public string BaseName { get; } = baseName ?? throw new ArgumentNullException(nameof(baseName)); + private bool hasContent = false; - public string CmdPath => @$"%TEMP%\{BaseName}.txt"; + protected string CmdPath => @$"%TEMP%\{Tag()}.txt"; - public string PsPath => @$"$env:TEMP\{BaseName}.txt"; + protected string PsPath => @$"$env:TEMP\{Tag()}.txt"; - public string PsLogPath => @$"$env:TEMP\{BaseName}.log"; + protected string PsLogPath => @$"$env:TEMP\{Tag()}.log"; - public bool HasContent { get; set; } = false; + public void Add(T step, CommandAppender appender) + { + appender.Append( + CommandBuilder.WriteToFile(CmdPath, step.Selector) + ); + hasContent = true; + } + + public void Write(CommandAppender appender) + { + if (hasContent) + { + appender.Append(RemoveCommand()); + } + } + + protected abstract string RemoveCommand(); + + protected abstract string Tag(); +} + +class PackageRemover : Remover +{ + protected override string RemoveCommand() + { + return CommandBuilder.PowerShellCommand(@$"Get-AppxProvisionedPackage -Online | where DisplayName -In (Get-Content {PsPath} ) | Remove-AppxProvisionedPackage -AllUsers -Online *>&1 >> {PsLogPath};"); + } + + protected override string Tag() + { + return "remove-packages"; + } +} + +class CapabilityRemover : Remover +{ + protected override string RemoveCommand() + { + return CommandBuilder.PowerShellCommand(@$"Get-WindowsCapability -Online | where {{($_.Name -split '~')[0] -in (Get-Content {PsPath} ) }} | Remove-WindowsCapability -Online *>&1 >> {PsLogPath};"); + } + + protected override string Tag() + { + return "remove-caps"; + } +} + +class FeatureRemover : Remover +{ + protected override string RemoveCommand() + { + return CommandBuilder.PowerShellCommand(@$"Get-WindowsOptionalFeature -Online | where FeatureName -In (Get-Content {PsPath} ) | Disable-WindowsOptionalFeature -Online -Remove -NoRestart *>&1 >> {PsLogPath};"); + } + + protected override string Tag() + { + return "remove-features"; + } } class BloatwareModifier(ModifierContext context) : Modifier(context) @@ -22,24 +79,9 @@ public override void Process() { CommandAppender appender = new(Document, NamespaceManager, CommandConfig.Specialize); - var packages = new ContentInfo("remove-packages"); - var caps = new ContentInfo("remove-caps"); - - void RemovePackage(PackageBloatwareStep step) - { - appender.Append( - CommandBuilder.WriteToFile(packages.CmdPath, step.Selector) - ); - packages.HasContent = true; - } - - void RemoveCapability(CapabilityBloatwareStep step) - { - appender.Append( - CommandBuilder.WriteToFile(caps.CmdPath, step.Selector) - ); - caps.HasContent = true; - } + var packageRemover = new PackageRemover(); + var capabilityRemover = new CapabilityRemover(); + var featureRemover = new FeatureRemover(); foreach (Bloatware bw in Configuration.Bloatwares) { @@ -48,78 +90,56 @@ void RemoveCapability(CapabilityBloatwareStep step) switch (step) { case PackageBloatwareStep package: - { - RemovePackage(package); - break; - } - + packageRemover.Add(package, appender); + break; case CapabilityBloatwareStep capability: - { - RemoveCapability(capability); - break; - } + capabilityRemover.Add(capability, appender); + break; + case OptionalFeatureBloatwareStep feature: + featureRemover.Add(feature, appender); + break; case CustomBloatwareStep when bw.Id == "RemoveOneDrive": - { - appender.Append([ - CommandBuilder.ShellCommand(@"del ""C:\Users\Default\AppData\Roaming\Microsoft\Windows\Start Menu\Programs\OneDrive.lnk"""), - CommandBuilder.ShellCommand(@"del ""C:\Windows\System32\OneDriveSetup.exe"""), - CommandBuilder.ShellCommand(@"del ""C:\Windows\SysWOW64\OneDriveSetup.exe"""), - .. CommandBuilder.RegistryDefaultUserCommand((rootKey, subKey) => { + appender.Append([ + CommandBuilder.ShellCommand(@"del ""C:\Users\Default\AppData\Roaming\Microsoft\Windows\Start Menu\Programs\OneDrive.lnk"""), + CommandBuilder.ShellCommand(@"del ""C:\Windows\System32\OneDriveSetup.exe"""), + CommandBuilder.ShellCommand(@"del ""C:\Windows\SysWOW64\OneDriveSetup.exe"""), + .. CommandBuilder.RegistryDefaultUserCommand((rootKey, subKey) => { return [CommandBuilder.RegistryCommand(@$"delete ""{rootKey}\{subKey}\Software\Microsoft\Windows\CurrentVersion\Run"" /v OneDriveSetup /f")]; - }), - ]); - break; - } + }), + ]); + break; case CustomBloatwareStep when bw.Id == "RemoveTeams": - { - appender.Append( - CommandBuilder.RegistryCommand(@"add ""HKLM\SOFTWARE\Microsoft\Windows\CurrentVersion\Communications"" /v ConfigureChatAutoInstall /t REG_DWORD /d 0 /f") - ); - break; - } + appender.Append( + CommandBuilder.RegistryCommand(@"add ""HKLM\SOFTWARE\Microsoft\Windows\CurrentVersion\Communications"" /v ConfigureChatAutoInstall /t REG_DWORD /d 0 /f") + ); + break; case CustomBloatwareStep when bw.Id == "RemoveNotepad": - { - appender.Append( - CommandBuilder.RegistryDefaultUserCommand((rootKey, subKey) => - { - return [CommandBuilder.RegistryCommand(@$"add ""{rootKey}\{subKey}\Software\Microsoft\Notepad"" /v ShowStoreBanner /t REG_DWORD /d 0 /f")]; - }) - ); - break; - } + appender.Append( + CommandBuilder.RegistryDefaultUserCommand((rootKey, subKey) => + { + return [CommandBuilder.RegistryCommand(@$"add ""{rootKey}\{subKey}\Software\Microsoft\Notepad"" /v ShowStoreBanner /t REG_DWORD /d 0 /f")]; + }) + ); + break; case CustomBloatwareStep when bw.Id == "RemoveOutlook": - { - appender.Append( - CommandBuilder.RegistryCommand(@"delete ""HKLM\SOFTWARE\Microsoft\WindowsUpdate\Orchestrator\UScheduler_Oobe\OutlookUpdate"" /f") - ); - break; - } + appender.Append( + CommandBuilder.RegistryCommand(@"delete ""HKLM\SOFTWARE\Microsoft\WindowsUpdate\Orchestrator\UScheduler_Oobe\OutlookUpdate"" /f") + ); + break; case CustomBloatwareStep when bw.Id == "RemoveDevHome": - { - appender.Append( - CommandBuilder.RegistryCommand(@"delete ""HKLM\SOFTWARE\Microsoft\WindowsUpdate\Orchestrator\UScheduler_Oobe\DevHomeUpdate"" /f") - ); - break; - } + appender.Append( + CommandBuilder.RegistryCommand(@"delete ""HKLM\SOFTWARE\Microsoft\WindowsUpdate\Orchestrator\UScheduler_Oobe\DevHomeUpdate"" /f") + ); + break; default: throw new NotSupportedException(); } } } - if (packages.HasContent) - { - appender.Append( - CommandBuilder.PowerShellCommand(@$"Get-AppxProvisionedPackage -Online | where DisplayName -In (Get-Content {packages.PsPath} ) | Remove-AppxProvisionedPackage -AllUsers -Online *>&1 >> {packages.PsLogPath};") - ); - } - - if (caps.HasContent) - { - appender.Append( - CommandBuilder.PowerShellCommand(@$"Get-WindowsCapability -Online | where {{($_.Name -split '~')[0] -in (Get-Content {caps.PsPath} ) }} | Remove-WindowsCapability -Online *>&1 >> {caps.PsLogPath};") - ); - } + packageRemover.Write(appender); + capabilityRemover.Write(appender); + featureRemover.Write(appender); if (!Configuration.Bloatwares.IsEmpty) { diff --git a/resource/Bloatware.json b/resource/Bloatware.json index b579647..4a4a67a 100644 --- a/resource/Bloatware.json +++ b/resource/Bloatware.json @@ -152,6 +152,13 @@ 10, 11 ] + }, + { + "$type": "Schneegans.Unattend.OptionalFeatureBloatwareStep, UnattendGenerator", + "Selector": "Microsoft-SnippingTool", + "Versions": [ + 10 + ] } ] },