VMware Cloud Community
orian
Hot Shot
Hot Shot
Jump to solution

Remove Network adapter - Error

Hi,

I use the following code in order to remove all the network adapters connected to specific virtual machine.

Sometime the workflow finishes with no error, but most of the times I receive the following error in the vCenter: Invalid configuration for device '1'.

What can I change in my code?

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 VcVirtualPCNet32   

   || device instanceof VcVirtualEthernetCard) {

    // 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() ;  

System.log(vm.name);

 

for each (var nic in nicsToRemove) {

  deviceConfigSpec.operation = VcVirtualDeviceConfigSpecOperation.remove;

  deviceConfigSpec.device = nic;

  System.log("The nic is: " + nic.connectable.status);

  vmConfigSpec.deviceChange = deviceChanges;

    if (nic.connectable.status == "ok"){

  deviceChanges.push(deviceConfigSpec);

  } 

}

try {   

  myTask = vm.reconfigVM_Task(vmConfigSpec);   

} catch (ex) {

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

}

Tags (1)
Reply
0 Kudos
1 Solution

Accepted Solutions
qc4vmware
Virtuoso
Virtuoso
Jump to solution

It looks like your code is flawed here:

for each (var nic in nicsToRemove) {

  deviceConfigSpec.operation = VcVirtualDeviceConfigSpecOperation.remove;

  deviceConfigSpec.device = nic;

  System.log("The nic is: " + nic.connectable.status);

  vmConfigSpec.deviceChange = deviceChanges;

    if (nic.connectable.status == "ok"){

  deviceChanges.push(deviceConfigSpec);

  }

}

I think it should be:

for each (var nic in nicsToRemove) {

  deviceConfigSpec.operation = VcVirtualDeviceConfigSpecOperation.remove;

  deviceConfigSpec.device = nic;

  System.log("The nic is: " + nic.connectable.status);

   if (nic.connectable.status == "ok"){

       deviceChanges.push(deviceConfigSpec);

  }

}

vmConfigSpec.deviceChange = deviceChanges;

You also probably want to break out of the task if deviceChanges.length <= 0 since there is no nic in the desired state.

View solution in original post

Reply
0 Kudos
1 Reply
qc4vmware
Virtuoso
Virtuoso
Jump to solution

It looks like your code is flawed here:

for each (var nic in nicsToRemove) {

  deviceConfigSpec.operation = VcVirtualDeviceConfigSpecOperation.remove;

  deviceConfigSpec.device = nic;

  System.log("The nic is: " + nic.connectable.status);

  vmConfigSpec.deviceChange = deviceChanges;

    if (nic.connectable.status == "ok"){

  deviceChanges.push(deviceConfigSpec);

  }

}

I think it should be:

for each (var nic in nicsToRemove) {

  deviceConfigSpec.operation = VcVirtualDeviceConfigSpecOperation.remove;

  deviceConfigSpec.device = nic;

  System.log("The nic is: " + nic.connectable.status);

   if (nic.connectable.status == "ok"){

       deviceChanges.push(deviceConfigSpec);

  }

}

vmConfigSpec.deviceChange = deviceChanges;

You also probably want to break out of the task if deviceChanges.length <= 0 since there is no nic in the desired state.

Reply
0 Kudos