-
Notifications
You must be signed in to change notification settings - Fork 79
Description
Hi,
I've no idea if this of interest at all, or whether it was already discussed. Apologies!
I built a small set of scripts to build Linux and Windows Hyper-V VMs too, but only in a way suitable for my environment. I originally built Windows VMs from WDS but this takes ages so I hunted for ways to speed up the install, and found this fantastic repo. Congrats!
I was curious about Convert-WindowsImage.ps1 but it seems to have some strict requirements about the PowerShell environment it can run in. I fiddle in VS Code and couldn't figure out how to use it but I don't really know what I'm doing in PoSh.
I took at look at what Convert-WindowsImage.ps1 does, couldn't figure out various bits, but came up with some terrible hackery -
# Mount the source ISO
Mount-DiskImage -ImagePath $sourceISO
$mountedISO = Get-DiskImage -ImagePath $sourceISO | Get-Volume
$mountedDriveLetter = $mountedISO.DriveLetter
if (-not $mountedDriveLetter) {
Dismount-VHD -Path $outputVHDX
throw "Failed to get the drive letter of the mounted ISO."
}
$mountedDriveLetterPath = $mountedDriveLetter + ":\"
# Create the VHDX
$SizeBytes = "60GB"
New-VHD -Path $outputVHDX -Dynamic -SizeBytes $SizeBytes
Mount-VHD -Path $outputVHDX
$vhd = Get-Disk | Where-Object { $_.Location -eq "$outputVHDX" }
# Initialize, partition, and format the VHDX
Initialize-Disk -Number $vhd.Number -PartitionStyle GPT
# Create the EFI System Partition
$systemPartition = New-Partition -DiskNumber $vhd.Number -Size 100MB -GptType "{c12a7328-f81f-11d2-ba4b-00a0c93ec93b}" -AssignDriveLetter
Format-Volume -Partition $systemPartition -FileSystem FAT32 -NewFileSystemLabel "System"
# Create the MSR Partition
New-Partition -DiskNumber $vhd.Number -Size 16MB -GptType "{e3c9e316-0b5c-4db8-817d-f92df00215ae}"
# Create the Windows Partition
$windowsPartition = New-Partition -DiskNumber $vhd.Number -UseMaximumSize -GptType "{ebd0a0a2-b9e5-4433-87c0-68b6b72699c7}" -AssignDriveLetter
Format-Volume -Partition $windowsPartition -FileSystem NTFS -NewFileSystemLabel "Windows"
# Create directory structure on EFI System Partition
New-Item -Path "$($systemPartition.DriveLetter):\EFI" -ItemType Directory -Force
$mapping = @{
"25s" = "*Standard*Desktop*"
"25d" = "*Datacenter*Desktop*"
"25sc" = "* Standard *"
"25dc" = "* Datacenter *"
}
$installWimPath = $mountedDriveLetterPath + "sources\install.wim"
$imageInfo = Get-WindowsImage -ImagePath $installWimPath
$imageInfo = $imageInfo | Sort-Object ImageName
#lookup image index of $os using mapping
$imageIndex = $null
$pattern = $mapping[$os]
foreach ($image in $imageInfo) {
if ($image.ImageName -like $pattern) {
$imageIndex = $image.ImageIndex
$imageName=$image.ImageName
break
}
}
if ($null -eq $imageIndex) {
Dismount-VHD -Path $outputVHDX
Dismount-DiskImage -ImagePath $sourceISO
Remove-Item -Path $outputVHDX -Force
throw "No matching image found for $os."
}
Write-Host "dism apply, will be slow - $imageName"
dism /Apply-Image /ImageFile:${mountedDriveLetterPath}sources\install.wim /Index:$($image.ImageIndex) /ApplyDir:$($windowsPartition.DriveLetter):\ /CheckIntegrity
#prep boot files
bcdboot "$($windowsPartition.DriveLetter):\Windows" /s "$($systemPartition.DriveLetter):" /f ALL
# Copy unattend file to Windows partition
Copy-Item -Path $UnattendPath -Destination "$($windowsPartition.DriveLetter):\Windows\Panther\Unattend.xml" -Force
# Dismount the VHDX and the ISO
Dismount-VHD -Path $outputVHDX
Dismount-DiskImage -ImagePath $sourceISO
My only goal was to be able to create a VHDX from an ISO, in VS Code. Obviously, my bodging doesn't do everything Convert-WindowsImage.ps1 does but I can't even figure out what most of that large script does. I really don't know what I'm doing in PoSh so might have done this for nothing.
Again, I have no idea if useful in any way. It's just a start of some ideas, has terrible error handling and other bits so far, and is not meant with any disrespect to the convert script, or this repo.
Cheers.