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.
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
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
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!
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
Luc,
Thank you very much for your help!!!
It's working great now!!!
qwert