none
How to attach DVD CD to VM using WMI in PowerShell RRS feed

  • Question

  • Hello, I have seen many scripts that create a new dvd drive and and attach it to a VM as long as there is already an existing DVD drive but have not seen any that just add an unconnected DVD drive.  I also need to use WMI in powershell and not the Hyper V library on codeplex or SCVMM.  Any help would be great!  Thanks. 

    Here is what I have so far...

     $vmName = "vmname"

    $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 1}

    # 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 0 -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 = 1
    $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)))

    Tuesday, June 14, 2011 7:08 PM

Answers

  • I got it finally.  I was just going about it the wrong way.  I needed to add resources to the vm like during creation.  Here is the script.  Cheers!!

     #Mount a DVD to the VM

    #get the current computer name
    $currentname=get-wmiobject Win32_ComputerSystem

    $server=$currentname.name

    $VMname="vmname"

    #get the virtual machine management service
    $virtualmanagementservice = Get-WmiObject -computername $server -namespace "root\virtualization" -class "Msvm_VirtualSystemManagementService" 

    #get the virtual machine you want to modify
    $Vm = Get-WmiObject -computername $server -Namespace root\virtualization Msvm_ComputerSystem | where {$_.ElementName -like $VMname}

    #get that vm's settings
    $vmsettings = Get-WmiObject -computername $server -Namespace "root\virtualization" -Query "Associators of {$Vm} Where ResultClass=Msvm_VirtualSystemSettingData AssocClass=Msvm_SettingsDefineState"

    #Get the IDE controller.  In this case I am using 1 because I want 0 to be used for vhd's
    $idecontroller = (Get-WmiObject -computername $server -Namespace "root\virtualization" -Query "Associators of {$vmsettings} Where ResultClass=Msvm_ResourceAllocationSettingData AssocClass=Msvm_VirtualSystemSettingDataComponent" | where-object {$_.ResourceSubType -eq "Microsoft Emulated IDE Controller" -and $_.Address -eq 1})

    #get the allocation capability of Microsoft Synthetic DVD Drive and the default disk
    $allocationcapability = Get-WmiObject -computername $server -Namespace "root\virtualization" -Query "SELECT * FROM Msvm_AllocationCapabilities WHERE ResourceSubType = 'Microsoft Synthetic DVD Drive' AND ResourceType ='16'"
    $thedefaultdisk = (Get-WmiObject -computername $server -Namespace "root\virtualization" -Query "Associators of {$allocationcapability} Where ResultClass=Msvm_ResourceAllocationSettingData AssocClass=Msvm_SettingsDefineCapabilities" | where-object {$_.InstanceID -like "*Default"})

    #add the drive parent and address and create the drive
    $thedefaultdisk.Parent = $idecontroller.__Path
    $thedefaultdisk.Address = 0
    $newdrive = ($virtualmanagementservice.AddVirtualSystemResources($Vm.__Path, $thedefaultdisk.PSBase.GetText(1))).NewResources

    $allocationcapability = Get-WmiObject -computername $server -Namespace "root\virtualization" -Query "SELECT * FROM Msvm_AllocationCapabilities WHERE ResourceSubType = 'Microsoft Synthetic DVD Drive' AND ResourceType ='16'"
    $defaultDVD = (Get-WmiObject -computername $server -Namespace "root\virtualization" -Query "Associators of {$allocationcapability} Where ResultClass=Msvm_ResourceAllocationSettingData AssocClass=Msvm_SettingsDefineCapabilities" | where-object {$_.InstanceID -like "*Default"})
        
    $defaultDVD.Parent = $newdrive

    $virtualmanagementservice.AddVirtualSystemResources($Vm.__Path, $defaultDVD.PSBase.GetText(1)) |out-null 

    • Marked as answer by mbiver Wednesday, June 15, 2011 5:39 PM
    Wednesday, June 15, 2011 5:39 PM