From 9787719a11dc7ae48f7ff42835bc57ea9266b0fa Mon Sep 17 00:00:00 2001 From: Joseph Palermo Date: Mon, 11 Sep 2023 19:40:54 -0700 Subject: [PATCH] Extract function to remove available windows features We suspect this sometimes triggers the need for a VM restart so we want to try running it before the final restart to avoid a situation where Dism.exe is unable to run successfully. --- modules/BOSH.Disk/BOSH.Disk.psd1 | 2 +- modules/BOSH.Disk/BOSH.Disk.psm1 | 15 ++++++++++++--- stemcell-automation/AutomationHelpers.Tests.ps1 | 1 + stemcell-automation/AutomationHelpers.ps1 | 1 + 4 files changed, 15 insertions(+), 4 deletions(-) diff --git a/modules/BOSH.Disk/BOSH.Disk.psd1 b/modules/BOSH.Disk/BOSH.Disk.psd1 index 4ba773fe..cb1b5898 100644 --- a/modules/BOSH.Disk/BOSH.Disk.psd1 +++ b/modules/BOSH.Disk/BOSH.Disk.psd1 @@ -7,7 +7,7 @@ Copyright = '(c) 2017 BOSH' Description = 'Commands for Disk Utilities on a BOSH deployed vm' PowerShellVersion = '4.0' RequiredModules = @('BOSH.Utils') -FunctionsToExport = @('Compress-Disk','Optimize-Disk') +FunctionsToExport = @('Compress-Disk','Optimize-Disk', 'Remove-Available-Windows-Features') CmdletsToExport = @() VariablesToExport = '*' AliasesToExport = @() diff --git a/modules/BOSH.Disk/BOSH.Disk.psm1 b/modules/BOSH.Disk/BOSH.Disk.psm1 index cf9b3e10..4835abcd 100644 --- a/modules/BOSH.Disk/BOSH.Disk.psm1 +++ b/modules/BOSH.Disk/BOSH.Disk.psm1 @@ -16,11 +16,10 @@ function Compress-Disk { function Optimize-Disk { Write-Log "Starting to clean disk" - Get-WindowsFeature | - ? { $_.InstallState -eq 'Available' } | - Uninstall-WindowsFeature -Remove + Remove-Available-Windows-Features # Cleanup WinSxS folder: https://technet.microsoft.com/en-us/library/dn251565.aspx + # /LogLevel default is 3 Write-Log "Running 'Dism.exe /online /LogLevel:4 /Cleanup-Image /StartComponentCleanup /ResetBase'" Dism.exe /online /LogLevel:4 /Cleanup-Image /StartComponentCleanup /ResetBase if ($LASTEXITCODE -ne 0) { @@ -38,6 +37,16 @@ function Optimize-Disk { Write-Log "Finished clean disk" } +function Remove-Available-Windows-Features { + Write-Log "Starting to remove 'Available' Windows Features" + + Get-WindowsFeature | + ? { $_.InstallState -eq 'Available' } | + Uninstall-WindowsFeature -Remove + + Write-Log "Finished removing 'Available' Windows Features" +} + function DefragDisk { # First - get the volumes via WMI $volumes = gwmi win32_volume diff --git a/stemcell-automation/AutomationHelpers.Tests.ps1 b/stemcell-automation/AutomationHelpers.Tests.ps1 index ecae6f33..0f7284b6 100644 --- a/stemcell-automation/AutomationHelpers.Tests.ps1 +++ b/stemcell-automation/AutomationHelpers.Tests.ps1 @@ -24,6 +24,7 @@ Describe "Setup" { Mock Install-WUCerts { $provisionerCalls.Add("Install-WUCerts") } Mock InstallCFFeatures { $provisionerCalls.Add("InstallCFFeatures") } + Mock Remove-Available-Windows-Features { } Mock Create-VersionFile { $provisionerCalls.Add("Create-VersionFile") } Mock Restart-Computer { $provisionerCalls.Add("Restart-Computer") } diff --git a/stemcell-automation/AutomationHelpers.ps1 b/stemcell-automation/AutomationHelpers.ps1 index dabfadf5..e487c37c 100644 --- a/stemcell-automation/AutomationHelpers.ps1 +++ b/stemcell-automation/AutomationHelpers.ps1 @@ -36,6 +36,7 @@ function Setup() Install-SecurityPoliciesAndRegistries Enable-SSHD InstallCFFeatures + Remove-Available-Windows-Features try { Install-WUCerts } catch [Exception]