Saturday, July 2, 2016

vSwitch Documentation with CDP Information

I was asked today to document all the vSwitches in all of my standalone ESXi host.  Initially I was going to log into each and document everything in excel but I knew there was a more efficient way.  So I started to google and got dozens of sites back and dug through them all and finally found one that worked for me.  I was able to run through one host at a time, but I wanted it to be better.  So I added an import and for each with some squiggly brackets then piped that out to a text file.  See below for full PowerCLI script with my modifications.  Original script before my changes can be found here: http://kunaludapi.blogspot.com

Start of script

 Add-PSSnapin VMware.VimAutomation.core 
 Add-PSSnapin VMware.VimAutomation.Vds 
  
Import-Csv C:\temp\ESXi_Server_Names.csv | foreach { 
  
 Connect-viServer -server $_.Server -User root -Password "your password"
   
 $Collection = @() 
  
 $Esxihosts = Get-VMHost | Where-Object {$_.ConnectionState -eq "Connected"} 
 foreach ($Esxihost in $Esxihosts) { 
   $Esxcli = Get-EsxCli -VMHost $Esxihost 
   $Esxihostview = Get-VMHost $EsxiHost | get-view 
   $NetworkSystem = $Esxihostview.Configmanager.Networksystem 
   $Networkview = Get-View $NetworkSystem 
      
   $DvSwitchInfo = Get-VDSwitch -VMHost $Esxihost 
   if ($DvSwitchInfo -ne $null) { 
     $DvSwitchHost = $DvSwitchInfo.ExtensionData.Config.Host 
     $DvSwitchHostView = Get-View $DvSwitchHost.config.host 
     $VMhostnic = $DvSwitchHostView.config.network.pnic 
     $DVNic = $DvSwitchHost.config.backing.PnicSpec.PnicDevice 
   } 
    
   $VMnics = $Esxihost | get-vmhostnetworkadapter -Physical 
   Foreach ($VMnic in $VMnics){ 
       $realInfo = $Networkview.QueryNetworkHint($VMnic) 
       $pNics = $esxcli.network.nic.list() | where-object {$vmnic.name -eq $_.name} | Select-Object Description, Link          
       $Description = $esxcli.network.nic.list() 
       $CDPextended = $realInfo.connectedswitchport 
         if ($vmnic.Name -eq $DVNic) { 
            
           $vSwitch = $DVswitchInfo | where-object {$vmnic.Name -eq $DVNic} | select-object -ExpandProperty Name 
         } 
         else { 
           $vSwitchname = $Esxihost | Get-VirtualSwitch | Where-object {$_.nic -eq $VMnic.DeviceName} 
           $vSwitch = $vSwitchname.name 
         } 
   $CDPdetails = New-Object PSObject 
   $CDPdetails | Add-Member -Name EsxName -Value $esxihost.Name -MemberType NoteProperty 
   $CDPdetails | Add-Member -Name VMNic -Value $VMnic -MemberType NoteProperty 
   $CDPdetails | Add-Member -Name vSwitch -Value $vSwitch -MemberType NoteProperty 
   $CDPdetails | Add-Member -Name Link -Value $pNics.Link -MemberType NoteProperty  
   $CDPdetails | Add-Member -Name PortNo -Value $CDPextended.PortId -MemberType NoteProperty 
   $CDPdetails | Add-Member -Name Device-ID -Value $CDPextended.devID -MemberType NoteProperty 
   $CDPdetails | Add-Member -Name Switch-IP -Value $CDPextended.Address -MemberType NoteProperty 
   $CDPdetails | Add-Member -Name MacAddress -Value $vmnic.Mac -MemberType NoteProperty 
   $CDPdetails | Add-Member -Name SpeedMB -Value $vmnic.ExtensionData.LinkSpeed.SpeedMB -MemberType NoteProperty 
   $CDPdetails | Add-Member -Name Duplex -Value $vmnic.ExtensionData.LinkSpeed.Duplex -MemberType NoteProperty 
   $CDPdetails | Add-Member -Name Pnic-Vendor -Value $pNics.Description -MemberType NoteProperty 
   $CDPdetails | Add-Member -Name Pnic-drivers -Value $vmnic.ExtensionData.Driver -MemberType NoteProperty 
   $CDPdetails | Add-Member -Name PCI-Slot -Value $vmnic.ExtensionData.Pci -MemberType NoteProperty 
   $collection += $CDPdetails 
   } 
 } 
  
 $Collection | Sort-Object esxname, vmnic | ft * | Out-File C:\Temp\ESXi_Net.txt -Width 300 -append
  
 Disconnect-VIserver * -confirm:$false }


End of script

No comments:

Post a Comment