Skip to content

Commit

Permalink
Extract function to remove available windows features
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
jpalermo committed Sep 12, 2023
1 parent 54292a2 commit 9787719
Show file tree
Hide file tree
Showing 4 changed files with 15 additions and 4 deletions.
2 changes: 1 addition & 1 deletion modules/BOSH.Disk/BOSH.Disk.psd1
Original file line number Diff line number Diff line change
Expand Up @@ -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 = @()
Expand Down
15 changes: 12 additions & 3 deletions modules/BOSH.Disk/BOSH.Disk.psm1
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand All @@ -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
Expand Down
1 change: 1 addition & 0 deletions stemcell-automation/AutomationHelpers.Tests.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -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") }

Expand Down
1 change: 1 addition & 0 deletions stemcell-automation/AutomationHelpers.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ function Setup()
Install-SecurityPoliciesAndRegistries
Enable-SSHD
InstallCFFeatures
Remove-Available-Windows-Features

try { Install-WUCerts }
catch [Exception]
Expand Down

0 comments on commit 9787719

Please sign in to comment.