liveBook · Manning

Add-VMDvdDrive

As explained in Practical Automation with PowerShell: Effective scripting from the console to the cloud

Add-VMDvdDrive is a Hyper-V PowerShell cmdlet that adds a virtual DVD drive to a virtual machine.

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

How Practical Automation with PowerShell: Effective scripting from the console to the cloud covers it

How deep it goes
Explains conditional attachment and rerun safety, but does not descend into cmdlet internals.
Examples
Uses a VM setup sequence that attaches an ISO, selects DVD-first boot order, and supports rerunning after later failures.
What it assumes
Assumes a Hyper-V VM object and ISO path, while demonstrating the DVD-drive existence check from the configuration step.
What you can do after
Operate/configure a VM script that attaches an ISO without creating duplicate DVD drives during retries.
How it connects
Connects virtual machines, ISO files, Set-VMDvdDrive, Set-VMFirmware, and resumable automation.

This explainer was generated by Manning's knowledge graph from the text of Practical Automation with PowerShell: Effective scripting from the console to the cloud. The section links point to the parts of the book it draws on.