Listing allowed VLANs on uplinks (vmnic) of ESXi 5.1 hosts

ESXi 5.1 has the ability to show  allowed VLANs on  physical NICs (vmnic). By default this option is not enabled on NICs. We have to enable explicitly and gather the VLAN details. Please find the script below which will help you to enable the VLAN STATS on the NICs which have the LINK status UP.

[crayon lang=”powershell”]
# Script for listing the allowed VLANS on physcical NICs of the ESXi Host.
Connect-VIServer  <vCenter Server>

$HostName= <ESXi Host Name or IP>
$esxcli = Get-EsxCli -VMHost $HostName

$esxcli.network.nic.list() | Where-Object {$_.Link -eq”Up”} | %{
$vmnic_name = $_.Name
try
{
($esxcli.network.nic.vlan.stats.get($vmnic_name) | Select -expand vlanid) -join’,’
}
catch
{
Write-Host VLAN stats are disabled for $vmnic_name. Enabling it now..
$esxcli.network.nic.vlan.stats.set(“true”,$vmnic_name)

# I have observed there is a delay taken by host to generate the complete list of VLANs. So its preferred to put a minute of delay before fetching the details.

Start-Sleep-Seconds 60

($esxcli.network.nic.vlan.stats.get($vmnic_name) | Select -expand vlanid) -join’,’
}
}

Sample output:

VLAN stats are disabled for vmnic2 . Enabling it now..

true

0,10,20,21,22,113,114,115,123,848

VLAN stats are disabled for vmnic3 . Enabling it now..

true

0,10,20,21,22,113,115,123,848

[/crayon]