VMware Cloud Community
orian
Hot Shot
Hot Shot
Jump to solution

Remove network adapter

Hi,

I want to go over a list of virtual machines and remove their network adapter.

Is there a workflow that receives an array of virtual machines and removes the  network adapter?

If no, someone create a workflow\script which do it?

Thanks!

1 Solution

Accepted Solutions
iiliev
VMware Employee
VMware Employee
Jump to solution

Here is some sample code to remove network adapters from a single virtual machine (vm input parameter)

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

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

View solution in original post

9 Replies
AdilArif
Enthusiast
Enthusiast
Jump to solution

You could use the combination of Get-VM and Remove-networkAdapter cmdlets. First of you can create a list VMs in a csv file and use the below. Assuming the column header for the VMs is VMName.

$VMs = Import-Csv C:\VMs.csv

foreach ($VM in $VMs) {

Get-VM -Name $VM.VMName | Remove-NetworkAdapter

}

Get-VM - vSphere PowerCLI Cmdlets Reference

Remove-NetworkAdapter - vSphere PowerCLI Cmdlets Reference

Hope this helps.

Cheers, Adil Arif https://in.linkedin.com/pub/adil-arif/5b/a22/30 Blog: http://enterprisedaddy.com
0 Kudos
orian
Hot Shot
Hot Shot
Jump to solution

Hi,

Thanks, but these are powershell commands, that can be use only with invoke script.

Is there a java script that can be used through the vmware orchestrator?

0 Kudos
AdilArif
Enthusiast
Enthusiast
Jump to solution

No, I wouldn't be aware of that as I have no experience with Orchestrator. Somebody on the community should be able to help.

Cheers, Adil Arif https://in.linkedin.com/pub/adil-arif/5b/a22/30 Blog: http://enterprisedaddy.com
0 Kudos
iiliev
VMware Employee
VMware Employee
Jump to solution

Here is some sample code to remove network adapters from a single virtual machine (vm input parameter)

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

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

orian
Hot Shot
Hot Shot
Jump to solution

Wow, Thank you for the code!

Do you have a similar code for adding a network adapter?

0 Kudos
iiliev
VMware Employee
VMware Employee
Jump to solution

Here it is (you may want to experiment with deviceConfigSpec parameters eg. if you want to add different type of network adapter)

var vmConfigSpec = new VcVirtualMachineConfigSpec(); 

var deviceConfigSpec = new VcVirtualDeviceConfigSpec(); 

deviceConfigSpec.operation = VcVirtualDeviceConfigSpecOperation.add;

deviceConfigSpec.device = new VcVirtualE1000();

deviceConfigSpec.device.key = -100;

deviceConfigSpec.device.backing = new VcVirtualEthernetCardNetworkBackingInfo();

deviceConfigSpec.device.backing.deviceName = "VM Network";

deviceConfigSpec.device.connectable = new VcVirtualDeviceConnectInfo();

deviceConfigSpec.device.connectable.startConnected = true;

deviceConfigSpec.device.connectable.allowGuestControl = true;

deviceConfigSpec.device.connectable.connected = true;

deviceConfigSpec.device.controllerKey = 100;

deviceConfigSpec.device.addressType = "generated";

deviceConfigSpec.device.wakeOnLanEnabled = true;

vmConfigSpec.deviceChange = [deviceConfigSpec]; 

try { 

  myTask = vm.reconfigVM_Task(vmConfigSpec); 

} catch (ex) { 

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

orian
Hot Shot
Hot Shot
Jump to solution

Thank you again!

I think I will love this product.

Another question:

I'm familiar with powershell commands, but I'm a beginner with VMware orechestrator and java script

How can I find easily these classes that you use in the last scripts? I know there is a search box, but when I search "network" I receive a lot of attribute\methods\scripts - can you give me some tips for an easy start?

another question:

Is there an easy way to configure IP address, subnet, default gateway for the new network adapter?

for now, I use Invoke-vmscript command, which take a while because of the connection progress to the vcenter.

0 Kudos
orian
Hot Shot
Hot Shot
Jump to solution

Hi,

When I run the remove network code, I sometimes receive in the vSphere the following error:

Invalid configuration for device '1'.

What should I change in the code?

Thanks!

0 Kudos
anton749
Contributor
Contributor
Jump to solution

Hi, do you know after i add a NIC to a VM how can i identify the NIC and configure settings to it: IP, gateway, subnet mask, dns. I have a powershell script to do that in windows but still need to figure out how to tell the script which NIC to apply the config too. I could pass the MAC address but would need to find out which is the MAC of the new NIC, is there a better way using the vSphere API so that i can use it both in win and linux? thanks
0 Kudos