VMware Cloud Community
ghman
Enthusiast
Enthusiast
Jump to solution

Orchestrator modify PortGroup/Remove NICs

So I've just completed a workflow where I create a clone, add NICs to it with a specific PortGroup, modify the vCPU and RAM and then add hard drives to the new machine. Surprisingly this was fairly simple to complete. However, I'm left with two items that I'm finding harder to reconfigure.

1. The clone could have started with 1..n network interfaces already installed. I need to modify the first two NICs to be on a different Port Group. If I have a VC:DistributedVirtualPortGroup then how do I get a list of NICs on the VM and modify the portgroup of a specific NIC?

2. The cloned machine may have more NICs than I need based on some input I have. How do I get a list of the NICs on the VM and remove the ones I do not want?

I looked for a "remove NIC" workflow in the "Library" and "All Actions" but couldn't find one.

As usual any help is greatly appreciated.

Thank you.

0 Kudos
1 Solution

Accepted Solutions
iiliev
VMware Employee
VMware Employee
Jump to solution

Here is sample code to enumerate NICs in a VM and remove them:

var nicsToRemove = []; 

 

for each (var device in vm.config.hardware.device) {   

  if (device instanceof VcVirtualVmxnet   

   || device instanceof VcVirtualVmxnet2   

   || device instanceof VcVirtualVmxnet3 

   || device instanceof VcVirtualE1000   

   || device instanceof VcVirtualE1000e   

   || device instanceof VcVirtualPCNet32   

   || device instanceof VcVirtualSriovEthernetCard) { 

    // Here you can add some custom logic to decide whether to remove the network adapter 

    // This sample code will remove all network adapters 

      nicsToRemove.push(device);   

  } 

}   

 

var vmConfigSpec = new VcVirtualMachineConfigSpec();   

var deviceChanges = []; 

var deviceConfigSpec = new VcVirtualDeviceConfigSpec() ;   

 

for each (var nic in nicsToRemove) {   

  deviceConfigSpec.operation = VcVirtualDeviceConfigSpecOperation.remove;   

  deviceConfigSpec.device = nic;   

   

  deviceChanges.push(deviceConfigSpec);   

}   

   

vmConfigSpec.deviceChange = deviceChanges;   

   

try {   

  myTask = vm.reconfigVM_Task(vmConfigSpec);   

} catch (ex) {   

  System.warn("Error while reconfiguring VM: " + ex);   

}   

It removes all NICs; you need to add come code to check if the current NIC need to be removed around line 13, and don't add it to the array nicsToRemove if you decide to keep the NIC.

View solution in original post

0 Kudos
1 Reply
iiliev
VMware Employee
VMware Employee
Jump to solution

Here is sample code to enumerate NICs in a VM and remove them:

var nicsToRemove = []; 

 

for each (var device in vm.config.hardware.device) {   

  if (device instanceof VcVirtualVmxnet   

   || device instanceof VcVirtualVmxnet2   

   || device instanceof VcVirtualVmxnet3 

   || device instanceof VcVirtualE1000   

   || device instanceof VcVirtualE1000e   

   || device instanceof VcVirtualPCNet32   

   || device instanceof VcVirtualSriovEthernetCard) { 

    // Here you can add some custom logic to decide whether to remove the network adapter 

    // This sample code will remove all network adapters 

      nicsToRemove.push(device);   

  } 

}   

 

var vmConfigSpec = new VcVirtualMachineConfigSpec();   

var deviceChanges = []; 

var deviceConfigSpec = new VcVirtualDeviceConfigSpec() ;   

 

for each (var nic in nicsToRemove) {   

  deviceConfigSpec.operation = VcVirtualDeviceConfigSpecOperation.remove;   

  deviceConfigSpec.device = nic;   

   

  deviceChanges.push(deviceConfigSpec);   

}   

   

vmConfigSpec.deviceChange = deviceChanges;   

   

try {   

  myTask = vm.reconfigVM_Task(vmConfigSpec);   

} catch (ex) {   

  System.warn("Error while reconfiguring VM: " + ex);   

}   

It removes all NICs; you need to add come code to check if the current NIC need to be removed around line 13, and don't add it to the array nicsToRemove if you decide to keep the NIC.

0 Kudos