Home > Blogs > VMware PowerCLI Blog


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.

12 thoughts on “How to find out what VLANs your ESX hosts can really see.

  1. ak

    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!!!

    Reply
  2. Alexander Thoma

    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

    Reply
  3. daniel

    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!

    Reply
  4. Carter Shanklin

    There was a problem with the script’s type name, it should work for you now.

    Reply
  5. CH

    I ran into this as well and added a post to my blog regarding this on the service console of the ESX host
    http://darusintegration.blogspot.com/2010_04_01_archive.html
    C

    Reply
  6. nick

    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?

    Reply
  7. Frank

    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

    Reply
    1. Frank

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

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

      Reply
    2. Fer

      thanks!!! great script, how can i replace the vmhostid with the esx servername?

      Reply
      1. Joe

        Just use $NicImpl.VMHost instead of $NicImpl.VMHostId

        Reply
  8. Yasin AKILLI

    Thanks Carter, For your share.

    Reply
  9. Basavaraj R Navalgund

    Hi Team,

    Thanks and this cmd is handy must be used in troubleshooting ip and vlan related issues.

    Regards
    Basavaraj.R navaglund
    banavalg@yahoo.com

    Reply

Leave a Reply

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

*