Finding a Specific MAC Address in vCenter

One of my customers found that they had an IP conflict on their network, but the network team was unavailable.  They asked me if I could help them track down the address, while waiting for help from the network team.  I don’t have nearly as much visibility into the environment as a network admin, but vCenter is aware of the vast majority of the servers in the environment and it has data about what MAC Address each one is using.  Of course, that data is buried under “Edit Settings” on each VM and, when you have thousands of VMs to look through, doing that process by hand would be a staggering effort.  So, I scripted it.

This is a really simple script that takes a single parameter –MacAddress.  That address should be specified in colon separated format (like 00:50:56:00:00:01) and it accepts wildcards (so *00:01 is a fine search query).  The script first searches all VMs for the specified MAC Address, then moves on and searches all of the VMKernel interfaces on the ESXi hosts.  If it finds any matches, it spits out the name of the object that owns that MAC, which device is using it and which network that device is attached to.

As always, this script is intended for educational purposes; make sure that you fully understand any script that you download from the internet before running it in your environment; while this worked for me in my particular situation, that's no guarantee that it'll work for you in yours, etc.


param(
   $MacAddress
)
echo ""
echo "Searching for VMs with a MAC address of $MacAddress..."
if ($foundVMs = get-view -viewtype virtualmachine | ? {$_.config.hardware.device.macaddress -like $MacAddress})
{
   $allVMs = get-vm $foundVMs.name
   $allVMs | get-networkadapter | ? {$_.MacAddress -like $MacAddress} | select @{Name="Object";Expression={$_.Parent}},@{Name="Adapter";Expression={$_.Name}},NetworkName
}
else
{
   write-host "No VMs found with a MAC Address of $MacAddress." -foregroundcolor "yellow"
}
echo ""
echo "Searching for VMKernel interfacse with a MAC address of $MacAddress..."
if ($foundHosts = get-view -viewtype hostsystem | ? {$_.config.network.vnic.spec.mac -like $MacAddress})
{
   $allHosts = get-vmhost $foundHosts.name
   $allHosts | get-vmhostnetworkadapter | ? {$_.Mac -like $MacAddress} | Select @{Name="Object";Expression={$_.VMHost}},@{Name="Adapter";Expression={$_.Name}}
}
else
{
   write-host "No VMK Interfaces found with a MAC Address of $MacAddress." -foregroundcolor "yellow"
}

Comments

Popular posts from this blog

Clone a Standard vSwitch from one ESXi Host to Another

PowerShell Sorting by Multiple Columns

Deleting Orphaned (AKA Zombie) VMDK Files