diff --git a/modules/BOSH.Disk/BOSH.Disk.psm1 b/modules/BOSH.Disk/BOSH.Disk.psm1 index d39ed8d0..cf9b3e10 100644 --- a/modules/BOSH.Disk/BOSH.Disk.psm1 +++ b/modules/BOSH.Disk/BOSH.Disk.psm1 @@ -19,31 +19,21 @@ function Optimize-Disk { Get-WindowsFeature | ? { $_.InstallState -eq 'Available' } | Uninstall-WindowsFeature -Remove - $DismStatus = 0 - do { - # 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 - $DismStatus = $LASTEXITCODE - if ($DismStatus -ne 0) { - Write-Log "Error: Running 'Dism.exe /online /LogLevel:4 /Cleanup-Image /StartComponentCleanup /ResetBase'" - Write-Log "Sleeping for 30 seconds then retrying" - Start-Sleep -Seconds 30 - } - } while ($DismStatus -ne 0) + # Cleanup WinSxS folder: https://technet.microsoft.com/en-us/library/dn251565.aspx + 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) { + Write-Log "Error: Running 'Dism.exe /online /LogLevel:4 /Cleanup-Image /StartComponentCleanup /ResetBase'" + Throw "Running 'Dism.exe /online /LogLevel:4 /Cleanup-Image /StartComponentCleanup /ResetBase' failed" + } - do { - Write-Log "Running 'Dism.exe /online /LogLevel:4 /Cleanup-Image /SPSuperseded'" - Dism.exe /online /LogLevel:4 /Cleanup-Image /SPSuperseded - $DismStatus = $LASTEXITCODE - if ($DismStatus -ne 0) { - Write-Log "Error: Running 'Dism.exe /online /LogLevel:4 /Cleanup-Image /SPSuperseded'" - Write-Log "Sleeping for 30 seconds then retrying" - Start-Sleep -Seconds 30 - } - } while ($DismStatus -ne 0) + Write-Log "Running 'Dism.exe /online /LogLevel:4 /Cleanup-Image /SPSuperseded'" + Dism.exe /online /LogLevel:4 /Cleanup-Image /SPSuperseded + if ($LASTEXITCODE -ne 0) { + Write-Log "Error: Running 'Dism.exe /online /LogLevel:4 /Cleanup-Image /SPSuperseded'" + Throw "Running 'Dism.exe /online /LogLevel:4 /Cleanup-Image /SPSuperseded' failed" + } Write-Log "Finished clean disk" } diff --git a/stemcell-automation/AutomationHelpers.ps1 b/stemcell-automation/AutomationHelpers.ps1 index d8ccf621..dabfadf5 100644 --- a/stemcell-automation/AutomationHelpers.ps1 +++ b/stemcell-automation/AutomationHelpers.ps1 @@ -61,11 +61,11 @@ function PostReboot [string]$Owner = "", [switch]$SkipRandomPassword ) - RunQuickerDism + RunQuickerDism -IgnoreErrors $True InstallCFCell - RunQuickerDism + RunQuickerDism -IgnoreErrors $True CleanUpVM - RunQuickerDism + RunQuickerDism -IgnoreErrors $True SysprepVM -Organization $Organization -Owner $Owner -SkipRandomPassword $SkipRandomPassword RunQuickerDism } @@ -76,21 +76,18 @@ function RunQuickerDism { ) Write-Log "Running Quicker Dism (that is, not executing with /ResetBase)" - $DismStatus = 0 - do { - # /LogLevel default is 3 - Write-Log "Running 'Dism.exe /online /LogLevel:4 /Cleanup-Image /StartComponentCleanup'" - Dism.exe /online /LogLevel:4 /Cleanup-Image /StartComponentCleanup - $DismStatus = $LASTEXITCODE - if ($DismStatus -ne 0) { - Write-Log "Error: Running 'Dism.exe /online /LogLevel:4 /Cleanup-Image /StartComponentCleanup'" - if ($IgnoreErrors) { - break - } - Write-Log "Sleeping for 30 seconds then retrying" - Start-Sleep -Seconds 30 + # /LogLevel default is 3 + Write-Log "Running 'Dism.exe /online /LogLevel:4 /Cleanup-Image /StartComponentCleanup'" + Dism.exe /online /LogLevel:4 /Cleanup-Image /StartComponentCleanup + if ($LASTEXITCODE -ne 0) { + Write-Log "Error: Running 'Dism.exe /online /LogLevel:4 /Cleanup-Image /StartComponentCleanup'" + if ($IgnoreErrors) { + Write-Log "Ignoring: Error: Running 'Dism.exe /online /LogLevel:4 /Cleanup-Image /StartComponentCleanup'" } - } while ($DismStatus -ne 0) + else { + Throw "Running 'Dism.exe /online /LogLevel:4 /Cleanup-Image /StartComponentCleanup' failed" + } + } } function CopyPSModules