Script to Gather CDP

                              Script to Gather CDP 


This script helps VMware admins to gather the CDP report along with the allowed VLANs on each interface(vmnic)

Prerequisite:-

1. Supported VMware PowerCLI Version
2. CDP should be enabled on Network Switches.

How To:-

1. Run the script in VMware Powercli 
2. It will prompt for the VC name against which it needs to be run, an Esxi name can also be           
    provided, if it needs to be run against only one Esxi followed by the credentials.
3. Script is tested and safe to run.
4. Result will be saved on the C drive, if you need to save it somewhere else then please modify the last line of the script with the desired path.
5. Script will take some time during execution.
6. Out-put of the script will be saved in a csv format.



Code:-


$vcs=read-host provide the vCenter Name
$info=@()
foreach($vc in $vcs)
{
write-host -foregroundcolor green "Connecting to VC/ESXi" $VC
get-vc $vc 
write-host "collecting host data"
$hsts=get-vmhost *
   foreach($hst in $hsts)
  {
   write-host -foregroundcolor Yellow "Connecting to $hst"
   $nicstat=get-esxcli -vmhost $hst
   $nicinfo=$nicstat.network.nic.list()
   $ntw=get-view $hst.extensiondata.configmanager.networksystem
   write-host "collected nicinfo" 
   $pnics=$nicinfo| where-object{$_.link -like "UP"}
   $nics=$pnics.name
        foreach($nic in $nics)
       {
         write-host -foregroundcolor Yellow "Enabling VLAN Statistics for Nic" $nic
         $nicstat.network.nic.vlan.stats.set("true","$nic")
         sleep 40
        $data=' '|select Hostname, VC, NIC, Switch_Name, Switch_IP, Switch_Port, FullDuplex, MTU,          Trunked_VLANs, vlan_Count
        $pnicatt=$ntw.querynetworkhint($nic)
        $CDP=$pnicatt.connectedswitchport
        $data.Hostname=$hst.networkinfo.hostname
        $data.VC=$vc
        $data.nic=$nic
        $data.Switch_IP=$cdp.address
        $data.Switch_Name=$cdp.devid
        $data.Switch_Port=$cdp.portid
        $data.Fullduplex=$cdp.fullduplex
        $data.mtu=$cdp.mtu
        $nicstat.network.nic.vlan.stats.get("$nic") | select vlanid
        sleep 10
        $nicstat.network.nic.vlan.stats.get("$nic") | select vlanid
        sleep 10
        $vlan=$nicstat.network.nic.vlan.stats.get("$nic") | select vlanid
        $vlanop=($nicstat.network.nic.vlan.stats.get("$nic") | select -expand vlanid) -join ','
        $data.Trunked_Vlans=$vlanop
        $data.Vlan_Count=$vlan.count
        write-host -foregroundcolor Yellow "Disabling VLAN Statistics for Nic" $nic
        $nicstat.network.nic.vlan.stats.set("false","$nic")
        $info+=$data
        }
     }
      write-host -foregroundcolor green "Disconnecting VC" $VC
      disconnect-viserver $vc -confirm:$false
 }

    write-host -foregroundcolor Yellow "Exporting Data to a File"
    $info | export-csv -path "$env:homedrive\$vc-CDP1.csv"


Comments