I'm still struggling with this seemingly simple task. To recap:
* I created a new VM
* I removed the existing disk
* There is an IDE0 controller present
* I'm trying to add an existing IDE disk (from elsewhere) to the IDE0 controller as the only disk
$ds = "Datastore03"
$vmName = "Test VM"
$vmdkName = "[" + $ds + "] " + $vmName + "/Test VM.vmdk"
$vm = Get-VM -Name $vmName
$spec = New-Object VMware.Vim.VirtualMachineConfigSpec
$spec.deviceChange = @()
$spec.deviceChange += New-Object VMware.Vim.VirtualDeviceConfigSpec
$spec.deviceChange[0].device = New-Object VMware.Vim.VirtualDisk
$spec.deviceChange[0].device.backing = New-Object VMware.Vim.VirtualDiskFlatVer2BackingInfo
$spec.deviceChange[0].device.backing.datastore = $ds.ExtensionData.MoRef
$spec.deviceChange[0].device.backing.fileName = $vmdkName
$spec.deviceChange[0].device.backing.diskMode = "independent_persistent"
$spec.deviceChange[0].device.unitnumber = -1
$spec.deviceChange[0].device.controllerKey = $ideCtrl.ExtensionData.Key
$spec.deviceChange[0].operation = "add"
$vm.ExtensionData.ReconfigVM($spec)
This produces the error Exception calling "ReconfigVM" with "1" argument(s): "Device requires a controller.". Note that the controller is present in the GUI, and running...
$ideCtrl = $vm.ExtensionData.Config.Hardware.Device | where {$_.GetType().Name -eq "VirtualIDEController"} | select -First 1
echo $ideCtrl
...produces:
BusNumber : 0
Device : {3000}
Key : 200
DeviceInfo : VMware.Vim.Description
Backing :
Connectable :
SlotInfo :
ControllerKey :
UnitNumber :
When checking the devices list this seems to correctly correspond to the IDE0 controller.
It seems this should be an "Add" operation since it's a new disk, but based on previous comments I tried it as an "Edit" too and receive the error Exception calling "ReconfigVM" with "1" argument(s): "Invalid configuration for device '0'."
I'd be grateful for any assistance as this is the last piece of the puzzle I'm stuck on.