Guest User

bugfix

a guest
Nov 15th, 2025
27
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.92 KB | None | 0 0
  1. Replaced Code Block #1 (lines 191-198)
  2. try {
  3. Write-Host "Mounting Iso. Please wait."
  4. $mountedISO = Mount-DiskImage -PassThru "$filePath"
  5. Write-Host "Done mounting Iso `"$($mountedISO.ImagePath)`""
  6.  
  7. # Wait for Windows to assign drive letter
  8. Start-Sleep -Seconds 3
  9.  
  10. # Try Get-Volume first (standard method)
  11. $driveLetter = (Get-Volume -DiskImage $mountedISO -ErrorAction SilentlyContinue).DriveLetter
  12.  
  13. # Fallback: If Get-Volume fails, scan all drives for install.wim/esd
  14. if ([string]::IsNullOrEmpty($driveLetter)) {
  15. Write-Host "Get-Volume failed, using fallback drive detection..." -ForegroundColor Yellow
  16.  
  17. # Scan all drive letters for Windows installation media
  18. $drives = Get-PSDrive -PSProvider FileSystem | Where-Object { $_.Root -match '^[A-Z]:\\$' }
  19.  
  20. foreach ($drive in $drives) {
  21. $testWim = Join-Path $drive.Root "sources\install.wim"
  22. $testEsd = Join-Path $drive.Root "sources\install.esd"
  23.  
  24. if ((Test-Path $testWim) -or (Test-Path $testEsd)) {
  25. # Verify it's a CD-ROM/ISO drive (DriveType 5)
  26. $volumeInfo = Get-CimInstance -ClassName Win32_Volume -Filter "DriveLetter='$($drive.Name):'" -ErrorAction SilentlyContinue
  27.  
  28. if ($volumeInfo -and $volumeInfo.DriveType -eq 5) {
  29. $driveLetter = $drive.Name
  30. Write-Host "Found Windows ISO at drive $driveLetter" -ForegroundColor Green
  31. break
  32. }
  33. }
  34. }
  35.  
  36. # Final check: If still not found, try Win32_LogicalDisk as last resort
  37. if ([string]::IsNullOrEmpty($driveLetter)) {
  38. $cdDrives = Get-CimInstance -ClassName Win32_LogicalDisk -Filter "DriveType=5" -ErrorAction SilentlyContinue
  39.  
  40. foreach ($cdDrive in $cdDrives) {
  41. $testDrive = $cdDrive.DeviceID.TrimEnd(':')
  42. $testWim = "${testDrive}:\sources\install.wim"
  43. $testEsd = "${testDrive}:\sources\install.esd"
  44.  
  45. if ((Test-Path $testWim) -or (Test-Path $testEsd)) {
  46. $driveLetter = $testDrive
  47. Write-Host "Found Windows ISO at drive $driveLetter (via LogicalDisk)" -ForegroundColor Green
  48. break
  49. }
  50. }
  51. }
  52.  
  53. # If all methods failed, throw error
  54. if ([string]::IsNullOrEmpty($driveLetter)) {
  55. throw "Could not detect drive letter for mounted ISO after trying all detection methods."
  56. }
  57. }
  58.  
  59. Write-Host "Iso mounted to '$driveLetter'"
  60. } catch {
  61.  
  62.  
  63. Replaced Code Block #2 (line 317)
  64. # Dismount the ISO - use DiskImage path instead of Get-Volume since Get-Volume may fail
  65. try {
  66. if ($mountedISO) {
  67. Dismount-DiskImage -ImagePath $mountedISO.ImagePath -ErrorAction Stop
  68. Write-Host "Successfully dismounted ISO"
  69. } else {
  70. # Fallback: try to dismount by finding the DiskImage for this drive letter
  71. $diskImage = Get-DiskImage -DevicePath "\\.\CDROM*" -ErrorAction SilentlyContinue |
  72. Where-Object { $_.Attached -eq $true }
  73.  
  74. if ($diskImage) {
  75. $diskImage | Dismount-DiskImage -ErrorAction Stop
  76. Write-Host "Successfully dismounted ISO (fallback method)"
  77. } else {
  78. Write-Host "Could not find mounted ISO to dismount. You may need to dismount manually." -ForegroundColor Yellow
  79. }
  80. }
  81. } catch {
  82. Write-Host "Warning: Could not dismount ISO automatically. Error: $($_.Exception.Message)" -ForegroundColor Yellow
  83. Write-Host "You may need to manually dismount the ISO from File Explorer" -ForegroundColor Yellow
  84. }
Advertisement
Add Comment
Please, Sign In to add comment