I have a workflow that takes a vm (u_vm below) and an array of strings (u_ldevs below). It removes disks from the VM that reside on datastores where the last 4 digits match a string in the given array (u_ldevs). (in case you are wondering, ldev is a 4 digit hex number that is the LUN id on the storage array and we add that to the end of each datastore name)
The System.log messages in the loop output the expected data; all disks to be removed are correct and the device is found. The task runs successfully (and I see it successful in vSphere), however the VM is unchanged. I added a debug loop at the bottom to iterate over the devices to validate that the spec contains what I put in it, but that bottom loop returns no data, which makes me think I'm missing something really stupid. Can someone take a look?
System.log(u_vm.name) var spec = new VcVirtualMachineConfigSpec(); spec.deviceChange = []; var ix = 0; for each (var device in u_vm.config.hardware.device) { if (device instanceof VcVirtualDisk) { if (device.backing instanceof VcVirtualDiskFlatVer2BackingInfo) { var dsname = device.backing.datastore.name; var possibleLdev = dsname.substring(dsname.length-4, dsname.length); for each (var ldev in u_ldevs) { if (ldev == possibleLdev) { var devChange = new VcVirtualDeviceConfigSpec(); devChange.operation = VcVirtualDeviceConfigSpecOperation.remove; devChange.device = device; spec.deviceChange.push(devChange); System.log("Found " + dsname); // this prints the datastore name System.log("device: " + device.deviceInfo.label); // this prints the device label "Hard Disk #" } } } } } for (var ix in spec.deviceChange) // have also tried a for each (var devChange in spec.deviceChange) ... { System.log('dev change:'); // this never prints System.log('dc op: ' + spec.deviceChange[ix].operation.value); // this never prints System.log('dev: ' + spec.deviceChange[ix].device.deviceInfo.summary); // this never prints } task = u_vm.reconfigVM_Task(spec); // this run successfully