Automation

 View Only
  • 1.  Save CDP to HTML for Mulitple VMHosts via PowerCLI

    Posted Jun 22, 2012 06:50 PM

    Hello,

    I've been trying some of the existing scripts found here to gather network switch port CDP information from pNICs on ESXi hosts.  These scripts assume only one host is queried or they connect to a vCenter Server to gather a VMHost list.  When I try connecting to multiple ESXi hosts I run into this error:

    Method invocation failed because [System.Object[]] doesn't contain a method named 'QueryNetworkHint'.

    Can a PowerCLI script perform the following task?

    I would like the script to start by connecting to multiple ESXi hosts that are not managed in vCenter.  The Get-PowerCLIConfiguration Default Server Mode is set to Multiple so that 'Get-VMHost' returns list of VMHosts.

    The script would then gather information from each pNIC on each host and export the results to HTML table format as shown in the attached .htm example.  I'm hoping for help with the section that gathers the information for multiple ESXi hosts.

    # Begin script

    Connect-VIServer esx1 -user user1 -password password1

    Connect-VIServer esx2 -user user1 -password password1

    Connect-VIServer esx3 -user user1 -password password1

    $filename = "C:\www\cdp_example.htm"

    ???

    # Example data

    $report = @()
    $row = "" | select VMHost, vSwitch, physNIC, DevID, PortID, VLANID, Speed, MAC

    $row.VMHost = "esx1.example.com"

    $row.vSwitch = "vSwitch0"

    $row.physNIC = "vmnic0"
    $row.DevID = "switch1.example.com"
    $row.PortID = "GigabitEthernet3/5"

    $row.VLANID = "101"

    $row.Speed = "1000"
    $row.MAC = "00:0c:a3:4b:ff:01"

    $report += $row

    $report | ConvertTo-Html -title "ESXi CDP Info" | Out-File $filename

    --

    Example HTML table output.

    <html>
    <head><style>body { background-color:#EEEEEE; }
    body,table,td,th { font-family:Tahoma; color:Black; Font-Size:10pt }
    th { font-weight:bold; background-color:#CCCCCC; }
    td { background-color:white; }</style></head>

    <body>

    <table>

    <tr><th>VMHost</th><th>vSwitch</th><th>physNIC</th><th>DevID</th><th>PortID</th><th>VLANID</th><th>Speed</th><th>Duplex</th><th>MAC</th></tr>

    <tr><td>esx1.example.com</td><td>vSwitch0</td><td>vmnic0</td><td>switch1.example.com</td><td>GigabitEthernet2/4</td><td>101</td><td>1000</td><td>Full</td><td>00:0c:a3:4b:ff:01</td></tr>

    <tr><td>esx1.example.com</td><td>vSwitch0</td><td>vmnic1</td><td>switch2.example.com</td><td>GigabitEthernet3/5</td><td>101</td><td>1000</td><td>Full</td><td>00:0c:a3:4b:ff:02</td></tr>

    <tr><td>esx2.example.com</td><td>vSwitch0</td><td>vmnic0</td><td>switch1.example.com</td><td>GigabitEthernet3/5</td><td>101</td><td>1000</td><td>Full</td><td>00:0c:a3:4b:ff:03</td></tr>

    <tr><td>esx2.example.com</td><td>vSwitch0</td><td>vmnic1</td><td>switch2.example.com</td><td>GigabitEthernet3/5</td><td>101</td><td>1000</td><td>Full</td><td>00:0c:a3:4b:ff:04</td></tr>

    </table>

    </body></html>

    --

    Thanks!



  • 2.  RE: Save CDP to HTML for Mulitple VMHosts via PowerCLI

    Posted Jun 22, 2012 08:46 PM

    Have a look at the script in Export Script to a .csv file.

    It produces more or less the information you want from the CDP info.

    Instead of sending the output to a CSV file, you''ll have to change that to produce HTML outout.



  • 3.  RE: Save CDP to HTML for Mulitple VMHosts via PowerCLI

    Posted Jun 22, 2012 09:36 PM

    Thanks for the reply!

    That script helps, however, it still reports the following error regarding the QueryNetworkHint method.  So the output file does not include switch device or PortID information.  Maybe I'm missing some library for QueryNetworkHint?  I'm using PowerCLI  version 5.0.1. build 581491.

    Method invocation failed because [System.Object[]] doesn't contain a method named 'QueryNetworkHint'.
    At C:\www\cdp2html.ps1:15 char:44
    +         $cdpInfo = $netSys.QueryNetworkHint <<<< ($pnic.Device)
        + CategoryInfo          : InvalidOperation: (QueryNetworkHint:String) [], RuntimeException
        + FullyQualifiedErrorId : MethodNotFound

    I was able to output to html using the ConvertTo-Html cmdlet.  So I'm getting very close.



  • 4.  RE: Save CDP to HTML for Mulitple VMHosts via PowerCLI

    Posted Jun 22, 2012 11:01 PM

    -Update-

    If I run the script while connected to only one ESX host, the QueryNetworkHint method works and the output includes the switch device and PortID detail from CDP.  If I connect to more than one ESX host, I get the error I posted in my previous post.

    Connect-VIServer esx1 -user user1 -password password1

    Connect-VIServer esx2 -user user1 -password password1

    $report = @()
    
    foreach($esxImpl in (Get-VMHost)){
        $esx = $esxImpl | Get-View
        $netSys = Get-View $esx.ConfigManager.NetworkSystem
        foreach($pnic in $esx.Config.Network.Pnic){
            $vSw = $esxImpl | Get-VirtualSwitch | where {$_.Nic -contains $pNic.Device}
            $pg = $esxImpl | Get-VirtualPortGroup | where {$_.VirtualSwitchName -eq $vSw.Name}
            $order = ($esx.Config.Network.Vswitch | where {$_.Name -eq $vSw.Name}).Spec.Policy.NicTeaming.NicOrder
            $cdpInfo = $netSys.QueryNetworkHint($pnic.Device)
            $report += $pnic | Select @{N="ESXname";E={$esxImpl.Name}},
            @{N="pNic";E={$pnic.Device}},
            @{N="Model";E={($esx.Hardware.PciDevice | where {$_.Id -eq $pnic.Pci}).DeviceName}},
            @{N="vSwitch";E={$vSw.Name}},
            @{N="Portgroups";E={$pg | %{$_.Name}}},
            @{N="Speed";E={$pnic.LinkSpeed.SpeedMb}},
            @{N="Status";E={if($pnic.LinkSpeed -ne $null){"up"}else{"down"}}},
            @{N="PCI Location";E={$pnic.Pci}},
            @{N="Active/stand-by/unassigned";E={if($order.ActiveNic -contains $pnic.Device){"active"}elseif($order.StandByNic -contains $pnic.Device){"standby"}else{"unused"}}},
            @{N="IP range";E={[string]::Join("/",@($cdpInfo[0].Subnet | %{$_.IpSubnet + "(" + $type + ")"}))}},
            @{N="Physical switch";E={&{if($cdpInfo[0].connectedSwitchPort){$cdpInfo[0].connectedSwitchPort.devId}else{"CDP not configured"}}}},
            @{N="PortID";E={&{if($cdpInfo[0].connectedSwitchPort){$cdpInfo[0].connectedSwitchPort.portId}else{"CDP not configured"}}}}
        }
    }
    $report | ConvertTo-Html | Out-File "C:\report.htm"



  • 5.  RE: Save CDP to HTML for Mulitple VMHosts via PowerCLI
    Best Answer

    Posted Jun 22, 2012 11:49 PM

    Try changing this line

    $netSys = Get-View $esx.ConfigManager.NetworkSystem

    into this

    $netSys = Get-View $esx.ConfigManager.NetworkSystem -Server $esx


  • 6.  RE: Save CDP to HTML for Mulitple VMHosts via PowerCLI

    Posted Sep 04, 2012 07:56 PM

    Thanks Luc.  That helped get me to a final working script.

    And I bought a copy of your PowerCLI book.



  • 7.  RE: Save CDP to HTML for Mulitple VMHosts via PowerCLI

    Posted Sep 04, 2012 08:47 PM

    Wow, thanks. Much appreciated