I managed to get all Port Groups in a Sandard switch but the list includes the Distributed Port Groups from the Distributed vSwitch that it is included inside the Standard switch structure. I don't want this in my case.

I am using VMware vSphere SDK (5.5) and I tried this one:

    public List<Network> GetPortGroups(VimClient vimClient, Datacenter selectedDC = null, string pgName = null)
    {
        List<Network> lstPortGroups = new List<Network>();
        NameValueCollection pgFilter = new NameValueCollection();
        ManagedObjectReference DcMoRef = new ManagedObjectReference();

        if (pgName != null)
        {
            pgFilter.Add("name", pgName);
        }
        else
        {
            pgFilter = null;
        }

        if (selectedDC != null)
        {
            DcMoRef = selectedDC.MoRef;
        }
        else
        {
            DcMoRef = null;
        }

        List<EntityViewBase> appPortGroups = vimClient.FindEntityViews(typeof(Network), DcMoRef, pgFilter, null);
        if (appPortGroups != null)
        {
            foreach (EntityViewBase appPortGroup in appPortGroups)
            {
                Network thisPortGroup = (Network)appPortGroup;
                lstPortGroups.Add(thisPortGroup);
            }
            return lstPortGroups;
        }
        else
        {
            return null;
        }
    }
1

There are 1 best solutions below

0
On
    public List<Network> GetStandardPgs(Datacenter selectedDC = null)
    {
        List<Network> lstPortGroups = new List<Network>();
        ManagedObjectReference DcMoRef = new ManagedObjectReference();

        if (selectedDC != null)
        {
            DcMoRef = selectedDC.MoRef;
        }
        else
        {
            DcMoRef = null;
        }
        List<EntityViewBase> appPortGroups = _vmwarecontext.FindEntityViews(typeof(Network), DcMoRef, null, null);
        if (appPortGroups != null)
        {
            foreach (EntityViewBase appPortGroup in appPortGroups.Where(x => x.GetType() == typeof(Network)))
            {
                lstPortGroups.Add((Network)appPortGroup);
            }
            return lstPortGroups;
        }
        else
        {
            return null;
        }
    }