From 9871739727b98f36c25dbc094a955c27682107a0 Mon Sep 17 00:00:00 2001 From: Elliot Chernofsky Date: Fri, 1 Aug 2025 23:02:28 +0000 Subject: [PATCH 1/2] Refactor: Create function to handle start menu cleanup Refactors to use a new function. This change eliminates code duplication between the and sections, making the script more maintainable. --- packages/debloat.vm/debloat.vm.nuspec | 2 +- .../debloat.vm/tools/chocolateyinstall.ps1 | 46 ++++++++++++++----- 2 files changed, 36 insertions(+), 12 deletions(-) diff --git a/packages/debloat.vm/debloat.vm.nuspec b/packages/debloat.vm/debloat.vm.nuspec index 6d2721de4..f50987607 100644 --- a/packages/debloat.vm/debloat.vm.nuspec +++ b/packages/debloat.vm/debloat.vm.nuspec @@ -2,7 +2,7 @@ debloat.vm - 0.0.0.20250731 + 0.0.0.20250801 Debloat and performance configurations for Windows OS Mandiant diff --git a/packages/debloat.vm/tools/chocolateyinstall.ps1 b/packages/debloat.vm/tools/chocolateyinstall.ps1 index 5b63e11c6..96bcf91c7 100644 --- a/packages/debloat.vm/tools/chocolateyinstall.ps1 +++ b/packages/debloat.vm/tools/chocolateyinstall.ps1 @@ -54,6 +54,33 @@ function Fix-AppxPackageDeployment { } } +function Clean-Win11StartMenu { + <# + .SYNOPSIS + Cleans up the start menu by copying a predefined binary file. + + .DESCRIPTION + This function handles the logic for cleaning up the Windows 11 start menu + by replacing the default configuration files. It uses a predefined + 'start2.bin' file to ensure a consistent, clean start menu layout. + This is a shared function called by both 'Win11' and 'Win11ARM' sections + to avoid code duplication. + #> + [CmdletBinding()] + param( + [Parameter(Mandatory = $true)] + [string]$PackageStartDir + ) + + VM-Write-Log "INFO" "Cleaning up start menu in Windows 11." + + # Cleanest solution possible given lack of relative path and infinite paths for user download location + Copy-Item -Path (Join-Path $PackageStartDir "start2.bin") -Destination (Join-Path ${Env:UserProfile} "Appdata\Local\Packages\Microsoft.Windows.StartMenuExperienceHost_cw5n1h2txyewy\LocalState\") + + # Cover case in older win11 versions where the config file is still start.bin + Copy-Item -Path (Join-Path $PackageStartDir "start2.bin") -Destination (Join-Path ${Env:UserProfile} "Appdata\Local\Packages\Microsoft.Windows.StartMenuExperienceHost_cw5n1h2txyewy\LocalState\start.bin") +} + try { # Determine OS Version $osVersion = VM-Get-WindowsVersion @@ -62,24 +89,21 @@ try { $packageStartDir = Join-Path $packageToolsDir "start" -Resolve switch ($osVersion) { - "Win10" { $config = Join-Path $packageToolsDir "win10.xml" } + "Win10" { + $config = Join-Path $packageToolsDir "win10.xml" + } "Win11" { $config = Join-Path $packageToolsDir "win11.xml" - VM-Write-Log "INFO" "Cleaning up start menu in Windows 11." - # Clean up start menu. Cleanest solution possible given lack - # of relative path and inifinite paths for user download location - Copy-Item -Path (Join-Path $packageStartDir "start2.bin") -Destination (Join-Path ${Env:UserProfile} "Appdata\Local\Packages\Microsoft.Windows.StartMenuExperienceHost_cw5n1h2txyewy\LocalState\") - # cover case in older win11 versions where the config file is still start.bin - Copy-Item -Path (Join-Path $packageStartDir "start2.bin") -Destination (Join-Path ${Env:UserProfile} "Appdata\Local\Packages\Microsoft.Windows.StartMenuExperienceHost_cw5n1h2txyewy\LocalState\start.bin") + # Call the new function to clean the start menu + Clean-Win11StartMenu -PackageStartDir $packageStartDir # Call the function to apply the AppxPackage fix for Windows 11 Fix-AppxPackageDeployment } "Win11ARM" { $config = Join-Path $packageToolsDir "win11arm.xml" - VM-Write-Log "INFO" "Cleaning up start menu in Windows 11." - Copy-Item -Path (Join-Path $packageStartDir "start2.bin") -Destination (Join-Path ${Env:UserProfile} "Appdata\Local\Packages\Microsoft.Windows.StartMenuExperienceHost_cw5n1h2txyewy\LocalState\") - Copy-Item -Path (Join-Path $packageStartDir "start2.bin") -Destination (Join-Path ${Env:UserProfile} "Appdata\Local\Packages\Microsoft.Windows.StartMenuExperienceHost_cw5n1h2txyewy\LocalState\start.bin") + # Call the new function to clean the start menu + Clean-Win11StartMenu -PackageStartDir $packageStartDir } default { VM-Write-Log "WARN" "Debloater unable to determine Windows version, defaulting to Windows 10." @@ -92,4 +116,4 @@ try { } catch { VM-Write-Log-Exception $_ -} +} \ No newline at end of file From f247720a43d67024d8ac5b53cda626d35891c9b2 Mon Sep 17 00:00:00 2001 From: Elliot Chernofsky Date: Sat, 2 Aug 2025 00:19:33 +0000 Subject: [PATCH 2/2] Fix: Improve error handling and prevent script from halting Refactored the function to include granular blocks within each processing loop. This prevents a single configuration error, such as an invalid registry type, from halting the entire script. The change ensures that the script continues to process subsequent items in the XML configuration file, while still logging a specific error message for any individual item that fails. --- packages/common.vm/common.vm.nuspec | 2 +- .../common.vm/tools/vm.common/vm.common.psm1 | 94 ++++++++---- packages/debloat.vm/debloat.vm.nuspec | 2 +- packages/debloat.vm/tools/win11.xml | 134 +++++++++--------- packages/debloat.vm/tools/win11arm.xml | 134 +++++++++--------- 5 files changed, 202 insertions(+), 164 deletions(-) diff --git a/packages/common.vm/common.vm.nuspec b/packages/common.vm/common.vm.nuspec index b63a7ab20..95944276b 100755 --- a/packages/common.vm/common.vm.nuspec +++ b/packages/common.vm/common.vm.nuspec @@ -2,7 +2,7 @@ common.vm - 0.0.0.20250509 + 0.0.0.20250801 Common libraries for VM-packages Mandiant diff --git a/packages/common.vm/tools/vm.common/vm.common.psm1 b/packages/common.vm/tools/vm.common/vm.common.psm1 index 7b640f498..06c43ab8e 100755 --- a/packages/common.vm/tools/vm.common/vm.common.psm1 +++ b/packages/common.vm/tools/vm.common/vm.common.psm1 @@ -1281,17 +1281,17 @@ function VM-Set-Service-Manual-Start { $service = Get-Service -Name $serviceName -ErrorAction SilentlyContinue if ($service) { if ($service.Status -eq "Running") { - Write-Output "INFO" "Stopping service $serviceName..." + VM-Write-Log "INFO" "Stopping service $serviceName..." Stop-Service -Name $service.Name -Force -ErrorAction Stop - Write-Output "INFO" "Service $serviceName has been stopped." + VM-Write-Log "INFO" "Service $serviceName has been stopped." } Set-Service -Name $service.Name -StartupType Manual - Write-Output "INFO" "Service $serviceName has been set to manual startup." + VM-Write-Log "INFO" "Service $serviceName has been set to manual startup." } else { - Write-Output "WARN" "Service $serviceName not found." + VM-Write-Log "WARN" "Service $serviceName not found." } } catch { - Write-Output "ERROR" "An error occurred: $_" + VM-Write-Log "ERROR" "An error occurred: $_" } } @@ -1517,6 +1517,8 @@ function VM-Configure-PS-Logging { # Main function for debloater and configuration changes # Expects an XML file function VM-Apply-Configurations { + # Main function for debloater and configuration changes + # Expects an XML file param( [Parameter(Position = 0)] [string]$configFile @@ -1526,73 +1528,109 @@ function VM-Apply-Configurations { # Load and parse the XML config file VM-Assert-Path $configFile $config = [xml](Get-Content $configFile) + } catch { + VM-Write-Log "ERROR" "An error occurred while loading or parsing the config file. Error: $_" + return # Exit the function if the file cannot be loaded. + } - # Process the apps - if ($config.config.apps.app) { - $config.config.apps.app | ForEach-Object { + # Process the apps + if ($config.config.apps.app) { + VM-Write-Log "INFO" "Processing Appx Packages..." + $config.config.apps.app | ForEach-Object { + try { $appName = $_.name VM-Remove-Appx-Package -appName $appName + } catch { + VM-Write-Log "ERROR" "Failed to remove app '$appName'. Error: $($_.Exception.Message)" } } + } - # Process the services - if ($config.config.services.service) { - $config.config.services.service | ForEach-Object { + # Process the services + if ($config.config.services.service) { + VM-Write-Log "INFO" "Processing services..." + $config.config.services.service | ForEach-Object { + try { $serviceName = $_.name VM-Set-Service-Manual-Start -serviceName $serviceName + } catch { + VM-Write-Log "ERROR" "Failed to set service '$serviceName' to manual start. Error: $($_.Exception.Message)" } } + } - # Process the tasks - if ($config.config.tasks.task) { - $config.config.tasks.task | ForEach-Object { + # Process the tasks + if ($config.config.tasks.task) { + VM-Write-Log "INFO" "Processing scheduled tasks..." + $config.config.tasks.task | ForEach-Object { + try { $descName = $_.name $taskName = $_.value VM-Disable-Scheduled-Task -name $descName -value $taskName + } catch { + VM-Write-Log "ERROR" "Failed to disable task '$taskName'. Error: $($_.Exception.Message)" } } + } - # Process the registry items - if ($config.config."registry-items"."registry-item") { - $config.config."registry-items"."registry-item" | ForEach-Object { + # Process the registry items + if ($config.config."registry-items"."registry-item") { + VM-Write-Log "INFO" "Processing registry items..." + $config.config."registry-items"."registry-item" | ForEach-Object { + try { $name = $_.name $path = $_.path $value = $_.value $type = $_.type $data = $_.data VM-Update-Registry-Value -name $name -path $path -value $value -type $type -data $data + } catch { + VM-Write-Log "ERROR" "Failed to update registry item '$name'. Error: $($_.Exception.Message)" } } + } - # Process the path items - if ($config.config."path-items"."path-item") { - $config.config."path-items"."path-item" | ForEach-Object { + # Process the path items + if ($config.config."path-items"."path-item") { + VM-Write-Log "INFO" "Processing path items..." + $config.config."path-items"."path-item" | ForEach-Object { + try { $name = $_.name $type = $_.type $path = $_.path VM-Remove-Path -name $name -type $type -path $path + } catch { + VM-Write-Log "ERROR" "Failed to remove path item '$name'. Error: $($_.Exception.Message)" } } + } - # Process the locales - if ($config.config."locales"."locale") { - $config.config."locales"."locale" | ForEach-Object { + # Process the locales + if ($config.config."locales"."locale") { + VM-Write-Log "INFO" "Processing locales..." + $config.config."locales"."locale" | ForEach-Object { + try { $name = $_.name $lang = $_.lang VM-Install-Locale -name $name -lang $lang + } catch { + VM-Write-Log "ERROR" "Failed to install locale '$name'. Error: $($_.Exception.Message)" } } + } - # Process the custom items - if ($config.config."custom-items"."custom-item") { - $config.config."custom-items"."custom-item" | ForEach-Object { + # Process the custom items + if ($config.config."custom-items"."custom-item") { + VM-Write-Log "INFO" "Processing custom commands..." + $config.config."custom-items"."custom-item" | ForEach-Object { + try { $name = $_.name $cmds = @($_.cmd | ForEach-Object { $_.value }) VM-Execute-Custom-Command -name $name -cmds $cmds + } catch { + VM-Write-Log "ERROR" "Failed to execute custom commands for '$name'. Error: $($_.Exception.Message)" } } - } catch { - VM-Write-Log "ERROR" "An error occurred while applying config. Error: $_" } } diff --git a/packages/debloat.vm/debloat.vm.nuspec b/packages/debloat.vm/debloat.vm.nuspec index f50987607..c1caf1e69 100644 --- a/packages/debloat.vm/debloat.vm.nuspec +++ b/packages/debloat.vm/debloat.vm.nuspec @@ -6,7 +6,7 @@ Debloat and performance configurations for Windows OS Mandiant - + diff --git a/packages/debloat.vm/tools/win11.xml b/packages/debloat.vm/tools/win11.xml index 60c3e7473..f04fc271d 100644 --- a/packages/debloat.vm/tools/win11.xml +++ b/packages/debloat.vm/tools/win11.xml @@ -258,96 +258,96 @@ - - + + - + - + - - + + - + - - - + + + - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + - - - - - - - - - - - - - - - - - - - + + + + + + + + + + + + + + + + + + + diff --git a/packages/debloat.vm/tools/win11arm.xml b/packages/debloat.vm/tools/win11arm.xml index 60c3e7473..f04fc271d 100644 --- a/packages/debloat.vm/tools/win11arm.xml +++ b/packages/debloat.vm/tools/win11arm.xml @@ -258,96 +258,96 @@ - - + + - + - + - - + + - + - - - + + + - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + - - - - - - - - - - - - - - - - - - - + + + + + + + + + + + + + + + + + + +