Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- Replaced Code Block #1 (lines 191-198)
- try {
- Write-Host "Mounting Iso. Please wait."
- $mountedISO = Mount-DiskImage -PassThru "$filePath"
- Write-Host "Done mounting Iso `"$($mountedISO.ImagePath)`""
- # Wait for Windows to assign drive letter
- Start-Sleep -Seconds 3
- # Try Get-Volume first (standard method)
- $driveLetter = (Get-Volume -DiskImage $mountedISO -ErrorAction SilentlyContinue).DriveLetter
- # Fallback: If Get-Volume fails, scan all drives for install.wim/esd
- if ([string]::IsNullOrEmpty($driveLetter)) {
- Write-Host "Get-Volume failed, using fallback drive detection..." -ForegroundColor Yellow
- # Scan all drive letters for Windows installation media
- $drives = Get-PSDrive -PSProvider FileSystem | Where-Object { $_.Root -match '^[A-Z]:\\$' }
- foreach ($drive in $drives) {
- $testWim = Join-Path $drive.Root "sources\install.wim"
- $testEsd = Join-Path $drive.Root "sources\install.esd"
- if ((Test-Path $testWim) -or (Test-Path $testEsd)) {
- # Verify it's a CD-ROM/ISO drive (DriveType 5)
- $volumeInfo = Get-CimInstance -ClassName Win32_Volume -Filter "DriveLetter='$($drive.Name):'" -ErrorAction SilentlyContinue
- if ($volumeInfo -and $volumeInfo.DriveType -eq 5) {
- $driveLetter = $drive.Name
- Write-Host "Found Windows ISO at drive $driveLetter" -ForegroundColor Green
- break
- }
- }
- }
- # Final check: If still not found, try Win32_LogicalDisk as last resort
- if ([string]::IsNullOrEmpty($driveLetter)) {
- $cdDrives = Get-CimInstance -ClassName Win32_LogicalDisk -Filter "DriveType=5" -ErrorAction SilentlyContinue
- foreach ($cdDrive in $cdDrives) {
- $testDrive = $cdDrive.DeviceID.TrimEnd(':')
- $testWim = "${testDrive}:\sources\install.wim"
- $testEsd = "${testDrive}:\sources\install.esd"
- if ((Test-Path $testWim) -or (Test-Path $testEsd)) {
- $driveLetter = $testDrive
- Write-Host "Found Windows ISO at drive $driveLetter (via LogicalDisk)" -ForegroundColor Green
- break
- }
- }
- }
- # If all methods failed, throw error
- if ([string]::IsNullOrEmpty($driveLetter)) {
- throw "Could not detect drive letter for mounted ISO after trying all detection methods."
- }
- }
- Write-Host "Iso mounted to '$driveLetter'"
- } catch {
- Replaced Code Block #2 (line 317)
- # Dismount the ISO - use DiskImage path instead of Get-Volume since Get-Volume may fail
- try {
- if ($mountedISO) {
- Dismount-DiskImage -ImagePath $mountedISO.ImagePath -ErrorAction Stop
- Write-Host "Successfully dismounted ISO"
- } else {
- # Fallback: try to dismount by finding the DiskImage for this drive letter
- $diskImage = Get-DiskImage -DevicePath "\\.\CDROM*" -ErrorAction SilentlyContinue |
- Where-Object { $_.Attached -eq $true }
- if ($diskImage) {
- $diskImage | Dismount-DiskImage -ErrorAction Stop
- Write-Host "Successfully dismounted ISO (fallback method)"
- } else {
- Write-Host "Could not find mounted ISO to dismount. You may need to dismount manually." -ForegroundColor Yellow
- }
- }
- } catch {
- Write-Host "Warning: Could not dismount ISO automatically. Error: $($_.Exception.Message)" -ForegroundColor Yellow
- Write-Host "You may need to manually dismount the ISO from File Explorer" -ForegroundColor Yellow
- }
Advertisement
Add Comment
Please, Sign In to add comment