VMware Cloud Community
wurzelwaldi
Contributor
Contributor
Jump to solution

Change network link on virtual nic

Hallo,

since a few days i try to get a script working in orchestrator.

here my script:

System.log("Start SetNics");

// virtualDeviceConfigSpec1 = new VcVirtualDeviceConfigSpec();

virtualDeviceConfigSpec1 = System.getModule("com.vmware.library.vc.vm.spec.config").getVirtualDeviceConfigSpec(nic1,null,VcVirtualDeviceConfigSpecOperation.edit)

// virtualDeviceConfigSpec1.device = nic1;

Server.log("Set NIC2 ("virtualDeviceConfigSpec1.device.deviceInfo.label") to ConfigSprec");

virtualDeviceConfigSpec1.device.backing = new VcVirtualEthernetCardNetworkBackingInfo();

virtualDeviceConfigSpec1.device.backing.network = network1.reference;

Server.log("Set Network1 to backing");

// virtualDeviceConfigSpec1.operation = VcVirtualDeviceConfigSpecOperation.edit;

var virtualMachineConfigSpec = new VcVirtualMachineConfigSpec();

virtualMachineConfigSpec.deviceChange = new Array();

virtualMachineConfigSpec.deviceChange.push(virtualDeviceConfigSpec1);

tempVm.reconfigVM_Task(virtualMachineConfigSpec);

the script should change the network a nic is connected to.

the variables are:

tempVm = VirtualMachine

network1 = Network

nic1 = VcVirtualPCNet32

i hope someone could help Smiley Happy

with kind regards

malte

1 Solution

Accepted Solutions
admin
Immortal
Immortal
Jump to solution

When you get the VirtualDeviceConfigSpec object, from the getVirtualDeviceConfigSpec() method, the method returns the blank template for the VirtualDeviceConfigSpec. To use this spec in your case, you also need to specify which virtual device to change i.e. you need to set the virtualDeviceConfigSpec1.device property in order to change the network link for a the network device of a virtual machine. For doing the same, please refer the following steps:

  • 1. Retreive all the available virtual devices of the virtual machine using the vm.config.hardware.device property

  • 2. Fetch the appropriate network device for which the network link needs to be changed

  • 3. Assign this fetched device to the device property of VirtualDeviceConfigSpec. Here you can either use getvirtualdevicespec() method or create a new virtualdeviceconfigspec object.

Kindly refer to the below code snippet to change the network for a VM.

Here, we need to provide three input parameters, namely the virtual machine whose network needs to be changed, the name of the existing network and the name of the new network.

// existNetwork string variable holds the name of the existing network

// newNetwork string variable holds the name of the new network

var vmspec = new VcVirtualMachineConfigSpec();

var device = new VcVirtualPCNet32();

var devicearr = new Array();

var nic = new VcVirtualEthernetCardNetworkBackingInfo();

nic.deviceName = newNetwork;

var devicespec = new VcVirtualDeviceConfigSpec();

var devices = vm.config.hardware.device;

for( var i in devices)

{

if(devices[i] instanceof VcVirtualPCNet32)

{

if (devices[i].deviceInfo.summary == existNetwork)

{

devicespec.device = devices[i];

devicespec.operation = VcVirtualDeviceConfigSpecOperation.edit;

devicespec.device.backing = nic;

devicearr[0] = devicespec;

}

}

}

vmspec.deviceChange = devicearr;

task = vm.reconfigVM_Task(vmspec);

I hope this helps.

View solution in original post

0 Kudos
11 Replies
admin
Immortal
Immortal
Jump to solution

Juste two things I see:

- virtualDeviceConfigSpec1.device.backing.network = network1.reference; If I remember correctly, the backing network could only be set using the networkLabel (string) property, not by the reference.

- try replacing virtualMachineConfigSpec.deviceChange.push(virtualDeviceConfigSpec1); by virtualMachineConfigSpec.deviceChange[0] = virtualDeviceConfigSpec1; (I don't remember which one is working always and which one is having some problems sometimes.

Do you have any error message? What happened to the task?

0 Kudos
wurzelwaldi
Contributor
Contributor
Jump to solution

1. virtualDeviceConfigSpec1.device.backing.network = network1.virtualDeviceConfigSpec2.device = nic2;; If I remember correctly, the backing network could only be set using the networkLabel (string) property, not by the reference.

Working fine but like the method with push nothings happens

2. try replacing virtualMachineConfigSpec.deviceChange.push(virtualDeviceConfigSpec1); by virtualMachineConfigSpec.deviceChange[0] = virtualDeviceConfigSpec1; (I don't remember which one is working always and which one is having some problems sometimes.

New Code:

virtualDeviceConfigSpec2.device.backing.network = network2.name;

Cannot convert TestNetwork to com.vmware.vim.vi4.ManagedObjectReference (Workflow:VMC / SetNics (item23)#17)

My Problem is, I can see the reconfigure task in virtual center but nothing happens.

Malte

PS: I know from my tests in the past that there is a property in c# called "operationSpecified". By setting this true, the reconfigVM_Task knows that modifications on the device were made.

0 Kudos
admin
Immortal
Immortal
Jump to solution

Look at action com.vmware.library.vc.vm.spec.config.device.backing.device.getVirtualEthernetCardNetworkBackingInfo..

with network as a VC:Network

var backing = new VcVirtualEthernetCardNetworkBackingInfo();
backing.deviceName = network.name;
...

0 Kudos
wurzelwaldi
Contributor
Contributor
Jump to solution

Thank you for the answer,

but the problem could not be solved.

Here is my new code:

virtualDeviceConfigSpec1 = System.getModule("com.vmware.library.vc.vm.spec.config").getVirtualDeviceConfigSpec(nic1,null,VcVirtualDeviceConfigSpecOperation.edit)

backing1 = new VcVirtualEthernetCardNetworkBackingInfo();

backing1.deviceName = network1.name;

virtualDeviceConfigSpec1.device.backing = backing1;

var virtualMachineConfigSpec = new VcVirtualMachineConfigSpec();

virtualMachineConfigSpec.deviceChange = new Array();

virtualMachineConfigSpec.deviceChange[0]=virtualDeviceConfigSpec1;

tempVm.reconfigVM_Task(virtualMachineConfigSpec);

the problem is the same. I can se the task in vCenter but nothing happens when it's finished.

malte

0 Kudos
admin
Immortal
Immortal
Jump to solution

Maybe you should try to revert back to virtualMachineConfigSpec.deviceChange.push(virtualDeviceConfigSpec1);

You must specifiy the VirtualDeviceConfigSpec.operation to be able to update something.

The following code is extraced from on of the Clone Workflows:

var deviceChange = new Array();
var change = new VcVirtualDeviceConfigSpec();
change.device = virtualEthernetCard;
change.operation = VcVirtualDeviceConfigSpecOperation.edit;
deviceChange.push(change);

0 Kudos
admin
Immortal
Immortal
Jump to solution

When you get the VirtualDeviceConfigSpec object, from the getVirtualDeviceConfigSpec() method, the method returns the blank template for the VirtualDeviceConfigSpec. To use this spec in your case, you also need to specify which virtual device to change i.e. you need to set the virtualDeviceConfigSpec1.device property in order to change the network link for a the network device of a virtual machine. For doing the same, please refer the following steps:

  • 1. Retreive all the available virtual devices of the virtual machine using the vm.config.hardware.device property

  • 2. Fetch the appropriate network device for which the network link needs to be changed

  • 3. Assign this fetched device to the device property of VirtualDeviceConfigSpec. Here you can either use getvirtualdevicespec() method or create a new virtualdeviceconfigspec object.

Kindly refer to the below code snippet to change the network for a VM.

Here, we need to provide three input parameters, namely the virtual machine whose network needs to be changed, the name of the existing network and the name of the new network.

// existNetwork string variable holds the name of the existing network

// newNetwork string variable holds the name of the new network

var vmspec = new VcVirtualMachineConfigSpec();

var device = new VcVirtualPCNet32();

var devicearr = new Array();

var nic = new VcVirtualEthernetCardNetworkBackingInfo();

nic.deviceName = newNetwork;

var devicespec = new VcVirtualDeviceConfigSpec();

var devices = vm.config.hardware.device;

for( var i in devices)

{

if(devices[i] instanceof VcVirtualPCNet32)

{

if (devices[i].deviceInfo.summary == existNetwork)

{

devicespec.device = devices[i];

devicespec.operation = VcVirtualDeviceConfigSpecOperation.edit;

devicespec.device.backing = nic;

devicearr[0] = devicespec;

}

}

}

vmspec.deviceChange = devicearr;

task = vm.reconfigVM_Task(vmspec);

I hope this helps.

0 Kudos
wurzelwaldi
Contributor
Contributor
Jump to solution

Thanks for all answers!

With AngelaS solution it works fine. :smileygrin:

0 Kudos
ber05
Contributor
Contributor
Jump to solution

modifications of AngelaS code works fine for me (Orchestrator 4.1)

see attached file (change_networks.js)...

jddias
VMware Employee
VMware Employee
Jump to solution

Since the backing network var is a string, how can I convert from VC:Network so that the workflow will allow the user to select from available networks?  I know that must be simple but can't find any hints on how to do that.

Visit my blog for vCloud Management tips and tricks - http://www.storagegumbo.com
0 Kudos
Burke-
VMware Employee
VMware Employee
Jump to solution

Jddias - you use the .name property of your VC:Network object.

If my answer resolved or helped you, please mark it as Correct or Helpful to award points. Thank you!

Visit http://www.vcoteam.info & http://blogs.vmware.com/orchestrator
for vRealize Orchestrator tips and tutorials - @TechnicalValues on Twitter
0 Kudos
jddias
VMware Employee
VMware Employee
Jump to solution

Hah, figured it was simple!  Thanks Burke.

Visit my blog for vCloud Management tips and tricks - http://www.storagegumbo.com
0 Kudos