VMware Cloud Community
schistad
Enthusiast
Enthusiast

Find vlan for portgroup?

Does anyone have a way of retrieving the vlan ID from a portgroup in vSphere 5.5?

For some reason it looks like there's no way to drill down from a host object to a portgroup object; at best you can get a string array containing the *names* of the portgroups, but so far I've found no way of retrieving the actual portgroup.spec property.

Since it looks like this used to work I've assumed that this is actually a recent bug.

EDIT: I should specify that I'm talking about traditional vSwitch portgroups here, not dvSwitch ones.

0 Kudos
2 Replies
Dan_Linsley
VMware Employee
VMware Employee

Check the VC:HostSystem.config.network.portgroup array.

This snippet I've used to get the VC:Network object for a VLAN number.  I look for all the portgroups on a host and find the first one that matches the VLAN number.  Returns the respective VC:Network object:

inputs:  host (VC:HostSystem), vlan (Number)

System.log("host: "+host.name);

var hPortgroups = host.config.network.portgroup;

var networks = host.network;

System.log("host portgroup count: "+hPortgroups.length);

for(var i=0; i<hPortgroups.length; i++) {

    if (hPortgroups[i].spec.vlanId == vlan) {

        portgroupName = hPortgroups[i].spec.name;

        System.log("Found possible portgroup "+portgroupName+" with vlan id: "+vlan);

        System.log("VM Portgroups found in cluster: "+networks.length);

        for (var j=0; j<networks.length;j++) {

            if (networks[j].name === portgroupName) {

                System.log("Found matching VM Portgroup");

                return networks[j];

            }

        }

    }

}

0 Kudos
Dan_Linsley
VMware Employee
VMware Employee

Looks like you may have already tried the path I provided though, I'll try to recreate it with 5.5

0 Kudos