Uncategorized

How to find out what VLANs your ESX hosts can really see.

Recently someone asked me if there was a way, programmatically, to figure out the “observed IP ranges” and “observed VLANs’” that you can see in vSphere Client. Don’t know what I mean? Here’s a picture to help you out.

vlans

The data is available as a tooltip in the network configuration tab (naturally!) when you hover over Observed IP Ranges. How do you get this stuff out of the API.

Turns out there’s a rather obscure little function called QueryNetworkHint. This function takes the name of the physical NIC as input, which makes it perfect for pairing with our Get-VMHostNetworkAdapter cmdlet. Here’s a PowerShell advanced function that does exactly that, followed by some screenshots of it in action.

 

Let’s have a look at that in action. First some basic usage, combining Get-VMHost, Get-VMHostNetworkAdapter and the Get-ObservedIPRange advanced function.

blog1

Comparing these to the screenshot above you’ll see they’re the same. Let’s take it a step further and suppose we want to determine if a certain adapter can see a certain VLAN, let’s say 2903. Here’s some code that does it:

blog2

This function can really help you out if you use sophisticated networking to your ESX hosts but don’t control the switches directly, so you have to coordinate with a member of the networking team.

Update 1: As a reader kindly pointed out, this will only show VLANs for which the ESX server has seen actual traffic, it will not probe the switch for configuration.

Update 2: The code sample has been updated to fix the type name in the advanced function. The old function used a type name that only worked in internal builds of PowerCLI.

Comments

12 comments have been added so far

  1. Oh AWESOME!!! We have been looking for something like this for a long time. We have an environment with 30+ VLANS and 30+ hosts and everytime we add a VLAN, I have no idea (nor any easy way to test) whether they have been added correctly!
    Now we just need a good way to manage Distributed Virtual Switches with Powershell 🙂
    THANK YOU!!!

  2. Hi All,
    this does only work if the ESX has already seen packets from these networks. For freshly added networks / VLANS it does not indicate proper config nor operation.
    My two cents,
    Alexander

  3. Great script, however it stops at line 4 saying “Unable to find type [Vmware.VimAutomation […] make sure that the assembly containing this type is loaded”. I’m running PowerShell2 and PowerCLI 4.0.1 on WinXP 32-bit and as far as I can tell the VimAutomation snapin is already loaded. Any ideas much appreciated!

  4. I still get this error message: Unable to find type [VMware.VimAutomation.Client20.Host.NIC.PhysicalNicImpl]: make sure that the assembly containing this type is loaded.
    Any thoughts?

  5. Great Script. Very useful . I took the script and modified it slightly so that it can take input from pipeline and iterate through each available physical nic on the host :-).

    function Get-ObservedIPRange {

    # Credit: http://poshcode.org/
    [CmdletBinding()]
    param(
    [Parameter(Mandatory=$true,ValueFromPipeline=$true,ValueFromPipelineByPropertyName=$True,HelpMessage=”Physical NIC from Get-VMHostNetworkAdapter”)]
    [Object[]]$Nics

    )

    process {

    forEach ($Nic in $Nics)
    {

    if($Nic -notlike “vmk*”)
    {
    [VMware.VimAutomation.Client20.Host.NIC.PhysicalNicImpl]$NicImpl = $Nic
    $hostView = Get-VMHost -Id $NicImpl.VMHostId | Get-View -Property ConfigManager
    $ns = Get-View $hostView.ConfigManager.NetworkSystem
    $hints = $ns.QueryNetworkHint($NicImpl.Name)

    foreach ($hint in $hints)
    {
    if( ($hint.ConnectedSwitchPort) )
    {
    foreach ($subnet in $hint.subnet)
    {
    $observed = New-Object -TypeName PSObject
    $observed | Add-Member -MemberType NoteProperty -Name Device -Value $NicImpl.Name
    $observed | Add-Member -MemberType NoteProperty -Name VMHostId -Value $NicImpl.VMHostId
    $observed | Add-Member -MemberType NoteProperty -Name IPSubnet -Value $subnet.IPSubnet
    $observed | Add-Member -MemberType NoteProperty -Name VlanId -Value $subnet.VlanId
    Write-Output $observed
    }
    }
    }

    }

    }
    }

    # Example use:

    # Get-VMHost esx01a.vmworld.com | Get-VMHostNetworkAdapter | | Get-ObservedIPRange

    1. Correction: Example should read:
      Get-VMHost esx01a.vmworld.com | Get-VMHostNetworkAdapter | Get-ObservedIPRange

      (Typo: Example had an extra “|” symbol.)

Leave a Reply

Your email address will not be published. Required fields are marked *