Automation

 View Only
  • 1.  VLAN ID associated with vSwitch

    Posted Apr 18, 2012 06:42 PM

    Hi,

    I am trying to run the script for finding the Vlan ID associated with the ESXi Host so that i do not have to  see individual vswitch but its giving error.Any advise on this will be much appreciated.Output in CSV. Please find the attached script.

    Output should like

    1. # Example output:
    2. #Device                 VMHostId                IPSubnet                               VlanId
    3. #------                 --------                --------                               ------
    4. #vmnic1                 HostSystem-host-102     10.91.245.128-10.91...                   2907
    5. #vmnic1                 HostSystem-host-102     10.91.244.133-10.91...                   2905
    6. #vmnic1                 HostSystem-host-102     10.91.243.253-10.91...                   2903
    7. #vmnic1                 HostSystem-host-102     10.91.246.11-10.91....                   2908
    8. #vmnic1                 HostSystem-host-102     10.91.246.129-10.91...                    2909


    thanks

    vmguy

    [vSphere PowerCLI] C:\tmp> .\vlan-id.ps1
    Unable to find type [VMware.VimAutomation.Client20.Host.NIC.PhysicalNicImpl]: make sure that the assembly containing this type is loaded.
    At C:\tmp\vlan-id.ps1:4 char:73
    +                 [VMware.VimAutomation.Client20.Host.NIC.PhysicalNicImpl] <<<<
        + CategoryInfo          : InvalidOperation: (VMware.VimAutom...PhysicalNicImpl:String) [], RuntimeException
        + FullyQualifiedErrorId : TypeNotFound



  • 2.  RE: VLAN ID associated with vSwitch
    Best Answer

    Posted Apr 18, 2012 09:04 PM


  • 3.  RE: VLAN ID associated with vSwitch

    Posted Apr 18, 2012 09:45 PM

    LucD,

      I would like say to thanks to you and you are always helpful.But there are lot of thread in this which confused me but i got few of them.

    Let me know which one  i give a try for my purpose :smileyhappy:  from your script which you mentioned in the thread.

    thanks

    vmguy



  • 4.  RE: VLAN ID associated with vSwitch

    Posted Apr 19, 2012 05:18 AM

    The reason you are getting the error in Carter's script is because it was written for an older PowerCLI version.

    Since then we had the (in)famous typename changes.

    Carter's script needs a minor adaption to make it work in the current PowerCLI version. I changed the typename in the parameter declaration for the Nic parameter.

    I also changed the VMHostId to VMHost, I suspect a hostname is more informative instead of an internal vSphere ID for the host.

    function Get-ObservedIPRange {
      param(
        [Parameter(Mandatory=$true,ValueFromPipeline=$true,HelpMessage="Physical NIC from Get-VMHostNetworkAdapter")]
        [VMware.VimAutomation.ViCore.Impl.V1.Host.Networking.Nic.PhysicalNicImpl]
        $Nic
      )  
    process {     $hostView = Get-VMHost -Id $Nic.VMHostId | Get-View -Property ConfigManager
        $ns = Get-View $hostView.ConfigManager.NetworkSystem
       
    $hints = $ns.QueryNetworkHint($Nic.Name)     foreach ($hint in $hints) {       foreach ($subnet in $hint.subnet) {         $observed = New-Object -TypeName PSObject
            $observed | Add-Member -MemberType NoteProperty -Name Device -Value $Nic.Name
            $observed | Add-Member -MemberType NoteProperty -Name VMHost -Value $Nic.VMHost.Name
            $observed | Add-Member -MemberType NoteProperty -Name IPSubnet -Value $subnet.IPSubnet
            $observed | Add-Member -MemberType NoteProperty -Name VlanId -Value $subnet.VlanId
           
    Write-Output $observed
          }     }   } }
    Get-VMHost | Get-VMHostNetworkAdapter | where { $_.Name -eq "vmnic1" } | Get-ObservedIPRange


  • 5.  RE: VLAN ID associated with vSwitch

    Posted Apr 19, 2012 06:36 AM

    Thanks LucD,

            I will give a try in once in office.is it possible to get output for a particular host or single  host if i execute the script like below and need out put in csv format. As this script wil pullout for entire ESXi in VC right ?

    function Get-ObservedIPRange {
      param(
        [Parameter(Mandatory=$true,ValueFromPipeline=$true,HelpMessage="Physical NIC from Get-VMHostNetworkAdapter")]
        [VMware.VimAutomation.ViCore.Impl.V1.Host.Networking.Nic.PhysicalNicImpl]
        $Nic
      )   process {     $hostView = Get-VMHost -Id $Nic.VMHostId | Get-View -Property ConfigManager
        $ns = Get-View $hostView.ConfigManager.NetworkSystem
        $hints = $ns.QueryNetworkHint($Nic.Name)     foreach ($hint in $hints) {       foreach ($subnet in $hint.subnet) {         $observed = New-Object -TypeName PSObject
            $observed | Add-Member -MemberType NoteProperty -Name Device -Value $Nic.Name
            $observed | Add-Member -MemberType NoteProperty -Name VMHost -Value $Nic.VMHost.Name
            $observed | Add-Member -MemberType NoteProperty -Name IPSubnet -Value $subnet.IPSubnet
            $observed | Add-Member -MemberType NoteProperty -Name VlanId -Value $subnet.VlanId
            Write-Output $observed
          }     }   } } Get-VMHost esx01.v1world.com | Get-VMHostNetworkAdapter | where { $_.Name -eq "vmnic1" } | Get-ObservedIPRange

    thanks

    vmguy



  • 6.  RE: VLAN ID associated with vSwitch

    Posted Apr 19, 2012 06:53 AM

    That is correct. And to save the result to a CSV just pipe the object to the Export-Csv cmdlet

    The last line would be something like this

    Get-VMHost MyEsx | Get-VMHostNetworkAdapter | where { $_.Name -eq "vmnic1" } | Get-ObservedIPRange | 
    Export-Csv
    .\report.csv -NoTypeInformation -UseCulture