WilliamLam.com

  • About
    • About
    • Privacy
  • VMware Cloud Foundation
  • VKS
  • Homelab
    • Resources
    • Nested Virtualization
  • VMware Nostalgia
  • Apple
You are here: Home / Automation / Quick Tip - Retrieving vSphere Distributed Switch (VDS) DVPort ID & Stats using PowerCLI

Quick Tip - Retrieving vSphere Distributed Switch (VDS) DVPort ID & Stats using PowerCLI

07.14.2021 by William Lam // Leave a Comment

I have seen several variations of this question get asked internally on how how to retrieve the DVPort ID and/or Stats on a vSphere Distributed Switch (VDS). Usually the question is prefaced with an example output from an ESXi host like the one show below using a classic CLI called esxcfg-vswitch. As you can see, there are a number of DVPort IDs which are either mapped to a physical NIC on the ESXi host or to a specific VM and its network adapter, if there is more than one.


My usual response for these sort of questions is that yes, it can be programmatically and automatically retrieved without going directly to an ESXi host. The answer is by using the vSphere API and specifically the set of methods provided by the VirtualDistributedSwitch managed object, which will allow users to retrieve all things related to the VDS.

Note: Although PowerCLI does provide some higher level cmdlets for managing VDS and Distributed Virtual Portgroups (DVPG), not everything that is available in VDS API is available through these higher level cmdlets, but that does not mean you can not use PowerCLI to easily retrieve all this additional information.

Ports

To retrieve the basic DVPort ID and Client information, you can use the FetchDVPorts() method which is available when getting a reference to a specific VDS. In the example PowerCLI snippet below, I am fetching all ports by providing the $null parameter, which does not filter out specific ports that I am interested in. After that, the key and state.runtimeinfo.linkpeer property contains the two pieces of information that allows us to build an output similar to that shown from esxcfg-vswitch. There is a ton of rich information for each DVPort, I highly recommend taking a look at the vSphere API documentation for DVPort to explore what else can be retrieved.

$vds = Get-VDSwitch -Name VDS

$ports = $vds.ExtensionData.FetchDVPorts($null)

$results = @()
foreach ($port in $ports | Sort-Object -Property key) {
    $tmp = [pscustomobject] @{
        DVPortID = $port.key;
        Client = $port.state.RuntimeInfo.LinkPeer
    }
    $results += $tmp
}

$results

Here is an example screenshot of the output that is expected:

Stats

For each DVPort, there are also a number of statistics that can be retrieved by looking at the state.stats property. Building on our example from above, I have decided to include two additional fields with the following metrics: BytesInUnicast and PacketsInBroadcast. To demonstrate this, I have updated the PowerCLI example from above to include these additional metrics as part of the output.

$vds = Get-VDSwitch -Name VDS

$ports = $vds.ExtensionData.FetchDVPorts($null)
foreach ($port in $ports | Sort-Object -Property key) {
    $tmp = [pscustomobject] @{
        DVPortID = $port.key;
        Client = $port.State.RuntimeInfo.LinkPeer
        BytesInUnicast = $port.state.stats.BytesInUnicast
        BytesOutUnicast = $port.state.stats.PacketsInBroadcast
    }
    $results += $tmp
}

$results

Here is an example screenshot of the output that is expected:


Lastly, all of this information can also be viewed using the vSphere UI under the Ports tab and this should give you an idea of all the different pieces of information that can be retrieved by using the vSphere API

More from my site

  • Retrieving statistics for a Distributed Virtual Port using the vSphere API & PowerCLI
  • Automate the reverse, migrating from vSphere Distributed Switch to Virtual Standard Switch using PowerCLI 5.5
  • Automate the migration from Virtual Standard Switch to vSphere Distributed Switch using PowerCLI 5.5
  • How to manually clean up a Distributed Virtual Switch (VDS) on an ESXi host?
  • Moving ESXi hosts with LACP/LAG between vCenter Servers?

Categories // Automation, PowerCLI, vSphere Tags // distributed virtual switch, PowerCLI, vds

Thanks for the comment!Cancel reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Search

Thank Author

Author

William is Distinguished Platform Engineering Architect in the VMware Cloud Foundation (VCF) Division at Broadcom. His primary focus is helping customers and partners build, run and operate a modern Private Cloud using the VMware Cloud Foundation (VCF) platform.

Connect

  • Bluesky
  • Email
  • GitHub
  • LinkedIn
  • Mastodon
  • Reddit
  • RSS
  • Twitter
  • Vimeo

Recent

  • Programmatically accessing the Broadcom Compatibility Guide (BCG) 05/06/2025
  • Quick Tip - Validating Broadcom Download Token  05/01/2025
  • Supported chipsets for the USB Network Native Driver for ESXi Fling 04/23/2025
  • vCenter Identity Federation with Authelia 04/16/2025
  • vCenter Server Identity Federation with Kanidm 04/10/2025

Advertisment

Privacy & Cookies: This site uses cookies. By continuing to use this website, you agree to their use.
To find out more, including how to control cookies, see here: Cookie Policy

Copyright WilliamLam.com © 2025

 

Loading Comments...