Automation

 View Only
  • 1.  check for duplicate Mac Address on VC

    Posted Feb 24, 2011 07:27 PM

    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.



  • 2.  RE: check for duplicate Mac Address on VC

    Posted Feb 24, 2011 08:02 PM

    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
            }
        } 
    }
    


  • 3.  RE: check for duplicate Mac Address on VC

    Posted Feb 24, 2011 08:47 PM

    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!



  • 4.  RE: check for duplicate Mac Address on VC
    Best Answer

    Posted Feb 24, 2011 09:45 PM

    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
            }
        } 
    }
    


  • 5.  RE: check for duplicate Mac Address on VC

    Posted Feb 25, 2011 05:27 PM

    Luc,

    Thank you very much for your help!!!

    It's working great now!!!

    qwert