VMware Cloud Community
MartinDing
Contributor
Contributor

how to auto change portgroup of NIC in vSphere by API script

hi experts,

These days I want to write a workflow in VCO to auto change port group of NIC.(AS attachment pic shows)

For two given VM: SampleVM and DestVM. If they are in different port groups, I want to allocate DestVM to the port group of SampleVM automatically through workflow.

 

Scripts can be seen as follows:

 

var SampleDevs = SampleVM.config.Hardware.Device;

var DestDevs = DestVM.config.Hardware.Device;

var SampleNic = null;

var DestNic = null;

for (var i in  SampleDevs)

{

if( SampleDevs[i].deviceInfo.Label == "Network adapter 1" )

SampleNic =  SampleDevs[i]; //return sampleVM's network adapter

}

 

for (var i in  DestDevs)

{

if( DestDevs[i].deviceInfo.Label == "Network adapter 1" )

DestNic =  DestDevs[i];  //return DestVM's network adapter

}

var spec = new VcVirtualMachineConfigSpec();

//spec.changeVersion = DestVM.config.changeVersion;

spec.Version = DestVM.config.Version;

var nicDev = new VcVirtualDeviceConfigSpec();

nicDev.operation = "edit";

nicDev.device = DestNic;

nicDev.device.backing = SampleNic.backing;

nicDev.device.deviceInfo.summary = SampleNic.deviceInfo.sumarry;

spec.deviceChange += nivDev;

 

But it can not work. Who can tell me how to modify the script? Thanks sincerely.

 

Martin Ding from EMC

var SampleDevs = SampleVM.config.Hardware.Device;
var DestDevs = DestVM.config.Hardware.Device;
var SampleNic = null;
var DestNic = null;
for (var i in  SampleDevs)
{
if( SampleDevs[i].deviceInfo.Label == "Network adapter 1" )
SampleNic =  SampleDevs[i];
}
 
for (var i in  DestDevs)
{
if( DestDevs[i].deviceInfo.Label == "Network adapter 1" )
DestNic =  DestDevs[i];
}
 
var spec = new VcVirtualMachineConfigSpec();
//spec.changeVersion = DestVM.config.changeVersion;
spec.Version = DestVM.config.Version;
 
 
//spec.deviceChange = [new VcVirtualDeviceConfigSpec()];
////spec.deviceChange[0].operation = [new VcVirtualDeviceConfigSpecOperation()];
////spec.deviceChange[0].operation.fromString("edit");
//spec.deviceChange.operarion = "edit";
////System.log(spec.deviceChange[0].operation);
////spec.deviceChange.device = DestNic;
//System.log(SampleNic.deviceInfo.label);
//System.log(SampleNic.deviceInfo.summary);
//System.log(SampleNic);
//spec.deviceChange.device.backing = SampleNic.backing;
//spec.deviceChange.device.deviceInfo.summary = SampleNic.deviceInfo.sumarry;
//DestVM.reconfigVM_Task(spec);
 
var nicDev = new VcVirtualDeviceConfigSpec();
//System.log(nicDev);
nicDev.operation = "edit";
nicDev.device = DestNic;
nicDev.device.backing = SampleNic.backing;
nicDev.device.deviceInfo.summary = SampleNic.deviceInfo.sumarry;
spec.deviceChange += nivDev;
 
Reply
0 Kudos
2 Replies
Burke-
VMware Employee
VMware Employee

I have not tried your code  - which part is not working? The connection to the distributed virtual portgroup? Rather than coding the connection yourself, have you looked at the library action "connectVmNicNumberToVirtualDistributedPortgroup" - this action is called by a library workflow Connect virtual machine NIC number to distributed virtual port group. Once you've determined the destination portgroup and the destination VM NIC number, this action can be used to connect the vm to that portgroup.

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
Reply
0 Kudos
MartinDing
Contributor
Contributor

Perfect 

Thanks 

Reply
0 Kudos