Skip to content

VM devices, cluster HA and port groups

Recipe 8.1 started a vCenter task and waited for it. Most day-to-day changes to a VM or a cluster follow the same pattern: build a spec that describes the change, pass it to a …_Task method, and wait. This recipe does three common ones, the kind the old vRealize Orchestrator Cookbook covered for vRO 6: a VM’s CD/DVD drive, its network adapter, and a cluster’s vSphere HA settings.

Use the library workflow when there is one. Write an action when you need something it doesn’t do, such as a report or a change it doesn’t offer.

Getting ready

  • vc01.vmw.lab registered in Orchestrator (recipe 1.9), and the action waitForTask in com.vcfguide.chapter8 (recipe 8.1).
  • A test VM you can power off and change, vcfguide-test-01, with one network adapter on a distributed port group, and a second distributed port group to move it to.
  • For part C, a cluster with vSphere HA turned on. Part C reads the cluster’s settings and changes the HA override of the test VM only.

How to do it

  1. Run the library workflow Add CD-ROM (in Library > vCenter > Virtual Machine management > Device Management) on vcfguide-test-01. Choose a Device type: Client Device (the vSphere Client user’s drive), Host Device (the ESXi host’s drive), or Datastore ISO file, with the ISO’s path such as [datastore1] iso/tools.iso. It adds an IDE controller first if the VM has none, and does nothing if the VM already has a CD/DVD drive.
  2. To change what an existing drive holds, use Mount CD-ROM, which takes the same inputs. Disconnect all detachable devices from a running virtual machine disconnects every CD/DVD and floppy drive, and the serial and parallel ports, of a running VM in one go.
  3. To see what a VM has, create the action describeVmDevices in com.vcfguide.chapter8, with the input vm (VC:VirtualMachine) and the return type Array/string. It lists the CD/DVD drives and network adapters, one line each:
com.vcfguide.chapter8/describeVmDevices
if (!vm) {
throw "[VCFG][describeVmDevices] vm is required";
}
// instanceof matches only the exact scripting class, not a vSphere base type:
// a vmxnet3 adapter is a VcVirtualVmxnet3 but not a VcVirtualEthernetCard.
function isNetworkAdapter(device) {
return device instanceof VcVirtualVmxnet3 || device instanceof VcVirtualVmxnet2 ||
device instanceof VcVirtualVmxnet || device instanceof VcVirtualE1000e ||
device instanceof VcVirtualE1000 || device instanceof VcVirtualPCNet32 ||
device instanceof VcVirtualSriovEthernetCard;
}
// Port group names by key, to show a distributed port group's name rather than its key
var portgroupNames = {};
var networks = vm.network;
for (var n = 0; n < networks.length; n++) {
if (networks[n] instanceof VcDistributedVirtualPortgroup) {
portgroupNames[networks[n].key] = networks[n].name;
}
}
var lines = [];
var devices = vm.config.hardware.device;
for (var i = 0; i < devices.length; i++) {
var device = devices[i];
var label = device.deviceInfo.label;
var connected = device.connectable ? (device.connectable.connected ? "connected" : "not connected") : "";
var backing = device.backing;
if (device instanceof VcVirtualCdrom) {
var source = "empty";
if (backing instanceof VcVirtualCdromIsoBackingInfo) {
source = "ISO " + backing.fileName;
} else if (backing instanceof VcVirtualCdromRemotePassthroughBackingInfo) {
source = "client device";
} else if (backing instanceof VcVirtualCdromAtapiBackingInfo) {
source = "host device " + backing.deviceName;
}
lines.push(label + ": " + source + ", " + connected);
} else if (isNetworkAdapter(device)) {
var network = "unknown network";
if (backing instanceof VcVirtualEthernetCardDistributedVirtualPortBackingInfo) {
var key = backing.port.portgroupKey;
network = "port group " + (portgroupNames[key] || key);
} else if (backing instanceof VcVirtualEthernetCardNetworkBackingInfo) {
network = "network " + backing.deviceName;
}
lines.push(label + ": " + network + ", " + connected);
}
}
return lines;

Run it on vcfguide-test-01. After step 1 it returns something like Network adapter 1: port group Test, not connected and CD/DVD drive 1: client device, not connected.

  1. There’s no library workflow to remove a CD/DVD drive. Create removeCdroms, with the input vm and the return type number. A drive can only be removed while the VM is off, so the action checks that first:
com.vcfguide.chapter8/removeCdroms
if (!vm) {
throw "[VCFG][removeCdroms] vm is required";
}
if (vm.runtime.powerState.value !== "poweredOff") {
throw "[VCFG][removeCdroms] " + vm.name + " must be powered off to remove a CD/DVD drive";
}
var changes = [];
var devices = vm.config.hardware.device;
for (var i = 0; i < devices.length; i++) {
if (devices[i] instanceof VcVirtualCdrom) {
var change = new VcVirtualDeviceConfigSpec();
change.operation = VcVirtualDeviceConfigSpecOperation.remove;
change.device = devices[i];
changes.push(change);
}
}
if (changes.length === 0) {
System.log("[VCFG][removeCdroms] " + vm.name + " has no CD/DVD drive");
return 0;
}
var spec = new VcVirtualMachineConfigSpec();
spec.deviceChange = changes;
System.getModule("com.vcfguide.chapter8").waitForTask(vm.reconfigVM_Task(spec), 120);
System.log("[VCFG][removeCdroms] removed " + changes.length + " CD/DVD drive(s) from " + vm.name);
return changes.length;

Run it on the powered-off test VM. It returns 1, and 0 the second time.

B. Move a network adapter to another port group

Section titled “B. Move a network adapter to another port group”
  1. Run the library workflow Connect virtual machine NIC number to distributed virtual port group on vcfguide-test-01, with the second port group and NIC number 0 (the first adapter). Run describeVmDevices again: the adapter is on the new port group.
  2. For a standard switch, use the workflows in Library > vCenter > Networking > Standard Virtual Switch, such as Add port group in standard virtual switch and Update VNIC for port group in standard virtual switch. The Distributed Virtual Port Group folder has the matching workflows for distributed switches: add, update and delete a port group.

C. Read cluster HA settings and change one VM’s restart priority

Section titled “C. Read cluster HA settings and change one VM’s restart priority”
  1. Create getClusterHaSettings, with the input cluster (VC:ClusterComputeResource) and the return type Properties:
com.vcfguide.chapter8/getClusterHaSettings
if (!cluster) {
throw "[VCFG][getClusterHaSettings] cluster is required";
}
var das = cluster.configurationEx.dasConfig;
var settings = new Properties();
settings.put("haEnabled", das.enabled);
settings.put("hostMonitoring", das.hostMonitoring);
settings.put("vmMonitoring", das.vmMonitoring);
settings.put("admissionControlEnabled", das.admissionControlEnabled);
settings.put("defaultRestartPriority", das.defaultVmSettings ? das.defaultVmSettings.restartPriority : null);
// Per-VM overrides: VM name -> restart priority
var overrides = new Properties();
var vmConfigs = cluster.configurationEx.dasVmConfig || [];
for (var i = 0; i < vmConfigs.length; i++) {
var vmSettings = vmConfigs[i].dasSettings;
overrides.put(vmConfigs[i].key.name, vmSettings ? vmSettings.restartPriority : null);
}
settings.put("vmOverrides", overrides);
return settings;

Run it on your cluster. On a VCF management cluster it shows HA on, host monitoring enabled, and overrides that VCF set for its own appliances, such as vcf-m-vc at high and the NSX Edge nodes at highest.

  1. Create setVmHaRestartPriority, with the inputs cluster, vm and priority (string), and the return type void. It adds, edits or removes one VM’s override, and leaves everything else in the cluster as it is:
com.vcfguide.chapter8/setVmHaRestartPriority
if (!cluster || !vm) {
throw "[VCFG][setVmHaRestartPriority] cluster and vm are required";
}
var allowed = ["disabled", "lowest", "low", "medium", "high", "highest"];
if (priority && allowed.indexOf(priority) === -1) {
throw "[VCFG][setVmHaRestartPriority] priority must be one of " + allowed.join(", ") + ", got: " + priority;
}
// Is there an override for this VM already? Then edit it, otherwise add one.
var exists = false;
var vmConfigs = cluster.configurationEx.dasVmConfig || [];
for (var i = 0; i < vmConfigs.length; i++) {
if (vmConfigs[i].key.id === vm.id) {
exists = true;
}
}
var vmSpec = new VcClusterDasVmConfigSpec();
if (!priority) {
if (!exists) {
System.log("[VCFG][setVmHaRestartPriority] " + vm.name + " has no override, nothing to remove");
return;
}
vmSpec.operation = VcArrayUpdateOperation.remove;
vmSpec.removeKey = vm;
} else {
var info = new VcClusterDasVmConfigInfo();
info.key = vm;
var dasSettings = new VcClusterDasVmSettings();
dasSettings.restartPriority = priority;
info.dasSettings = dasSettings;
vmSpec.info = info;
vmSpec.operation = exists ? VcArrayUpdateOperation.edit : VcArrayUpdateOperation.add;
}
var spec = new VcClusterConfigSpecEx();
spec.dasVmConfigSpec = [vmSpec];
// true = change only what the spec says; leave the rest of the cluster settings as they are
var task = cluster.reconfigureComputeResource_Task(spec, true);
System.getModule("com.vcfguide.chapter8").waitForTask(task, 120);
System.log("[VCFG][setVmHaRestartPriority] " + vm.name + ": " + (priority ? "restart priority " + priority : "override removed"));
  1. Run it on vcfguide-test-01 with low, then with highest, then with the priority left empty. Read the settings after each run: the test VM appears with low, then highest, then not at all, and the other overrides don’t change.
Figure 8.19. A cluster's HA settings and per-VM overrides, read from a workflow.
Checked on an embedded Orchestrator 9.1.1, inside VCF Automation, 1 Dec 2026. instanceof VcVirtualEthernetCard is false for a vmxnet3 adapter, so test the adapter classes one by one; instanceof VcVirtualCdrom and the backing classes work. Add CD-ROM offers Client Device, Host Device and Datastore ISO file. Adding, editing and removing an HA override with reconfigureComputeResource_Task(spec, true) left the cluster’s other overrides unchanged.

How it works

Specs describe the change, tasks apply it. VcVirtualMachineConfigSpec and VcClusterConfigSpecEx hold only what you want to change. A device change says what to do (add, edit, remove) and to which device. vCenter applies the whole spec in one task, so a change to several devices succeeds or fails as one.

Modify, don’t replace. The second argument of reconfigureComputeResource_Task is modify. With true, vCenter merges your spec into the cluster’s configuration. With false, it would replace the configuration with your spec, and settings you didn’t include would go back to their defaults. On a VCF management cluster that means the overrides VCF set for its own appliances.

Check the class, not the family. In the vSphere API, a vmxnet3 adapter is a kind of VirtualEthernetCard. In Orchestrator’s scripting objects, instanceof only matches the exact class. Test for each adapter class you expect, as describeVmDevices does, or look at a property they share, such as macAddress.

What not to touch. Turning HA off or changing admission control on a VCF management cluster affects every VCF appliance on it, and VCF expects its own settings there. Change settings for your workload VMs, on workload clusters, and leave the management cluster’s HA configuration to VCF.

Back in vRO 5.5–7The Cookbook used the vSphere plug-in workflows of vRO 6 to add CD-ROMs, change HA settings and work with port groups, from the Java client.
In Orchestrator 9.xThe same library workflows are in the 9.x library under vCenter, and the same vSphere API objects for anything they don't cover. VCF sets HA overrides for its own appliances; leave those alone.

There's more

Enable or disable HA. The library has Enable HA on cluster and Disable HA on cluster in Cluster and Compute resource. Use them on workload clusters, in a maintenance window, never on the management cluster without a reason VCF supports.

See also