Wednesday, May 15, 2013

Export CDP info using Powercli

I was asked the other day to export cdp information from esx hosts at a client site to file. I used the vmware kb here that has source code for powercli. I just took this code and ammended it to export to a file instead of the console. Here is my ammended code.

$logfile = "c:\Temp\cdpinfo.txt"
Connect-VIServer vcenterhostname -User domain\username -Password password
Get-Datacenter | Get-VMHost | Where-Object {$_.State -eq "Connected"} |
%{Get-View $_.ID} |
%{$esxname = $_.Name; Get-View $_.ConfigManager.NetworkSystem} |
%{ foreach($physnic in $_.NetworkInfo.Pnic){
    $pnicInfo = $_.QueryNetworkHint($physnic.Device)
    foreach($hint in $pnicInfo){
       $esxname | Out-File $logfile -Append
    $physnic.Device | Out-File $logfile -Append
      if( $hint.ConnectedSwitchPort ) {
        $hint.ConnectedSwitchPort | Out-File $logfile -Append
      else {
        "No CDP information available." | Out-File $logfile -Append