Fix: Add DISM fallback for Export-WindowsImage errors (#3224)

This commit is contained in:
Nilesh Mukherjee 2025-04-02 20:53:28 +05:30
parent 84a18503b8
commit 4ec23f76a8

View File

@ -63,7 +63,14 @@ public class PowerManagement {
# Detect if the Windows image is an ESD file and convert it to WIM
if (-not (Test-Path -Path "$mountDir\sources\install.wim" -PathType Leaf) -and (Test-Path -Path "$mountDir\sources\install.esd" -PathType Leaf)) {
Write-Host "Exporting Windows image to a WIM file, keeping the index we want to work on. This can take several minutes, depending on the performance of your computer..."
try {
# Try ps command first
Export-WindowsImage -SourceImagePath $mountDir\sources\install.esd -SourceIndex $index -DestinationImagePath $mountDir\sources\install.wim -CompressionType "Max"
} catch {
# Fallback to DISM command
Write-Host "Export-WindowsImage failed, using DISM instead..."
dism /Export-Image /SourceImageFile:$mountDir\sources\install.esd /SourceIndex:$index /DestinationImageFile:$mountDir\sources\install.wim /Compress:max
}
if ($?) {
Remove-Item -Path "$mountDir\sources\install.esd" -Force
# Since we've already exported the image index we wanted, switch to the first one
@ -360,7 +367,14 @@ public class PowerManagement {
try {
Write-Host "Exporting image into $mountDir\sources\install2.wim"
try {
# Try ps command first
Export-WindowsImage -SourceImagePath "$mountDir\sources\install.wim" -SourceIndex $index -DestinationImagePath "$mountDir\sources\install2.wim" -CompressionType "Max"
} catch {
# Fallback to DISM command
Write-Host "Export-WindowsImage failed, using DISM instead..."
dism /Export-Image /SourceImageFile:"$mountDir\sources\install.wim" /SourceIndex:$index /DestinationImageFile:"$mountDir\sources\install2.wim" /Compress:max
}
Write-Host "Remove old '$mountDir\sources\install.wim' and rename $mountDir\sources\install2.wim"
Remove-Item "$mountDir\sources\install.wim"
Rename-Item "$mountDir\sources\install2.wim" "$mountDir\sources\install.wim"