This powershell script gets information about physical nic CDP, Bandwidth, Duplex, Svswitch or dvswitch connected to etc.. Replace 192.168.33.21 with your vcenter or esxi host FQDN or IP address.
I have tested this script with powershell v3, powercli v5.5 and vsphere 5.x.
I have tested this script with powershell v3, powercli v5.5 and vsphere 5.x.
#####################################
## http://kunaludapi.blogspot.com
## Version: 1
## Tested this script on
## 1) Powershell v3
## 2) Powercli v5.5
## 3) Vsphere 5.x
#####################################
Add-PSSnapin VMware.VimAutomation.core
Add-PSSnapin VMware.VimAutomation.Vds
$vCenterCred = Get-Credential -Message "vCenter server or esxi credentials"
$vcenterServer = "192.168.33.21"
Connect-viServer -server $vcenterServer -Credential $vCenterCred
$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 #$_.NetworkInfo.Pnic
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 *
Disconnect-VIserver * -confirm:$false
I have written script to pull information from CDP as well as LLDP also check this link.
Hi Kunal,
ReplyDeleteThe vSwitch info is coming as blank. We have DVS in env.
Need to change -eq with -contains operator for dvswitching bit to work
ReplyDeletebefore
if ($vmnic.Name -eq $DVNic) {
$vSwitch = $DVswitchInfo | where-object {$vmnic.Name -eq $DVNic} | select-object -ExpandProperty Name
after
if ($DVNic -Contains $VMnic.Name) {
$vSwitch = $DVswitchInfo | where-object {$DVNic -Contains $VMnic.Name} | select-object -ExpandProperty Name
This comment has been removed by the author.
ReplyDeleteHi,
ReplyDeleteis it possible to also view if a vmnic is removed from the uplink ports?
This is great but shows that there's a connection upstream but what if the physical network adapter is removed from an uplink?
This is really amazing script. I need vmnic uptime could you please provide me command which I mention in this script?
ReplyDelete