Skip to content

Commit 2576ab9

Browse files
committed
Keep trying to commit and unmount images
1 parent 853a142 commit 2576ab9

1 file changed

Lines changed: 75 additions & 2 deletions

File tree

functions/microwin/Invoke-Microwin.ps1

Lines changed: 75 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -430,9 +430,45 @@ public class PowerManagement {
430430
Write-Host "Cleaning up image..."
431431
dism /English /image:$scratchDir /Cleanup-Image /StartComponentCleanup /ResetBase
432432
Write-Host "Cleanup complete."
433+
$committed = $false
434+
$unmounted = $false
435+
436+
Write-Host "Saving image..."
437+
438+
try {
439+
Save-WindowsImage -Path "$scratchDir"
440+
$committed = $true
441+
} catch {
442+
do {
443+
# we'll prevent stuff inside this loop from throwing exceptions and breaking from the loop.
444+
try {
445+
Save-WindowsImage -Path "$scratchDir"
446+
$committed = $true
447+
} catch {
448+
Write-Host "Commit operation unsuccessful. Trying again after 3 seconds..."
449+
Start-Sleep -Seconds 3
450+
}
451+
} until ($committed)
452+
}
433453

434454
Write-Host "Unmounting image..."
435-
Dismount-WindowsImage -Path "$scratchDir" -Save
455+
456+
try {
457+
# because we've already saved the changes earlier, we can safely discard
458+
Dismount-WindowsImage -Discard -Path "$scratchDir"
459+
$unmounted = $true
460+
} catch {
461+
do {
462+
# we'll prevent stuff inside this loop from throwing exceptions and breaking from the loop.
463+
try {
464+
Dismount-WindowsImage -Discard -Path "$scratchDir"
465+
$unmounted = $true
466+
} catch {
467+
Write-Host "Unmount operation unsuccessful. Trying again after 3 seconds..."
468+
Start-Sleep -Seconds 3
469+
}
470+
} until ($unmounted)
471+
}
436472
}
437473

438474
try {
@@ -518,8 +554,45 @@ public class PowerManagement {
518554
reg unload HKLM\zSOFTWARE
519555
reg unload HKLM\zSYSTEM
520556

557+
$committed = $false
558+
$unmounted = $false
559+
560+
Write-Host "Saving image..."
561+
562+
try {
563+
Save-WindowsImage -Path "$scratchDir"
564+
$committed = $true
565+
} catch {
566+
do {
567+
# we'll prevent stuff inside this loop from throwing exceptions and breaking from the loop.
568+
try {
569+
Save-WindowsImage -Path "$scratchDir"
570+
$committed = $true
571+
} catch {
572+
Write-Host "Commit operation unsuccessful. Trying again after 3 seconds..."
573+
Start-Sleep -Seconds 3
574+
}
575+
} until ($committed)
576+
}
577+
521578
Write-Host "Unmounting image..."
522-
Dismount-WindowsImage -Path "$scratchDir" -Save
579+
580+
try {
581+
# because we've already saved the changes earlier, we can safely discard
582+
Dismount-WindowsImage -Discard -Path "$scratchDir"
583+
$unmounted = $true
584+
} catch {
585+
do {
586+
# we'll prevent stuff inside this loop from throwing exceptions and breaking from the loop.
587+
try {
588+
Dismount-WindowsImage -Discard -Path "$scratchDir"
589+
$unmounted = $true
590+
} catch {
591+
Write-Host "Unmount operation unsuccessful. Trying again after 3 seconds..."
592+
Start-Sleep -Seconds 3
593+
}
594+
} until ($unmounted)
595+
}
523596

524597
Write-Host "Creating ISO image"
525598

0 commit comments

Comments
 (0)