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
with kind regards
malte
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.
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?
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.
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; ...
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
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);
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.
Thanks for all answers!
With AngelaS solution it works fine. :smileygrin:
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.
Jddias - you use the .name property of your VC:Network object.
Hah, figured it was simple! Thanks Burke.