Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Windows 11 ISO mount issue] Mount ISOs using PowerISO #378

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
68 changes: 35 additions & 33 deletions CopyFilesToVM.ps1
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
$params = @{
VMName = "GPUPV"
pisoPath = "C:\Program Files\PowerISO\piso.exe"
SourcePath = "C:\Users\james\Downloads\Win11_English_x64.iso"
Edition = 6
VhdFormat = "VHDX"
Expand Down Expand Up @@ -27,40 +28,41 @@ function Is-Administrator
(New-Object Security.Principal.WindowsPrincipal $CurrentUser).IsInRole([Security.Principal.WindowsBuiltinRole]::Administrator)
}

Function Dismount-ISO {

Function PowerISO-Mount-ISO {
param (
[string]$SourcePath
)
$disk = Get-Volume | Where-Object {$_.DriveType -eq "CD-ROM"} | select *
Foreach ($d in $disk) {
Dismount-DiskImage -ImagePath $sourcePath | Out-Null
)
#PowerISO command line docs: https://poweriso.com/tutorials/command-line-argus.htm
#We will use Power ISO exclusively, and we will use only 1 virtual drive (Not strictly necessary, just to keep script simple and fast)
Start-Process -NoNewWindow -FilePath $params.pisoPath -ArgumentList "unmount all"
Start-Process -NoNewWindow -FilePath $params.pisoPath -ArgumentList "setvdnum 1"

$pisoPath=$params.pisoPath
$listvdOutput = & "$pisoPath" "listvd"
# Find the first drive that is free (contains "<No media>")
$freeDrive = $null
foreach ($line in $listvdOutput) {
if ($line -match "Drive \[([A-Z]):\] <No media>") {
$freeDrive = $matches[1]
}
}

if (-not $freeDrive) {
return
}

# Mount the ISO to the free drive
$mountCommand = & "$pisoPath" mount "$SourcePath" "${freeDrive}:"
if ($mountCommand -like "*successfully*") {
Write-Output "${freeDrive}"
} else {
Write-Output "Failed to mount the ISO."
}
}

Function Mount-ISOReliable {
param (
[string]$SourcePath
)
$mountResult = Mount-DiskImage -ImagePath $SourcePath
$delay = 0
Do {
if ($delay -gt 15) {
Function Get-NewDriveLetter {
$UsedDriveLetters = ((Get-Volume).DriveLetter) -join ""
Do {
$DriveLetter = (65..90)| Get-Random | % {[char]$_}
}
Until (!$UsedDriveLetters.Contains("$DriveLetter"))
$DriveLetter
}
$DriveLetter = "$(Get-NewDriveLetter)" + ":"
Get-WmiObject -Class Win32_volume | Where-Object {$_.Label -eq "CCCOMA_X64FRE_EN-US_DV9"} | Set-WmiInstance -Arguments @{DriveLetter="$driveletter"}
}
Start-Sleep -s 1
$delay++
}
Until (($mountResult | Get-Volume).DriveLetter -ne $NULL)
($mountResult | Get-Volume).DriveLetter
Function Dismount-ISO {
Start-Process -NoNewWindow -FilePath $params.pisoPath -ArgumentList "unmount all"
}


Expand Down Expand Up @@ -116,11 +118,11 @@ if (!(test-path $params.SourcePath)) {
$ExitReason += "ISO Path Invalid. Please enter a valid ISO Path in the SourcePath section of Params."
}
else {
$ISODriveLetter = Mount-ISOReliable -SourcePath $params.SourcePath
$ISODriveLetter = PowerISO-Mount-ISO -SourcePath $params.SourcePath
if (!(Test-Path $("$ISODriveLetter"+":\Sources\install.wim"))) {
$ExitReason += "This ISO is invalid, please check readme for ISO downloading instructions."
}
Dismount-ISO -SourcePath $params.SourcePath
Dismount-ISO
}
if ($params.Username -eq $params.VMName ) {
$ExitReason += "Username cannot be the same as VMName."
Expand Down Expand Up @@ -2622,7 +2624,7 @@ You can use the fields below to configure the VHD or VHDX that you want to creat
}

# Close out the transcript and tell the user we're done.
Dismount-ISO -SourcePath $ISOPath
Dismount-ISO
Write-W2VInfo "Done."
if ($transcripting)
{
Expand Down Expand Up @@ -4357,7 +4359,7 @@ param(
[string]$autologon
)
$VHDPath = ConcatenateVHDPath -VHDPath $VHDPath -VMName $VMName
$DriveLetter = Mount-ISOReliable -SourcePath $SourcePath
$DriveLetter = PowerISO-Mount-ISO -SourcePath $params.SourcePath

if ($(Get-VM -Name $VMName -ErrorAction SilentlyContinue) -ne $NULL) {
SmartExit -ExitReason "Virtual Machine already exists with name $VMName, please delete existing VM or change VMName"
Expand Down