From 9737d5c930103afc987b9e1b6c7fa8935a422f73 Mon Sep 17 00:00:00 2001 From: rbalsleyMSFT <53497092+rbalsleyMSFT@users.noreply.github.com> Date: Tue, 16 Dec 2025 21:18:42 -0800 Subject: [PATCH] Centralizes KB path cleanup into common cleanup module Removes duplicated KB path cleanup logic scattered across multiple locations in the build script and consolidates it into the shared cleanup module. Adds KBPath parameter to the cleanup function and handles removal of Windows/.NET cumulative update downloads when RemoveUpdates flag is set. Improves maintainability by eliminating redundant cleanup code and ensures consistent cleanup behavior across different build scenarios including standard builds, VHDX caching, and restore defaults operations. --- FFUDevelopment/BuildFFUVM.ps1 | 26 ++----------------- .../FFU.Common/FFU.Common.Cleanup.psm1 | 25 ++++++++++++------ .../FFUUI.Core/FFUUI.Core.Config.psm1 | 1 + 3 files changed, 20 insertions(+), 32 deletions(-) diff --git a/FFUDevelopment/BuildFFUVM.ps1 b/FFUDevelopment/BuildFFUVM.ps1 index 3c2cac2..f657ed1 100644 --- a/FFUDevelopment/BuildFFUVM.ps1 +++ b/FFUDevelopment/BuildFFUVM.ps1 @@ -3811,14 +3811,8 @@ function Get-FFUEnvironment { } #Run shared cleanup to avoid duplicated logic - Invoke-FFUPostBuildCleanup -RootPath $FFUDevelopmentPath -AppsPath $AppsPath -DriversPath $DriversFolder -FFUCapturePath $FFUCaptureLocation -CaptureISOPath $CaptureISO -DeployISOPath $DeployISO -AppsISOPath $AppsISO -RemoveCaptureISO:$CleanupCaptureISO -RemoveDeployISO:$CleanupDeployISO -RemoveAppsISO:$CleanupAppsISO -RemoveDrivers:$CleanupDrivers -RemoveFFU:$RemoveFFU -RemoveApps:$RemoveApps -RemoveUpdates:$RemoveUpdates + Invoke-FFUPostBuildCleanup -RootPath $FFUDevelopmentPath -AppsPath $AppsPath -DriversPath $DriversFolder -FFUCapturePath $FFUCaptureLocation -CaptureISOPath $CaptureISO -DeployISOPath $DeployISO -AppsISOPath $AppsISO -RemoveCaptureISO:$CleanupCaptureISO -RemoveDeployISO:$CleanupDeployISO -RemoveAppsISO:$CleanupAppsISO -RemoveDrivers:$CleanupDrivers -RemoveFFU:$RemoveFFU -RemoveApps:$RemoveApps -RemoveUpdates:$RemoveUpdates -KBPath:$KBPath - #Clean up $KBPath - If (Test-Path -Path $KBPath) { - WriteLog "Removing $KBPath" - Remove-Item -Path $KBPath -Recurse -Force -ErrorAction SilentlyContinue - WriteLog 'Removal complete' - } # Remove existing Apps.iso if (Test-Path -Path $AppsISO) { WriteLog "Removing $AppsISO" @@ -5873,8 +5867,6 @@ try { $cachedVHDXInfo.IncludedUpdates += ([VhdxCacheUpdateItem]::new($includedUpdate.Name)) } } - WriteLog "Removing $KBPath" - Remove-Item -Path $KBPath -Recurse -Force | Out-Null WriteLog 'Clean Up the WinSxS Folder' WriteLog 'This can take 10+ minutes depending on how old the media is and the size of the KB. Please be patient' Dism /Image:$WindowsPartition /Cleanup-Image /StartComponentCleanup /ResetBase | Out-Null @@ -6193,22 +6185,8 @@ If ($BuildUSBDrive) { Set-Progress -Percentage 99 -Message "Finalizing and cleaning up..." # Delegated post-build cleanup to common module -Invoke-FFUPostBuildCleanup -RootPath $FFUDevelopmentPath -AppsPath $AppsPath -DriversPath $Driversfolder -FFUCapturePath $FFUCaptureLocation -CaptureISOPath $CaptureISO -DeployISOPath $DeployISO -AppsISOPath $AppsISO -RemoveCaptureISO:$CleanupCaptureISO -RemoveDeployISO:$CleanupDeployISO -RemoveAppsISO:$CleanupAppsISO -RemoveDrivers:$CleanupDrivers -RemoveFFU:$RemoveFFU -RemoveApps:$RemoveApps -RemoveUpdates:$RemoveUpdates +Invoke-FFUPostBuildCleanup -RootPath $FFUDevelopmentPath -AppsPath $AppsPath -DriversPath $DriversFolder -FFUCapturePath $FFUCaptureLocation -CaptureISOPath $CaptureISO -DeployISOPath $DeployISO -AppsISOPath $AppsISO -RemoveCaptureISO:$CleanupCaptureISO -RemoveDeployISO:$CleanupDeployISO -RemoveAppsISO:$CleanupAppsISO -RemoveDrivers:$CleanupDrivers -RemoveFFU:$RemoveFFU -RemoveApps:$RemoveApps -RemoveUpdates:$RemoveUpdates -KBPath:$KBPath -# Remove KBPath for cached vhdx files -if ($AllowVHDXCaching) { - try { - If (Test-Path -Path $KBPath) { - WriteLog "Removing $KBPath" - Remove-Item -Path $KBPath -Recurse -Force -ErrorAction SilentlyContinue - WriteLog 'Removal complete' - } - } - catch { - Writelog "Removing $KBPath failed with error $_" - throw $_ - } -} # Remove WinGetWin32Apps.json so it is always rebuilt next run if (Test-Path -Path $wingetWin32jsonFile -PathType Leaf) { diff --git a/FFUDevelopment/FFU.Common/FFU.Common.Cleanup.psm1 b/FFUDevelopment/FFU.Common/FFU.Common.Cleanup.psm1 index bb4fe35..3fe3491 100644 --- a/FFUDevelopment/FFU.Common/FFU.Common.Cleanup.psm1 +++ b/FFUDevelopment/FFU.Common/FFU.Common.Cleanup.psm1 @@ -9,6 +9,7 @@ function Invoke-FFUPostBuildCleanup { [string]$CaptureISOPath, [string]$DeployISOPath, [string]$AppsISOPath, + [string]$KBPath, [bool]$RemoveCaptureISO = $false, [bool]$RemoveDeployISO = $false, [bool]$RemoveAppsISO = $false, @@ -20,7 +21,7 @@ function Invoke-FFUPostBuildCleanup { $originalProgressPreference = $ProgressPreference $ProgressPreference = 'SilentlyContinue' try { - WriteLog "CommonCleanup: Starting cleanup (CaptureISO=$RemoveCaptureISO DeployISO=$RemoveDeployISO AppsISO=$RemoveAppsISO Drivers=$RemoveDrivers FFU=$RemoveFFU Apps=$RemoveApps Updates=$RemoveUpdates)." + WriteLog "CommonCleanup: Starting cleanup (CaptureISO=$RemoveCaptureISO DeployISO=$RemoveDeployISO AppsISO=$RemoveAppsISO Drivers=$RemoveDrivers FFU=$RemoveFFU Apps=$RemoveApps Updates=$RemoveUpdates KBPath=$KBPath)." # Primary ISO paths (new naming/location) if ($RemoveCaptureISO -and -not [string]::IsNullOrWhiteSpace($CaptureISOPath) -and (Test-Path -LiteralPath $CaptureISOPath)) { @@ -94,15 +95,23 @@ function Invoke-FFUPostBuildCleanup { } } - if ($RemoveUpdates -and -not [string]::IsNullOrWhiteSpace($AppsPath) -and (Test-Path -LiteralPath $AppsPath)) { - $updateDirs = @('Defender', 'Edge', 'MSRT', 'OneDrive', '.NET', 'CU', 'Microcode') - foreach ($d in $updateDirs) { - $target = Join-Path $AppsPath $d - if (Test-Path -LiteralPath $target) { - WriteLog "CommonCleanup: Removing update folder $target" - try { Remove-Item -LiteralPath $target -Recurse -Force -ErrorAction Stop } catch { WriteLog "CommonCleanup: Failed removing $target : $($_.Exception.Message)" } + if ($RemoveUpdates) { + if (-not [string]::IsNullOrWhiteSpace($AppsPath) -and (Test-Path -LiteralPath $AppsPath)) { + # Remove per-run app update payloads stored under Apps + $appUpdateDirs = @('Defender', 'Edge', 'MSRT', 'OneDrive') + foreach ($d in $appUpdateDirs) { + $target = Join-Path $AppsPath $d + if (Test-Path -LiteralPath $target) { + WriteLog "CommonCleanup: Removing update folder $target" + try { Remove-Item -LiteralPath $target -Recurse -Force -ErrorAction Stop } catch { WriteLog "CommonCleanup: Failed removing $target : $($_.Exception.Message)" } + } } } + if (-not [string]::IsNullOrWhiteSpace($KBPath) -and (Test-Path -LiteralPath $KBPath)) { + # Remove Windows/.NET CU downloads stored under KB + WriteLog "CommonCleanup: Removing downloaded updates in $KBPath" + try { Remove-Item -LiteralPath $KBPath -Recurse -Force -ErrorAction Stop } catch { WriteLog "CommonCleanup: Failed removing $KBPath : $($_.Exception.Message)" } + } } WriteLog "CommonCleanup: Completed." diff --git a/FFUDevelopment/FFUUI.Core/FFUUI.Core.Config.psm1 b/FFUDevelopment/FFUUI.Core/FFUUI.Core.Config.psm1 index 3e62a0e..8a67c74 100644 --- a/FFUDevelopment/FFUUI.Core/FFUUI.Core.Config.psm1 +++ b/FFUDevelopment/FFUUI.Core/FFUUI.Core.Config.psm1 @@ -870,6 +870,7 @@ function Invoke-RestoreDefaults { -CaptureISOPath $captureISOPath ` -DeployISOPath $deployISOPath ` -AppsISOPath $appsISOPath ` + -KBPath (Join-Path $rootPath 'KB') ` -RemoveCaptureISO:$true ` -RemoveDeployISO:$true ` -RemoveAppsISO:$true `