VMware Cloud Community
Pinball
Enthusiast
Enthusiast
Jump to solution

Include host name in csv export

Hi there,

I suppose this might be simple but need assistance to list the hostname as part of the export.

Here is the script i'm using but i'm getting only the details and not the assigned host.

Get-VMHost | Where-Object {$_.ConnectionState -eq "Connected"} |

%{Get-View $_.ID} |

%{$esxname = $_.Name; Get-View $_.ConfigManager.NetworkSystem} |

%{ foreach($physnic in $_.NetworkInfo.Pnic){

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

    foreach($hint in $pnicInfo){

      Write-Host $esxname $physnic.Device

      if( $hint.ConnectedSwitchPort ) {

        $hint.ConnectedSwitchPort

      }

      else {

        Write-Host "No CDP information available."; Write-Host

      }

    }

  }

} | Export-Csv "C:\PS\CDP-VCS01.csv" -NoTypeInformation -UseCulture



Hope this is a quick fix.


Johan

0 Kudos
1 Solution

Accepted Solutions
LucD
Leadership
Leadership
Jump to solution

You do know that a puppy will perish for every Write-Host you use :smileygrin:

Try something like this

&{Get-VMHost | Where-Object {$_.ConnectionState -eq "Connected"} | %{
 
$esxname = $_.Name
 
Get-View $_.ExtensionData.ConfigManager.NetworkSystem | %{
   
foreach($physnic in $_.NetworkInfo.Pnic){
     
$pnicInfo = $_.QueryNetworkHint($physnic.Device)
     
foreach($hint in $pnicInfo){
       
New-Object PSObject -Property @{
         
VMHost = $esxname
         
Device = $physnic.Device
         
SwitchPort = $hint.ConnectedSwitchPort.PortID
        }
      }
    }
  }
}}
| Export-Csv "C:\PS\CDP-VCS01.csv" -NoTypeInformation -UseCulture

I'm not sure which information you want from the CDP info, when available.

In the sample I include the PortID, any other property can be included in a similar way.


Blog: lucd.info  Twitter: @LucD22  Co-author PowerCLI Reference

View solution in original post

0 Kudos
2 Replies
LucD
Leadership
Leadership
Jump to solution

You do know that a puppy will perish for every Write-Host you use :smileygrin:

Try something like this

&{Get-VMHost | Where-Object {$_.ConnectionState -eq "Connected"} | %{
 
$esxname = $_.Name
 
Get-View $_.ExtensionData.ConfigManager.NetworkSystem | %{
   
foreach($physnic in $_.NetworkInfo.Pnic){
     
$pnicInfo = $_.QueryNetworkHint($physnic.Device)
     
foreach($hint in $pnicInfo){
       
New-Object PSObject -Property @{
         
VMHost = $esxname
         
Device = $physnic.Device
         
SwitchPort = $hint.ConnectedSwitchPort.PortID
        }
      }
    }
  }
}}
| Export-Csv "C:\PS\CDP-VCS01.csv" -NoTypeInformation -UseCulture

I'm not sure which information you want from the CDP info, when available.

In the sample I include the PortID, any other property can be included in a similar way.


Blog: lucd.info  Twitter: @LucD22  Co-author PowerCLI Reference

0 Kudos
Pinball
Enthusiast
Enthusiast
Jump to solution

Luc

Thank you very much for correcting the script. It works as expected and yes, it was only the hostname and port i was interested in.

Johan

0 Kudos