From 832f08c18b05a50b49a19321051a6177853a0ce4 Mon Sep 17 00:00:00 2001 From: CodingWonders <101426328+CodingWonders@users.noreply.github.com> Date: Sat, 2 Mar 2024 14:00:45 +0100 Subject: [PATCH] Automatic conversion of ESD files Automatically convert the install.esd file from installation media into a WIM file that can be mounted, then delete the ESD file --- functions/public/Invoke-WPFGetIso.ps1 | 9 +++++-- functions/public/Invoke-WPFMicrowin.ps1 | 20 ++++++++++++++++ winutil.ps1 | 32 ++++++++++++++++++++++--- 3 files changed, 56 insertions(+), 5 deletions(-) diff --git a/functions/public/Invoke-WPFGetIso.ps1 b/functions/public/Invoke-WPFGetIso.ps1 index 6a2fa50ccd..95f866c22e 100644 --- a/functions/public/Invoke-WPFGetIso.ps1 +++ b/functions/public/Invoke-WPFGetIso.ps1 @@ -160,13 +160,18 @@ function Invoke-WPFGetIso { $wimFile = "$mountDir\sources\install.wim" Write-Host "Getting image information $wimFile" - if (-not (Test-Path -Path $wimFile -PathType Leaf)) + if ((-not (Test-Path -Path $wimFile -PathType Leaf)) -and (-not (Test-Path -Path $wimFile.Replace(".wim", ".esd").Trim() -PathType Leaf))) { - $msg = "Install.wim file doesn't exist in the image, this could happen if you use unofficial Windows images, or a Media creation tool, which creates a final image that can not be modified. Please don't use shady images from the internet, use only official images. Here are instructions how to download ISO images if the Microsoft website is not showing the link to download and ISO. https://www.techrepublic.com/article/how-to-download-a-windows-10-iso-file-without-using-the-media-creation-tool/" + $msg = "Neither install.wim nor install.esd exist in the image, this could happen if you use unofficial Windows images. Please don't use shady images from the internet, use only official images. Here are instructions how to download ISO images if the Microsoft website is not showing the link to download and ISO. https://www.techrepublic.com/article/how-to-download-a-windows-10-iso-file-without-using-the-media-creation-tool/" Write-Host $msg [System.Windows.MessageBox]::Show($msg, "Winutil", [System.Windows.MessageBoxButton]::OK, [System.Windows.MessageBoxImage]::Error) throw } + elseif ((-not (Test-Path -Path $wimFile -PathType Leaf)) -and (Test-Path -Path $wimFile.Replace(".wim", ".esd").Trim() -PathType Leaf)) + { + Write-Host "Install.esd found on the image. It needs to be converted to a WIM file in order to begin processing" + $wimFile = $wimFile.Replace(".wim", ".esd").Trim() + } $sync.MicrowinWindowsFlavors.Items.Clear() Get-WindowsImage -ImagePath $wimFile | ForEach-Object { $imageIdx = $_.ImageIndex diff --git a/functions/public/Invoke-WPFMicrowin.ps1 b/functions/public/Invoke-WPFMicrowin.ps1 index cb304dcc4a..8aa4d79a46 100644 --- a/functions/public/Invoke-WPFMicrowin.ps1 +++ b/functions/public/Invoke-WPFMicrowin.ps1 @@ -57,6 +57,26 @@ public class PowerManagement { $mountDir = $sync.MicrowinMountDir.Text $scratchDir = $sync.MicrowinScratchDir.Text + # 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..." + Export-WindowsImage -SourceImagePath $mountDir\sources\install.esd -SourceIndex $index -DestinationImagePath $mountDir\sources\install.wim -CompressionType "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 + $index = 1 + } + else + { + $msg = "The export process has failed and MicroWin processing cannot continue" + Write-Host "Failed to export the image" + [System.Windows.MessageBox]::Show($msg, "Winutil", [System.Windows.MessageBoxButton]::OK, [System.Windows.MessageBoxImage]::Error) + return + } + } + $imgVersion = (Get-WindowsImage -ImagePath $mountDir\sources\install.wim -Index $index).Version # Detect image version to avoid performing MicroWin processing on Windows 8 and earlier diff --git a/winutil.ps1 b/winutil.ps1 index a17695c66c..3b7702b269 100644 --- a/winutil.ps1 +++ b/winutil.ps1 @@ -10,6 +10,7 @@ Author : Chris Titus @christitustech Runspace Author: @DeveloperDurp GitHub : https://github.com/ChrisTitusTech + Version : 24.03.02 #> param ( [switch]$Debug, @@ -46,7 +47,7 @@ Add-Type -AssemblyName System.Windows.Forms # Variable to sync between runspaces $sync = [Hashtable]::Synchronized(@{}) $sync.PSScriptRoot = $PSScriptRoot -$sync.version = "24.02.22" +$sync.version = "24.03.02" $sync.configs = @{} $sync.ProcessRunning = $false @@ -3207,13 +3208,18 @@ function Invoke-WPFGetIso { $wimFile = "$mountDir\sources\install.wim" Write-Host "Getting image information $wimFile" - if (-not (Test-Path -Path $wimFile -PathType Leaf)) + if ((-not (Test-Path -Path $wimFile -PathType Leaf)) -and (-not (Test-Path -Path $wimFile.Replace(".wim", ".esd").Trim() -PathType Leaf))) { - $msg = "Install.wim file doesn't exist in the image, this could happen if you use unofficial Windows images, or a Media creation tool, which creates a final image that can not be modified. Please don't use shady images from the internet, use only official images. Here are instructions how to download ISO images if the Microsoft website is not showing the link to download and ISO. https://www.techrepublic.com/article/how-to-download-a-windows-10-iso-file-without-using-the-media-creation-tool/" + $msg = "Neither install.wim nor install.esd exist in the image, this could happen if you use unofficial Windows images. Please don't use shady images from the internet, use only official images. Here are instructions how to download ISO images if the Microsoft website is not showing the link to download and ISO. https://www.techrepublic.com/article/how-to-download-a-windows-10-iso-file-without-using-the-media-creation-tool/" Write-Host $msg [System.Windows.MessageBox]::Show($msg, "Winutil", [System.Windows.MessageBoxButton]::OK, [System.Windows.MessageBoxImage]::Error) throw } + elseif ((-not (Test-Path -Path $wimFile -PathType Leaf)) -and (Test-Path -Path $wimFile.Replace(".wim", ".esd").Trim() -PathType Leaf)) + { + Write-Host "Install.esd found on the image. It needs to be converted to a WIM file in order to begin processing" + $wimFile = $wimFile.Replace(".wim", ".esd").Trim() + } $sync.MicrowinWindowsFlavors.Items.Clear() Get-WindowsImage -ImagePath $wimFile | ForEach-Object { $imageIdx = $_.ImageIndex @@ -3441,6 +3447,26 @@ public class PowerManagement { $mountDir = $sync.MicrowinMountDir.Text $scratchDir = $sync.MicrowinScratchDir.Text + # 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..." + Export-WindowsImage -SourceImagePath $mountDir\sources\install.esd -SourceIndex $index -DestinationImagePath $mountDir\sources\install.wim -CompressionType "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 + $index = 1 + } + else + { + $msg = "The export process has failed and MicroWin processing cannot continue" + Write-Host "Failed to export the image" + [System.Windows.MessageBox]::Show($msg, "Winutil", [System.Windows.MessageBoxButton]::OK, [System.Windows.MessageBoxImage]::Error) + return + } + } + $imgVersion = (Get-WindowsImage -ImagePath $mountDir\sources\install.wim -Index $index).Version # Detect image version to avoid performing MicroWin processing on Windows 8 and earlier