Retrieve all MAC addresses of NICs associated with ESXi hosts in a cluster

# Import VMware PowerCLI module
Import-Module VMware.PowerCLI

# Connect to vCenter
$vCenter = "vcenter.local"  # Replace with your vCenter server
$username = "administrator@vsphere.local"  # Replace with your vCenter username
$password = "yourpassword"  # Replace with your vCenter password

Connect-VIServer -Server $vCenter -User $username -Password $password

# Specify the cluster name
$clusterName = "ClusterName"  # Replace with the target cluster name

# Get all ESXi hosts in the specified cluster
$esxiHosts = Get-Cluster -Name $clusterName | Get-VMHost

# Loop through each ESXi host in the cluster
foreach ($host in $esxiHosts) {
    Write-Host "Processing ESXi Host: $($host.Name)" -ForegroundColor Cyan

    # Get all physical NICs (VMNICs) on the ESXi host
    $vmnics = Get-VMHostNetworkAdapter -VMHost $host | Where-Object { $_.NicType -eq "Physical" }

    # Get all VMkernel adapters on the ESXi host
    $vmkernelAdapters = Get-VMHostNetworkAdapter -VMHost $host | Where-Object { $_.NicType -eq "Vmkernel" }

    # Display VMNICs and their associated VMkernel adapters
    foreach ($vmnic in $vmnics) {
        $macAddress = $vmnic.Mac
        Write-Host "  VMNIC: $($vmnic.Name)" -ForegroundColor Green
        Write-Host "    MAC Address: $macAddress"

        # Check for associated VMkernel ports
        $associatedVmkernels = $vmkernelAdapters | Where-Object { $_.PortGroupName -eq $vmnic.PortGroupName }
        if ($associatedVmkernels) {
            foreach ($vmkernel in $associatedVmkernels) {
                Write-Host "    Associated VMkernel Adapter: $($vmkernel.Name)" -ForegroundColor Yellow
                Write-Host "      VMkernel IP: $($vmkernel.IPAddress)"
            }
        } else {
            Write-Host "    No associated VMkernel adapters." -ForegroundColor Red
        }
    }

    Write-Host ""  # Blank line for readability
}

# Disconnect from vCenter
Disconnect-VIServer -Confirm:$false




Sample Output :

Processing ESXi Host: esxi01.local
  VMNIC: vmnic0
    MAC Address: 00:50:56:11:22:33
    Associated VMkernel Adapter: vmk0
      VMkernel IP: 192.168.1.10

  VMNIC: vmnic1
    MAC Address: 00:50:56:44:55:66
    No associated VMkernel adapters.

Processing ESXi Host: esxi02.local
  VMNIC: vmnic0
    MAC Address: 00:50:56:77:88:99
    Associated VMkernel Adapter: vmk1
      VMkernel IP: 192.168.1.20

Exporting to a CSV (Optional)

If you want to save the results to a CSV file, modify the script as follows:

  1. Create a results array at the top:
$results = @()

Add results to the array inside the foreach loop:

$results += [PSCustomObject]@{
    HostName       = $host.Name
    VMNIC          = $vmnic.Name
    MACAddress     = $macAddress
    VMkernelAdapter = $vmkernel.Name
    VMkernelIP     = $vmkernel.IPAddress
}

Export the results at the end:

$results | Export-Csv -Path "C:\VMNIC_VMkernel_Report.csv" -NoTypeInformation

Leave a comment