[APORTE] [PowerShell] Montar y desmontar una unidad virtual de CD/DVD/BD o VHD/VHDX/VHDS
(1/1)
Eleкtro:
El siguiente script, desarrollado en PowerShell, sirve para montar y desmontar una unidad virtual de CD/DVD/BD con el archivo de imagen ISO especificado. Opcionalmente se le podría dar el mismo uso para montar discos virtuales en formato .vhd, .vhdx y .vhds
Desarrollé este script para evitar tener que recurrir a software de terceros (ej. ImDisk) en escenarios donde necesito automatizar el montaje de archivos ISO, sin interacción humana y sin dependencias externas.
El script no tiene parámetros configurables. El archivo ISO a montar y desmontar está establecido directamente en el código fuente, en la siguiente línea:
Código:
$isoPath = Join-Path $PSScriptRoot "CD-ROM.iso"
Si se requiere que el script no interrumpa, solo hay que comentar las dos líneas que llaman a las funciones Confirm-Continue y Show-GoodbyeScreen:
Código
...
Show-WelcomeScreen
# Confirm-Continue
Mount-ISO
# Show-GoodbyeScreen
Mount virtual drive.ps1
Código
<#PSScriptInfo
.VERSION 1.0
.GUID 14688e75-298c-1112-a2d0-1234567890aa
.AUTHOR ElektroStudios
.COMPANYNAME ElektroStudios
.COPYRIGHT ElektroStudios © 2025
#>
<#
===========================================================================================
| |
| Variables |
| |
===========================================================================================
#>
$isoPath = Join-Path $PSScriptRoot "CD-ROM.iso"
<#
===========================================================================================
| |
| Functions |
| |
===========================================================================================
#>
function Show-WelcomeScreen {
Clear-Host
Write-Host ""
Write-Host " $($host.ui.RawUI.WindowTitle)"
Write-Host " +==================================================+"
Write-Host " | |"
Write-Host " | This script will mount a virtual drive with the |"
Write-Host " | ISO file of the specified path. |"
Write-Host " | |"
Write-Host " +=================================================+"
Write-Host ""
Write-Host " Script Settings " -ForegroundColor DarkGray
Write-Host " ===========================" -ForegroundColor DarkGray
Write-Host " ISO File: $([System.IO.Path]::GetFullPath($isoPath))" -ForegroundColor Gray
Write-Host ""
}
function Confirm-Continue {
Write-Host " Press 'Y' key to continue or 'N' to exit."
Write-Host ""
Write-Host " -Continue? (Y/N)"
do {
$key = $Host.UI.RawUI.ReadKey("NoEcho,IncludeKeyDown")
$char = $key.Character.ToString().ToUpper()
if ($char -ne "Y" -and $char -ne "N") {
[console]::beep(1500, 500)
}
} while ($char -ne "Y" -and $char -ne "N")
if ($char -eq "N") {Exit(1)} else {Clear-Host}
}
function Mount-ISO {
if (Test-Path $isoPath) {
try {
$fullPath = (Resolve-Path $isoPath).Path
$alreadyMounted = $null
try {
$alreadyMounted = Get-DiskImage -ImagePath $fullPath -ErrorAction Stop
} catch {
}
if ($alreadyMounted -and $alreadyMounted.Attached) {
Write-Host "ISO is already mounted. Skipping mount." -ForegroundColor Cyan
} else {
try {
$image = Mount-DiskImage -ImagePath $fullPath -PassThru -ErrorAction Stop
Write-Host "Virtual drive has been mounted successfully." -ForegroundColor Green
} catch {
Write-Host "Error while mounting the ISO:" -ForegroundColor Red
Write-Host $_.Exception.Message -ForegroundColor Yellow
}
}
}
catch {
Write-Host "Failed to mount the virtual drive." -ForegroundColor Red
Write-Host $_.Exception.Message -ForegroundColor Yellow
}
} else {
Write-Warning "ISO file not found at: $isoPath"
}
}
function Show-GoodbyeScreen {
Write-Host "Operation Completed!" -BackgroundColor Black -ForegroundColor Green
Write-Host ""
Write-Host "Press any key to exit..."
$key = $Host.UI.RawUI.ReadKey("NoEcho, IncludeKeyDown")
Exit(0)
}
<#
===========================================================================================
| |
| Main |
| |
===========================================================================================
#>
[System.Console]::Title = "Mount ISO file - by Elektro"
#[System.Console]::SetWindowSize(150, 45)
[CultureInfo]::CurrentUICulture = "en-US"
try { Set-ExecutionPolicy -ExecutionPolicy "Unrestricted" -Scope "Process" } catch { }
Show-WelcomeScreen
Confirm-Continue
Mount-ISO
Show-GoodbyeScreen
Dismount virtual drive.ps1
Código
<#PSScriptInfo
.VERSION 1.0
.GUID 14688e75-298c-1112-a2d0-1234567890ab
.AUTHOR ElektroStudios
.COMPANYNAME ElektroStudios
.COPYRIGHT ElektroStudios © 2025
#>
<#
===========================================================================================
| |
| Variables |
| |
===========================================================================================
#>
$isoPath = Join-Path $PSScriptRoot "CD-ROM.iso"
<#
===========================================================================================
| |
| Functions |
| |
===========================================================================================
#>
function Show-WelcomeScreen {
Clear-Host
Write-Host ""
Write-Host " $($host.ui.RawUI.WindowTitle)"
Write-Host " +======================================================+"
Write-Host " | |"
Write-Host " | This script will dismount a virtual drive previously |"
Write-Host " | mounted with the ISO file of the specified path. |"
Write-Host " | |"
Write-Host " +======================================================+"
Write-Host ""
Write-Host " Script Settings " -ForegroundColor DarkGray
Write-Host " ===========================" -ForegroundColor DarkGray
Write-Host " ISO File: $([System.IO.Path]::GetFullPath($isoPath))" -ForegroundColor Gray
Write-Host ""
}
function Confirm-Continue {
Write-Host " Press 'Y' key to continue or 'N' to exit."
Write-Host ""
Write-Host " -Continue? (Y/N)"
do {
$key = $Host.UI.RawUI.ReadKey("NoEcho,IncludeKeyDown")
$char = $key.Character.ToString().ToUpper()
if ($char -ne "Y" -and $char -ne "N") {
[console]::beep(1500, 500)
}
} while ($char -ne "Y" -and $char -ne "N")
if ($char -eq "N") {Exit(1)} else {Clear-Host}
}
function Dismount-ISO {
if (Test-Path $isoPath) {
try {
$fullPath = (Resolve-Path $isoPath).Path
$alreadyMounted = $null
try {
$alreadyMounted = Get-DiskImage -ImagePath $fullPath -ErrorAction Stop
} catch {
}
if ($alreadyMounted -and $alreadyMounted.Attached) {
try {
Dismount-DiskImage -ImagePath $fullPath -ErrorAction Stop
Write-Host "Virtual drive has been dismounted successfully." -ForegroundColor Green
} catch {
Write-Host "Error while dismounting the virtual drive:" -ForegroundColor Red
Write-Host $_.Exception.Message -ForegroundColor Yellow
}
} else {
Write-Host "No virtual drive mounted for this ISO. Nothing to dismount." -ForegroundColor Cyan
}
}
catch {
Write-Host "Failed to dismount the virtual drive." -ForegroundColor Red
Write-Host $_.Exception.Message -ForegroundColor Yellow
}
} else {
Write-Warning "ISO file not found at: $isoPath"
}
}
function Show-GoodbyeScreen {
Write-Host "Operation Completed!" -BackgroundColor Black -ForegroundColor Green
Write-Host ""
Write-Host "Press any key to exit..."
$key = $Host.UI.RawUI.ReadKey("NoEcho, IncludeKeyDown")
Exit(0)
}
<#
===========================================================================================
| |
| Main |
| |
===========================================================================================
#>
[System.Console]::Title = "Dismount ISO file - by Elektro"
#[System.Console]::SetWindowSize(150, 45)
[CultureInfo]::CurrentUICulture = "en-US"
try { Set-ExecutionPolicy -ExecutionPolicy "Unrestricted" -Scope "Process" } catch { }
Show-WelcomeScreen
Confirm-Continue
Dismount-ISO
Show-GoodbyeScreen
Navegación