Add-VMDvdDrive attaches a virtual DVD drive to a Hyper-V virtual machine while configuring it for an ISO-based installation. section 10.6
Use it safely in resumable VM automation:
- Check whether the VM already has a DVD drive before adding one. section 10.6
- Add the drive with the ISO path when none exists; otherwise update the existing drive with Set-VMDvdDrive. section 10.6
- Treat an incorrect ISO path as an actionable failure rather than suppressing the error. section 10.6
Idempotent ISO attachment
A VM-configuration routine tests $VM.DVDDrives. It calls Add-VMDvdDrive -Path $ISO only when no drive exists; otherwise, it calls Set-VMDvdDrive -Path $ISO, avoiding duplicate drives after a rerun. section 10.6
Listing 10.9 Updating virtual machine settings
$VMMemory = @{ #1
DynamicMemoryEnabled = $true
MinimumBytes = 512MB
MaximumBytes = 2048MB
Buffer = 20
StartupBytes = 1024MB
}
$VM | Set-VMMemory @VMMemory
$VM | Set-VM -AutomaticCheckpointsEnabled $false #2
if(-not $VM.DVDDrives){ #3
$VM | Add-VMDvdDrive -Path $ISO
}
else{
$VM | Set-VMDvdDrive -Path $ISO
}
$BootOrder = @( #4
$VM.DVDDrives[0]
$VM.HardDrives[0]
)
$VM | Set-VMFirmware -BootOrder $BootOrder
Inspect the complete configuration sequence showing the DVD-drive existence test, ISO attachment, and boot-order setup. section 10.6
