8 Replies Latest reply on Jan 7, 2016 1:25 PM by PoshTechGuyTJ

    CISCO DISCOVERY PROTOCOL

    CarlosDionizio Novice

      Hi Friends!

      I need to read a content hosts using "get-content" but there is something wrong in script below , and i need to select only ethernet adapter!  can anyone help me ? thank you


      Write "Gathering VMHost objects" 

       

      $vmhosts = Get-Content C:\Users\c1217635\Documents\WindowsPowerShell\scripts\hostlist.txt

      #$vmhosts = Get-VMHost | Sort Name | Where-Object {$_.State -eq "Connected"} | Get-View 

      $MyCol = @() 

      foreach ($vmhost in $vmhosts){ 

      $ESXHost = $vmhost.Name 

      Write "Collating information for $ESXHost" 

      $networkSystem = Get-view $vmhost.ConfigManager.NetworkSystem 

      foreach($pnic in $networkSystem.NetworkConfig.Pnic){ 

           $pnicInfo = $networkSystem.QueryNetworkHint($pnic.Device) 

           foreach($Hint in $pnicInfo){     

               $NetworkInfo = "" | select-Object Host, PNic, Speed, MAC, DeviceID, PortID, Observed, VLAN 

               $NetworkInfo.Host = $vmhost.Name 

               $NetworkInfo.PNic = $Hint.Device 

               $NetworkInfo.DeviceID = $Hint.connectedSwitchPort.DevId 

               $NetworkInfo.PortID = $Hint.connectedSwitchPort.PortId 

               $record = 0 

               Do{ 

                   If ($Hint.Device -eq $vmhost.Config.Network.Pnic[$record].Device){ 

                       $NetworkInfo.Speed = $vmhost.Config.Network.Pnic[$record].LinkSpeed.SpeedMb 

                       $NetworkInfo.MAC = $vmhost.Config.Network.Pnic[$record].Mac 

                   } 

                   $record ++ 

               } 

               Until ($record -eq ($vmhost.Config.Network.Pnic.Length)) 

               foreach ($obs in $Hint.Subnet){ 

                   $NetworkInfo.Observed += $obs.IpSubnet + " " 

                   Foreach ($VLAN in $obs.VlanId){ 

                       If ($VLAN -eq $null){ 

                       } 

                       Else{ 

                           $strVLAN = $VLAN.ToString() 

                           $NetworkInfo.VLAN += $strVLAN + " " 

                       } 

                   } 

               } 

               $MyCol += $NetworkInfo 

           } 

      $Mycol | Sort Host| ConvertTo-HTML -head $a -body $Rodada | Out-File C:\Users\Documents\WindowsPowerShell\scripts\busca-VMNIC3.html

        • 1. Re: CISCO DISCOVERY PROTOCOL
          LucD Guru
          Community WarriorsvExpertUser Moderators

          It would you help if you could indicate what is wrong with the script.

          And perhaps show what output you would like to gather in the CSV.

          Blog: http://lucd.info | Twitter: @LucD22 | PowerCLI Reference co-author: http://tinyurl.com/hkn4glz
          • 2. Re: CISCO DISCOVERY PROTOCOL
            CarlosDionizio Novice

            Hi LucD,

             

            I Received .html with empty content.

            In output: Host,PNic,Speed,MAC,DeviceID,PortID (Only Ethernet),Observed,VLAN

            • 3. Re: CISCO DISCOVERY PROTOCOL
              LucD Guru
              User ModeratorsvExpertCommunity Warriors

              First thing to check, the script seems to use an input file that is a CSV with a Name column.

              While your version does a Get-Content.

               

              What is the content of the input file ?

              Blog: http://lucd.info | Twitter: @LucD22 | PowerCLI Reference co-author: http://tinyurl.com/hkn4glz
              • 4. Re: CISCO DISCOVERY PROTOCOL
                CarlosDionizio Novice

                the content is my ESXi hosts.

                • 5. Re: CISCO DISCOVERY PROTOCOL
                  LucD Guru
                  User ModeratorsCommunity WarriorsvExpert

                  Yes, but what is the layout of the file.

                  Can you post a sample of the content ?

                  Blog: http://lucd.info | Twitter: @LucD22 | PowerCLI Reference co-author: http://tinyurl.com/hkn4glz
                  • 6. Re: CISCO DISCOVERY PROTOCOL
                    CarlosDionizio Novice

                    Ih friend !

                     

                    1 - Attached the layout .html I need, and i need too select only ethernet adapter!

                    2 -  In hostlist.txt i have "esxihost079.domain.br"

                    tks for your help my friend!

                     

                    that's it...

                    • 7. Re: CISCO DISCOVERY PROTOCOL
                      LucD Guru
                      User ModeratorsvExpertCommunity Warriors

                      The hostlist.txt file might be the cause of the problem.

                      In the script you reference $vmhost.Name, but there is no Name column in that file.

                       

                      I would suggest to change the file to a CSV file with a layout like this

                      "Name"

                      "esxihost079.domain.br"

                      "esxihost080.domain.br"

                      Then use an Import-Csv cmdlet to read the file, instead of the Get-Content.

                      Blog: http://lucd.info | Twitter: @LucD22 | PowerCLI Reference co-author: http://tinyurl.com/hkn4glz
                      • 8. Re: CISCO DISCOVERY PROTOCOL
                        PoshTechGuyTJ Novice

                        I took this script and turned it into a function. Here is my function. It will error if your interfaces that are connected do not get CDP info. You can then take your file    get-content <filename> | %  { get-VMHostCDPInfo -VMHost ( get-vmhost $_) }

                         

                        If you are getting errors make sure you have the name exactly as it is in vcenter. I found building it as a function helped make sure I had all good data before I pumped it into my cmdlet.

                         

                        You can assign that to a variable and then pipe it to a CSV. $VARYOUBUILD | Export-CSV -Path <Filepath/filename.csv> -NoTypeInformation

                         

                         

                        function Get-VMHostCDPInfo

                        {

                            [CmdletBinding(DefaultParametersetName="VMHost")]

                            Param(

                                [Parameter(ValueFromPipelineByPropertyName=$true,Mandatory=$true,Position=0,ParameterSetName="VMHost")]

                                [VMware.VimAutomation.ViCore.Impl.V1.Inventory.VMHostImpl[]]$VMHost

                            )

                         

                         

                            BEGIN

                            {

                                $ESXHosts = @()

                            }

                            PROCESS

                            {

                                foreach ($ESXHost in $VMHost)

                                {

                                    $VSwitches = Get-VirtualSwitch -VMHost $ESXHost -Standard

                                    $VMHostView = Get-View $ESXHost.ID

                                    foreach ($View in $VMHostView)

                                    {

                                        $esxname = $View.Name

                                        $VMHostAdapters = Get-View $View.ConfigManager.NetworkSystem

                                        foreach ($HostAdapter in $VMHostAdapters)

                                        {

                                            foreach($physnic in $HostAdapter.NetworkInfo.Pnic)

                                            {

                                                $pnicInfo = $VMHostAdapters.QueryNetworkHint($physnic.Device)

                                                foreach($hint in $pnicInfo)

                                                {

                                                    if ($physnic.LinkSpeed)

                                                    {

                                                        $hint.ConnectedSwitchPort | Add-Member -MemberType NoteProperty -Name ComputerName -Value $esxname

                                                        $hint.ConnectedSwitchPort | Add-Member -MemberType NoteProperty -Name Adapter -Value $physnic.Device

                                                    }

                                                    $Hash = [ordered]@{ 'Computername'= $esxname;

                                                                        'Adapter'= $physnic.Device;

                                                                        'vSwitch' = ($VSwitches | where Nic -match "$($physnic.device)");

                                                                        'Switch' = $hint.ConnectedSwitchPort.DevID;

                                                                        'SwitchPort' = $hint.ConnectedSwitchPort.PortId;

                                                                        'CDPInfo'= $hint.ConnectedSwitchPort}

                                                    New-Object -TypeName PSObject -Property $Hash

                                                  

                                                }                    

                                            }

                                        }

                                    }

                                }

                            }

                            END

                            {

                          

                            }

                        }