简体   繁体   中英

How to get the list of Port Groups in a Standard switch in an ESXi without Distributed Port Groups using VMware vSphere SDK

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;
        }
    }
    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;
        }
    }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM