Fotolia

Tip

Mount an ISO file to a virtual DVD drive with PowerShell

It's easy to manage external media from PowerShell. It's worth noting, however, that the command syntax varies depending on whether you're referencing an IDE or SCSI drive.

One of the primary limitations of using Generation 2 VMs is that those VMs don't support the use of a physical DVD drive. As such, any virtualization administrator who wants to leverage DVD media must mount an ISO file to a virtual DVD drive. While this approach might not seem all that appealing, ISO files do offer one significant advantage.

If you've ever accessed a physical DVD drive from a Generation 1 VM, then you know that you can only attach a physical DVD drive to one VM at a time. Conversely, you can easily mount an ISO file to multiple VMs. By using ISO files instead of physical DVD media, you can script bulk operations involving ISO files and multiple VMs.

If you want to manipulate how a VM uses an ISO file, then you need to use a set of cmdlets based on the VMDvdDrive noun. Before we go over how to use these cmdlets, note that System Center Virtual Machine Manager has its own set of cmdlets based on a noun called VirtualDVDDrive. For the purposes of this article, I'll stick to native PowerShell.

The most basic way to use the VMDvdDrive noun is to check the mappings for an individual VM. Suppose, for a moment, that you want to see if a VM named DC is configured to use any sort of external media. You could find out by using this command:

Get-VMDVDDrive -VMName DC

As you can see in Figure A, this particular VM is configured to use an ISO file. The DvdMediaType field tells us that this is an ISO file rather than a physical DVD, and the Path field shows the path and the name of the ISO file that is attached to the VM.

ISO file attached to the VM
Figure A. The VM is connected to an ISO file.

What if you want to map the DVD drive to a different ISO file? The first step to do so would usually be to detach the existing ISO file -- there is also a shortcut to remap the virtual DVD drive using the Set-VMDvdDrive cmdlet. You can detach the virtual DVD drive with the Remove-VMDvdDrive cmdlet.

This cmdlet was originally designed to work with Integrated Drive Electronic (IDE) devices, which, of course, aren't supported on a Generation 2 VM. If you look at the Microsoft documentation, it lists the IDE controller number and controller location as required parameters, but these parameters are invalid for SCSI DVD drives.

You can get around this problem by using pipeline input. Append the pipe symbol to the Get-VMDvdDrive cmdlet I showed you earlier, and then enter Remove-VMDvdDrive. Here's what such a command might look like:

Get-VMDVDDrive -VMName DC | Remove-VMDVDDrive

You can see an example of this in Figure B.

Remove a SCSI virtual DVD drive
Figure B. This is how you remove a SCSI virtual DVD drive.

Now let's look at how you add a SCSI virtual DVD drive. The easiest way to accomplish this is by using the following command:

Add-VMDVDDrive -VMName <your VM name> -Path <ISO file>

In my case, for example, the command might be:

Add-VMDVDDrive -VMName DC -Path 'C:\ISO\Hyper-V Server 2016.iso'

You can see an example of this in Figure C.

Mount an ISO file as a virtual DVD drive.
Figure C. This is how you mount an ISO file as a virtual DVD drive.

These examples are designed to illustrate the basic principles of attaching and detaching a virtual DVD drive. However, it's possible to write scripts that are far more advanced. For example, you could use the techniques illustrated in this article to mount an ISO file to a VM, perform a software installation, and then remove the ISO file and attach a different one for the purpose of installing a different application.

Dig Deeper on IT systems management and monitoring