elhacker.net cabecera Bienvenido(a), Visitante. Por favor Ingresar o Registrarse
¿Perdiste tu email de activación?.


Tema destacado: Guía rápida para descarga de herramientas gratuitas de seguridad y desinfección


+  Foro de elhacker.net
|-+  Programación
| |-+  Scripting
| | |-+  [APORTE] [PowerShell] Montar y desmontar una unidad virtual de CD/DVD/BD o VHD/VHDX/VHDS
0 Usuarios y 2 Visitantes están viendo este tema.
Páginas: [1] Ir Abajo Respuesta Imprimir
Autor Tema: [APORTE] [PowerShell] Montar y desmontar una unidad virtual de CD/DVD/BD o VHD/VHDX/VHDS  (Leído 204 veces)
Eleкtro
Ex-Staff
*
Desconectado Desconectado

Mensajes: 9.928



Ver Perfil
[APORTE] [PowerShell] Montar y desmontar una unidad virtual de CD/DVD/BD o VHD/VHDX/VHDS
« en: Ayer a las 02:17 »

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
  1. ...
  2. Show-WelcomeScreen
  3. # Confirm-Continue
  4. Mount-ISO
  5. # Show-GoodbyeScreen



Mount virtual drive.ps1



Código
  1. <#PSScriptInfo
  2. .VERSION 1.0
  3. .GUID 14688e75-298c-1112-a2d0-1234567890aa
  4. .AUTHOR ElektroStudios
  5. .COMPANYNAME ElektroStudios
  6. .COPYRIGHT ElektroStudios © 2025
  7. #>
  8.  
  9. <#
  10. ===========================================================================================
  11. |                                                                                         |
  12. |                                        Variables                                        |
  13. |                                                                                         |
  14. ===========================================================================================
  15. #>
  16.  
  17. $isoPath = Join-Path $PSScriptRoot "CD-ROM.iso"
  18.  
  19. <#
  20. ===========================================================================================
  21. |                                                                                         |
  22. |                                    Functions                                            |
  23. |                                                                                         |
  24. ===========================================================================================
  25. #>
  26.  
  27. function Show-WelcomeScreen {
  28.    Clear-Host
  29.    Write-Host ""
  30.    Write-Host " $($host.ui.RawUI.WindowTitle)"
  31.    Write-Host " +==================================================+"
  32.    Write-Host " |                                                 |"
  33.    Write-Host " | This script will mount a virtual drive with the |"
  34.    Write-Host " | ISO file of the specified path.                 |"
  35.    Write-Host " |                                                 |"
  36.    Write-Host " +=================================================+"
  37.    Write-Host ""
  38.    Write-Host " Script Settings            " -ForegroundColor DarkGray
  39.    Write-Host " ===========================" -ForegroundColor DarkGray
  40.    Write-Host " ISO File: $([System.IO.Path]::GetFullPath($isoPath))" -ForegroundColor Gray
  41.    Write-Host ""
  42. }
  43.  
  44. function Confirm-Continue {
  45.    Write-Host " Press 'Y' key to continue or 'N' to exit."
  46.    Write-Host ""
  47.    Write-Host " -Continue? (Y/N)"
  48.    do {
  49.        $key = $Host.UI.RawUI.ReadKey("NoEcho,IncludeKeyDown")
  50.        $char = $key.Character.ToString().ToUpper()
  51.        if ($char -ne "Y" -and $char -ne "N") {
  52.            [console]::beep(1500, 500)
  53.        }
  54.    } while ($char -ne "Y" -and $char -ne "N")
  55.    if ($char -eq "N") {Exit(1)} else {Clear-Host}
  56. }
  57.  
  58. function Mount-ISO {
  59.    if (Test-Path $isoPath) {
  60.        try {
  61.            $fullPath = (Resolve-Path $isoPath).Path
  62.            $alreadyMounted = $null
  63.            try {
  64.                $alreadyMounted = Get-DiskImage -ImagePath $fullPath -ErrorAction Stop
  65.            } catch {
  66.            }
  67.  
  68.            if ($alreadyMounted -and $alreadyMounted.Attached) {
  69.                Write-Host "ISO is already mounted. Skipping mount." -ForegroundColor Cyan
  70.            } else {
  71.                try {
  72.                    $image = Mount-DiskImage -ImagePath $fullPath -PassThru -ErrorAction Stop
  73.                    Write-Host "Virtual drive has been mounted successfully." -ForegroundColor Green
  74.                } catch {
  75.                    Write-Host "Error while mounting the ISO:" -ForegroundColor Red
  76.                    Write-Host $_.Exception.Message -ForegroundColor Yellow
  77.                }
  78.            }
  79.        }
  80.        catch {
  81.            Write-Host "Failed to mount the virtual drive." -ForegroundColor Red
  82.            Write-Host $_.Exception.Message -ForegroundColor Yellow
  83.        }
  84.    } else {
  85.        Write-Warning "ISO file not found at: $isoPath"
  86.    }
  87. }
  88.  
  89. function Show-GoodbyeScreen {
  90.    Write-Host "Operation Completed!" -BackgroundColor Black -ForegroundColor Green
  91.    Write-Host ""
  92.    Write-Host "Press any key to exit..."
  93.    $key = $Host.UI.RawUI.ReadKey("NoEcho, IncludeKeyDown")
  94.    Exit(0)
  95. }
  96.  
  97. <#
  98. ===========================================================================================
  99. |                                                                                         |
  100. |                                         Main                                            |
  101. |                                                                                         |
  102. ===========================================================================================
  103. #>
  104.  
  105. [System.Console]::Title = "Mount ISO file - by Elektro"
  106. #[System.Console]::SetWindowSize(150, 45)
  107. [CultureInfo]::CurrentUICulture = "en-US"
  108.  
  109. try { Set-ExecutionPolicy -ExecutionPolicy "Unrestricted" -Scope "Process" } catch { }
  110.  
  111. Show-WelcomeScreen
  112. Confirm-Continue
  113. Mount-ISO
  114. Show-GoodbyeScreen
  115.  



Dismount virtual drive.ps1



Código
  1. <#PSScriptInfo
  2. .VERSION 1.0
  3. .GUID 14688e75-298c-1112-a2d0-1234567890ab
  4. .AUTHOR ElektroStudios
  5. .COMPANYNAME ElektroStudios
  6. .COPYRIGHT ElektroStudios © 2025
  7. #>
  8.  
  9. <#
  10. ===========================================================================================
  11. |                                                                                         |
  12. |                                        Variables                                        |
  13. |                                                                                         |
  14. ===========================================================================================
  15. #>
  16.  
  17. $isoPath = Join-Path $PSScriptRoot "CD-ROM.iso"
  18.  
  19. <#
  20. ===========================================================================================
  21. |                                                                                         |
  22. |                                    Functions                                            |
  23. |                                                                                         |
  24. ===========================================================================================
  25. #>
  26.  
  27. function Show-WelcomeScreen {
  28.    Clear-Host
  29.    Write-Host ""
  30.    Write-Host " $($host.ui.RawUI.WindowTitle)"
  31.    Write-Host " +======================================================+"
  32.    Write-Host " |                                                      |"
  33.    Write-Host " | This script will dismount a virtual drive previously |"
  34.    Write-Host " | mounted with the ISO file of the specified path.     |"
  35.    Write-Host " |                                                      |"
  36.    Write-Host " +======================================================+"
  37.    Write-Host ""
  38.    Write-Host " Script Settings            " -ForegroundColor DarkGray
  39.    Write-Host " ===========================" -ForegroundColor DarkGray
  40.    Write-Host " ISO File: $([System.IO.Path]::GetFullPath($isoPath))" -ForegroundColor Gray
  41.    Write-Host ""
  42. }
  43.  
  44. function Confirm-Continue {
  45.    Write-Host " Press 'Y' key to continue or 'N' to exit."
  46.    Write-Host ""
  47.    Write-Host " -Continue? (Y/N)"
  48.    do {
  49.        $key = $Host.UI.RawUI.ReadKey("NoEcho,IncludeKeyDown")
  50.        $char = $key.Character.ToString().ToUpper()
  51.        if ($char -ne "Y" -and $char -ne "N") {
  52.            [console]::beep(1500, 500)
  53.        }
  54.    } while ($char -ne "Y" -and $char -ne "N")
  55.    if ($char -eq "N") {Exit(1)} else {Clear-Host}
  56. }
  57.  
  58. function Dismount-ISO {
  59.    if (Test-Path $isoPath) {
  60.        try {
  61.            $fullPath = (Resolve-Path $isoPath).Path
  62.            $alreadyMounted = $null
  63.            try {
  64.                $alreadyMounted = Get-DiskImage -ImagePath $fullPath -ErrorAction Stop
  65.            } catch {
  66.            }
  67.            if ($alreadyMounted -and $alreadyMounted.Attached) {
  68.                try {
  69.                    Dismount-DiskImage -ImagePath $fullPath -ErrorAction Stop
  70.                    Write-Host "Virtual drive has been dismounted successfully." -ForegroundColor Green
  71.                } catch {
  72.                    Write-Host "Error while dismounting the virtual drive:" -ForegroundColor Red
  73.                    Write-Host $_.Exception.Message -ForegroundColor Yellow
  74.                }
  75.            } else {
  76.                Write-Host "No virtual drive mounted for this ISO. Nothing to dismount." -ForegroundColor Cyan
  77.            }
  78.        }
  79.        catch {
  80.            Write-Host "Failed to dismount the virtual drive." -ForegroundColor Red
  81.            Write-Host $_.Exception.Message -ForegroundColor Yellow
  82.        }
  83.    } else {
  84.        Write-Warning "ISO file not found at: $isoPath"
  85.    }
  86. }
  87.  
  88. function Show-GoodbyeScreen {
  89.    Write-Host "Operation Completed!" -BackgroundColor Black -ForegroundColor Green
  90.    Write-Host ""
  91.    Write-Host "Press any key to exit..."
  92.    $key = $Host.UI.RawUI.ReadKey("NoEcho, IncludeKeyDown")
  93.    Exit(0)
  94. }
  95.  
  96. <#
  97. ===========================================================================================
  98. |                                                                                         |
  99. |                                         Main                                            |
  100. |                                                                                         |
  101. ===========================================================================================
  102. #>
  103.  
  104. [System.Console]::Title = "Dismount ISO file - by Elektro"
  105. #[System.Console]::SetWindowSize(150, 45)
  106. [CultureInfo]::CurrentUICulture = "en-US"
  107.  
  108. try { Set-ExecutionPolicy -ExecutionPolicy "Unrestricted" -Scope "Process" } catch { }
  109.  
  110. Show-WelcomeScreen
  111. Confirm-Continue
  112. Dismount-ISO
  113. Show-GoodbyeScreen
  114.  


« Última modificación: Ayer a las 02:34 por Eleкtro » En línea



Páginas: [1] Ir Arriba Respuesta Imprimir 

Ir a:  

Mensajes similares
Asunto Iniciado por Respuestas Vistas Último mensaje
[VBS][AYUDA] Montar y Desmontar particion NFTS
Scripting
danny920825 1 2,122 Último mensaje 15 Marzo 2014, 12:30 pm
por Eleкtro
[APORTE] [PowerShell] 3rd Party Driver Backup Tool
Scripting
Eleкtro 3 2,706 Último mensaje 4 Marzo 2024, 01:07 am
por Danielㅤ
[APORTE] [PowerShell] Truncate Log Files
Scripting
Eleкtro 0 1,430 Último mensaje 3 Marzo 2024, 22:14 pm
por Eleкtro
[APORTE] [PowerShell] SetACL.exe | Get Full Registry Ownership
Scripting
Eleкtro 0 3,015 Último mensaje 4 Marzo 2024, 15:31 pm
por Eleкtro
[APORTE] [PowerShell] RAR.exe | Test RAR Files
Scripting
Eleкtro 0 8,254 Último mensaje 5 Abril 2024, 00:39 am
por Eleкtro
WAP2 - Aviso Legal - Powered by SMF 1.1.21 | SMF © 2006-2008, Simple Machines