Tuesday, June 21, 2011

VMs with duplicate MACs

We had quite a bit of trouble with vms that had duplicate MAC addresses in our environment. VMware said it should happen rarely but that hasn't been the case for us. So I wrote this handy script to find them for us.

#clear screen
cls

#remove any lingering variables
Remove-Variable -Name * -Force -ErrorAction SilentlyContinue

#where $vCenters are the names of your vCenters
$vCenters = 'vCenter1','vCenter2'
foreach ($vCenter in $vCenters)
{
#connect to vCenter
Connect-VIServer $vCenter 
$mac = @{}
foreach ($vm in Get-VM)
    {
    Get-NetworkAdapter -VM $vm | foreach
        {
         $net = $_.networkname
         if($mac.ContainsKey($_.MacAddress) -and $vm.Name -notlike $mac[$_.MacAddress])
           {
           if ($net -notlike $_.networkname)
            {
            Write-Host "Duplicate MAC address" $_.MacAddress "in" $vm.Name "and" $mac[$_.MacAddress]
   $output = "Duplicate MAC address $_.MacAddress in $vm.Name and $mac[$_.MacAddress]"
   $output | Out-File c:\dupmac.txt -Append
        }else{
            $mac[$_.MacAddress] = $vm.Name
            }
        }
    }
}
Disconnect-VIServer -Server $vCenter -Confirm:$false
}

No comments:

Post a Comment