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}},
@{N=”VMHost”;E={$esx.Name}},
@{N=”Portgroup”;E={$pg.Name}},
@{N=”VLAN”;E={$pg.VlanId}}
}

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

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

Advertisements
Comments
  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 “xxxxxxxx.com” value of type
    “VMware.VimAutomation.ViCore.Impl.V1.Inventory.VMHostImpl” to type
    “VMware.VimAutomation.ViCore.Types.V1.Host.Networking.Nic.HostVirtualNic”.
    At line:9 char:41
    + foreach($pg in (Get-VDPortgroup -VMHost $esx -Distributed | Where { $_.Name
    -Not …
    + ~~~~
    + CategoryInfo : InvalidArgument: (:) [Get-VDPortgroup], Paramete
    rBindingException
    + FullyQualifiedErrorId : CannotConvertArgumentNoMessage,VMware.VimAutomat
    ion.Vds.Commands.Cmdlets.GetVDPortgroup

    Nice to know about these procedure though.. good job

Leave a Reply

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

WordPress.com Logo

You are commenting using your WordPress.com 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 )

Google+ photo

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

Connecting to %s