天天看点

Powercli to check VM and ESX network info

1. Check all VM's VMnic on which ESX phyiscal NIC:

get-esxtop -CounterName Netport | Select PortID, ClientName, TeamUplink

2. Check dedicated VM

get-esxtop -CounterName Netport | Where-Object { $_.ClientName -like "PRABC*" } | Select PortID, ClientName, TeamUplink

or

get-esxtop -CounterName Netport | Where-Object { $_.ClientName -match "PRABCAD01" } | Select PortID, ClientName, TeamUplink

3. Check the NIC card driver

$esxcli.software.vib.list.Invoke() | Where { $_.Name -contains "elxnet" } | select Name,ID,Version,InstallDate

4. To get a list of network adapters on a host

Get-VMHostNetworkAdapter -VMHost "ESX03"

5. Check CDP info for the hosts of abc cluster

Get-Cluster abc | Get-VMHost | Get-VMHostNetworkAdapterCDP | Where-Object {$_.connected -eq "True"} | Export-Csv -notypeinformation -Path c:\temp\cdp.csv

Need add function first

function Get-VMHostNetworkAdapterCDP {
<# .SYNOPSIS Function to retrieve the Network Adapter CDP info of a vSphere host. .DESCRIPTION Function to retrieve the Network Adapter CDP info of a vSphere host. .PARAMETER VMHost A vSphere ESXi Host object .INPUTS System.Management.Automation.PSObject. .OUTPUTS System.Management.Automation.PSObject. .EXAMPLE PS> Get-VMHostNetworkAdapterCDP -VMHost ESXi01,ESXi02

 .EXAMPLE
 PS> Get-VMHost ESXi01,ESXi02 | Get-VMHostNetworkAdapterCDP

#>
[CmdletBinding()][OutputType('System.Management.Automation.PSObject')]

Param
 (

[parameter(Mandatory=$true,ValueFromPipeline=$true)]
 [ValidateNotNullOrEmpty()]
 [PSObject[]]$VMHost
 )

begin {

 $ErrorActionPreference = 'Stop'
 Write-Debug $MyInvocation.MyCommand.Name
 $CDPObject = @()
 }

process{

try {
 foreach ($ESXiHost in $VMHost){

if ($ESXiHost.GetType().Name -eq "string"){

 try {
 $ESXiHost = Get-VMHost $ESXiHost -ErrorAction Stop
 }
 catch [Exception]{
 Write-Warning "VMHost $ESXiHost does not exist"
 }
 }

 elseif ($ESXiHost -isnot [VMware.VimAutomation.ViCore.Impl.V1.Inventory.VMHostImpl]){
 Write-Warning "You did not pass a string or a VMHost object"
 Return
 }

$ConfigManagerView = Get-View $ESXiHost.ExtensionData.ConfigManager.NetworkSystem
 $PNICs = $ConfigManagerView.NetworkInfo.Pnic

foreach ($PNIC in $PNICs){

$PhysicalNicHintInfo = $ConfigManagerView.QueryNetworkHint($PNIC.Device)

if ($PhysicalNicHintInfo.ConnectedSwitchPort){

$Connected = $true
 }
 else {
 $Connected = $false
 }

$hash = @{

 VMHost = $ESXiHost.Name
 NIC = $PNIC.Device
 Connected = $Connected
 Switch = $PhysicalNicHintInfo.ConnectedSwitchPort.DevId
 HardwarePlatform = $PhysicalNicHintInfo.ConnectedSwitchPort.HardwarePlatform
 SoftwareVersion = $PhysicalNicHintInfo.ConnectedSwitchPort.SoftwareVersion
 ManagementAddress = $PhysicalNicHintInfo.ConnectedSwitchPort.MgmtAddr
 PortId = $PhysicalNicHintInfo.ConnectedSwitchPort.PortId

}
 $Object = New-Object PSObject -Property $hash
 $CDPObject += $Object
 }
 }
 }
 catch [Exception] {

 throw "Unable to retrieve CDP info"
 }
 }
 end {

 Write-Output $CDPObject
 }
}           
NIC