VMware Cloud Community
qwert1235
Enthusiast
Enthusiast
Jump to solution

check for duplicate Mac Address on VC

Hello:

I am wondering how can I check for duplicate Mac Address on VMs under VC.

Basicly, I want to run the script that will tell me if there any duplicate Mac Addresses are exist.

I know how to find mac on VM (I can use $vmMAC=$vm|Get-NetworkAdapter|select MacAddress), but have trouble to compare it with another Mac addresses without looping a whole script.

Thanks.

Reply
0 Kudos
1 Solution

Accepted Solutions
LucD
Leadership
Leadership
Jump to solution

No, $macTab is defined as a hash table on the first line.

It could be that the MAC address is not available on some your guests.

Try the following version, I added a Where-Object to eliminate those guests.

$macTab = @{}

foreach($vm in Get-VM){
    Get-NetworkAdapter -VM $vm | where {$_.MacAddress} | %{
        if($macTab.ContainsKey($_.MacAddress)){
            Write-Host "Duplicate MAC address" $_.MacAddress "in" $vm.Name "and" $macTab[$_.MacAddress]
        }
        else{
            $macTab[$_.MacAddress] = $vm.Name
        }
    } 
}


Blog: lucd.info  Twitter: @LucD22  Co-author PowerCLI Reference

View solution in original post

Reply
0 Kudos
4 Replies
LucD
Leadership
Leadership
Jump to solution

Try this, it uses a hash table where the key is the MAC address.

$macTab = @{}

foreach($vm in Get-VM){
    Get-NetworkAdapter -VM $vm | %{
        if($macTab.ContainsKey($_.MacAddress)){
            Write-Host "Duplicate MAC address" $_.MacAddress "in" $vm.Name "and" $macTab[$_.MacAddress]
        }
        else{
            $macTab[$_.MacAddress] = $vm.Name
        }
    } 
}


Blog: lucd.info  Twitter: @LucD22  Co-author PowerCLI Reference

qwert1235
Enthusiast
Enthusiast
Jump to solution

Luc,

I am having error:

You cannot call a method on a null-valued expression.

At :line: + if($macTab.ContainsKey <<<< ($_.MacAddress)){

should I define $macTab prior?

Thanks a lot!

Reply
0 Kudos
LucD
Leadership
Leadership
Jump to solution

No, $macTab is defined as a hash table on the first line.

It could be that the MAC address is not available on some your guests.

Try the following version, I added a Where-Object to eliminate those guests.

$macTab = @{}

foreach($vm in Get-VM){
    Get-NetworkAdapter -VM $vm | where {$_.MacAddress} | %{
        if($macTab.ContainsKey($_.MacAddress)){
            Write-Host "Duplicate MAC address" $_.MacAddress "in" $vm.Name "and" $macTab[$_.MacAddress]
        }
        else{
            $macTab[$_.MacAddress] = $vm.Name
        }
    } 
}


Blog: lucd.info  Twitter: @LucD22  Co-author PowerCLI Reference

Reply
0 Kudos
qwert1235
Enthusiast
Enthusiast
Jump to solution

Luc,

Thank you very much for your help!!!

It's working great now!!!

qwert

Reply
0 Kudos