Create a CSV of Port Group Names and VLAN IDs for Both Standard and Distributed vswitches

Posted: July 19, 2013 in Networking, Scripts, VMware

I recently had to gather the VLAN ID information from a vCenter environment where some hosts had standard and distributed switching in use for their virtual machine networking. Unfortunately, the data for VLAN ID is located in different fields depending on the vSwitch type. So, here’s a simple script to gather both sets of into a CSV.

As always, be sure to set your vCenter name for the connection.

$VC = “YourvCenter”
Connect-VIServer $VC

$VLANInfo = foreach($cluster in Get-Cluster){

foreach($esx in (Get-VMHost -Location $cluster)){

foreach($pg in (Get-VirtualPortgroup -VMHost $esx -Standard | Where { $_.Name -NotMatch “-DVUplinks” })){
Select -InputObject $pg @{N=”Cluster”;E={$cluster.Name}},

foreach($pg in (Get-VirtualPortgroup -VMHost $esx -Distributed | Where { $_.Name -NotMatch “-DVUplinks” })){
Select -InputObject $pg @{N=”Cluster”;E={$cluster.Name}},

$VLANInfo | Select VMHost, PortGroup, VLAN | Export-CSV D:\Scripts\vSphere-VLANs.csv
Disconnect-VIServer -Force -Confirm:$False

  1. Luis V says:

    Getting an error when it tries the part of script where it will be dealing with the distributed switch:

    Get-VDPortgroup : Cannot bind parameter ‘VMHostNetworkAdapter’. Cannot convert
    the “” value of type
    “VMware.VimAutomation.ViCore.Impl.V1.Inventory.VMHostImpl” to type
    At line:9 char:41
    + foreach($pg in (Get-VDPortgroup -VMHost $esx -Distributed | Where { $_.Name
    -Not …
    + ~~~~
    + CategoryInfo : InvalidArgument: (:) [Get-VDPortgroup], Paramete
    + FullyQualifiedErrorId : CannotConvertArgumentNoMessage,VMware.VimAutomat

    Nice to know about these procedure though.. good job

  2. Brian Caulfield says:

    Great script!
    If i wanted to also know the host network adapter that was connected to a particular portgroup, how would that be included in the script?

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )


Connecting to %s