'Adding the device 'Microsoft:Hyper-V:Virtual CD/DVD Disk' to 'VMNAME' failed

I am writing a script that should help me automate deployments of my Hyper-V VMs testing environments. Right now I want new VMs to be created with empty VHDX drives and Windows installation ISO attached as a virtual DVD drive.

I've created several VMs with the New-VM cmdlet. However, it appears that this cmdlet does not have an option to attach the ISO when creating a new virtual machine. It appears that I have to use the Add-VMDvdDrive PowerShell cmdlet. Correct?

However, when I run the following command, I get an error:

Add-VMDvdDrive -VMName "VMNAME" -Path "F:\WindowsServer2012R2.iso

Here is the error:

Add-VMDvdDrive : Adding the device 'Microsoft:Hyper-V:Virtual CD/DVD Disk' to 'VMNAME' failed.
At C:\Users\user\Documents\vmdeploy.ps1:3 char:1
+ Add-VMDvdDrive -VMName "svn1.contoso.com" -Path "F:\WindowsServer2012 ...
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : NotSpecified: (:) [Add-VMDvdDrive], VirtualizationException
    + FullyQualifiedErrorId : Unspecified,Microsoft.HyperV.PowerShell.Commands.AddVMDvdDrive

What am I doing wrong?



Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source