Hyper-V How To: Change ISO in VM Virtual DVD Drive using Script

Some friends here on the Hyper-V team shared a PowerShell 2.0 script for changing the ISO in a VM DVD drive:

# Change ISO in virtual DVD drive

param(
    [string]$vmName = $(throw "Must specify virtual machine name"),
    [int]$ideNum = $(throw "Must specify IDE location of DVD drive"),
    [int]$lunNum = $(throw "Must specify LUN location of DVD drive"),
    [string]$isoPath = $(throw "Must specify a path to ISO")
)

$vm = gwmi -namespace root\virtualization Msvm_ComputerSystem -filter "ElementName='$vmName'"

# Get the virtual machine's setting data
$vssd = gwmi -namespace root\virtualization `
    -query "Associators of {$vm} where ResultClass=Msvm_VirtualSystemSettingData" |`
    where{$_.SettingType -eq 3}

# Get IDE Controller
$ide = gwmi -namespace root\virtualization `
    -query "Associators of {$vssd} where ResultClass=Msvm_ResourceAllocationSettingData" |`
    where{$_.ResourceType -eq 5 -and $_.Address -eq $ideNum}

# Get the DVD Drive at Lun 0
$dvdDrive = gwmi -namespace root\virtualization `
    -query "Associators of {$vssd} where ResultClass=Msvm_ResourceAllocationSettingData" |`
    where{$_.ResourceType -eq 16 -and $_.Address -eq $lunNum -and $_.Parent -eq $ide.__PATH}

# Create a new DVD/CD virtual disk resource
$allocationCapabilities = gwmi -namespace root\virtualization Msvm_AllocationCapabilities `
    -filter "ResourceType = 21 AND ResourceSubtype = 'Microsoft Virtual CD/DVD Disk'"
$allocationPath = $allocationCapabilities.__PATH.replace("\","\\")
$default = gwmi -namespace root\virtualization Msvm_SettingsDefineCapabilities `
    -filter "ValueRange = 0 AND GroupComponent = '$allocationPath'"

$dvd = [wmi]$default.PartComponent

# Set the properties on the virtual DVD to attach it to the right drive
$dvd.Address = 0
$dvd.Parent = $dvdDrive.__PATH
$dvd.Connection = @($isoPath)

# Attach the new DVD to the virtual machine
$vmms = gwmi -namespace root\virtualization Msvm_VirtualSystemManagementService

$result = $vmms.AddVirtualSystemResources($vm,@($dvd.GetText(1)))

if($result.ReturnValue -eq 4096){
    # A Job was started, and can be tracked using its Msvm_Job instance
    $job = [wmi]$result.Job
    # Wait for job to finish
    while($job.jobstate -lt 7){$job.get()}
    # Return the Job's error code
    return $job.ErrorCode
}
# Otherwise, the method completed
return $result.ReturnValue

For more info on how to use PS cmdlets see: https://www.microsoft.com/technet/scriptcenter/topics/msh/cmdlets/index.mspx

See also James O’Neil’s New and improved PowerShell Library for Hyper-V. Now with more functions and... documentation!

For all 35 sample Hyper-V PS1 scripts in a zipfile, go to: Hyper-V PowerShell Example Scripts.zip-download