Q: How can I use the Windows PowerShell cmdlets for Hyper-V to eject all ISOs from the DVD drive?
The cmdlet to eject an ISO or disconnect a VHD isn't obvious to even the most brilliant of admins.
December 4, 2011
A: If you want to eject an ISO from a DVD drive or even just disconnect a virtual hard disk (VHD) from a controller, it’s not obvious what actual cmdlet to use.
You need to use the Remove-VMDrive cmdlet, which removes the entire device from the virtual machine (VM). You must also add the -Diskonly switch, which just unmounts. This is assuming you have installed the Microsoft Hyper-V cmdlets from CodePlex (http://pshyperv.codeplex.com/).
Below, you can see an example of how to import them into the PowerShell instance, and, in the same command, eject whatever ISO is connected to the DVD drive on a VM, where the VM, named VM1, is on IDE controller 1, device 0:
Import-Module "c:Program FilesmodulesHyperVHyperv.psd1"Remove-VMDrive -Diskonly -VM VM1 1 0
If you want to go one step further and eject ISOs from all your VMs on your box, you could use the following:
get-vm | foreach {Remove-VMDrive -Diskonly -VM $_.vmelementName 1 0}
You will see an OK for each VM with ISO disconnected.If you want to disconnect only a specific ISO file from all the VMs, you can enumerate all the virtual disks and search for the ISO using this command:
PS C:> get-vm | get-vmdisk | where {$_.diskpath -eq "D:LibraryISOsSoftwareen_lync_server_2010_x64_dvd_598415.iso"}
It returns this output:
VMElementName : savdalwss10VMGUID : E28694D7-E4C3-4F98-9BF9-CD6A40F932EB
ControllerName : IDE Controller 1
ControllerInstanceID : Microsoft:E28694D7-E4C3-4F98-9BF9-CD6A40F932EB83F8638B-8DCA-4152-9EDA-2CA8B33039B41
ControllerID : 1DriveName : DVD Drive
DriveInstanceID : Microsoft:E28694D7-E4C3-4F98-9BF9-CD6A40F932EB83F8638B-8DCA-4152-9EDA-2CA8B33039B41D
DriveLUN : 0DiskPath : D:LibraryISOsSoftwareen_lync_server_2010_x64_dvd_598415.iso
DiskImage : D:LibraryISOsSoftwareen_lync_server_2010_x64_dvd_598415.iso
DiskName : ISO
Disk ImageDiskInstanceID : Microsoft:E28694D7-E4C3-4F98-9BF9-CD6A40F932EB83F8638B-8DCA-4152-9EDA-2CA8B33039B41L
To actually disconnect, you just add the Remove-VMDrive foreach loop to the end, but this time you can use the controller ID and LUN ID that resulted from the get-vmdisk command. This way, you can disconnect the ISO no matter what devices it’s mounted on:
PS C:> Get-VM | Get-VMDisk | where {$_.diskpath -eq "D:LibraryISOsSoftwareen_lync_server_2010_x64_dvd_598415.iso"} | foreach {Remove-VMDrive -Diskonly -VM $_.vmelementName $_.ControllerID $_.DriveLUN}
To read more FAQs about all things Windows, see John Savill's FAQs page.
About the Author
You May Also Like