Automation

 View Only
Expand all | Collapse all

PowerCLI script to change the MAC address

  • 1.  PowerCLI script to change the MAC address

    Posted Nov 25, 2011 12:45 PM

    I'am looking for a PowerCLI script to change the MAC adress when updating to the VMXNET3 adapter. I want to re-use the old MAC address.



  • 2.  RE: PowerCLI script to change the MAC address

    Posted Nov 25, 2011 01:02 PM

    There are 2 conditions fo rthis to work:

    1) The VM must be powered off

    2) The MAC address on the current card must be a "manual" MAC address

    If those conditions are fullfilled, you could do

    $vm = Get-VM MyVM
    $nic = Get-NetworkAdapter -VM $vm
    
    Remove-NetworkAdapter
    -NetworkAdapter $nic -Confirm:$false
    New-NetworkAdapter -VM $vm -MacAddress $nic.MacAddress -Type "vmxnet3" -Confirm:$false

    Note that you will have to configure the IP settings of the new NIC



  • 3.  RE: PowerCLI script to change the MAC address

    Posted Nov 25, 2011 01:23 PM

    I change the script a little, but is unable to create a new-networkadapter because the MAC addres is not in the valid range. Is it possbile to overrule this so i can use this MAC address?

    $MAC = '00:50:56:90:19:01'
    $vm = Get-VM "vmname"
    $nic = Get-NetworkAdapter -VM $vm
    Remove-NetworkAdapter -NetworkAdapter $nic -Confirm:$false
    New-NetworkAdapter -VM $vm -MacAddress $MAC -Type "vmxnet3" -Confirm:$false



  • 4.  RE: PowerCLI script to change the MAC address

    Posted Nov 25, 2011 02:04 PM

    I'm afraid not with this method.

    The "manual" MAC addresses have to be in the range 00:50:56:00:00:00 and 00:50:56:3f:ff:ff.

    You can of course go into the guest OS and change the "Locally adminisered addess" in there.

    For Windows guest you could use the Invoke-VMScript cmdlet and launch a 'netsh' command.



  • 5.  RE: PowerCLI script to change the MAC address

    Posted Nov 25, 2011 03:27 PM

    You can adjust Luc's code a bit so that it exports the original info along with the MAC address.

    It would be something like:

    ## save the imported info $colVMInfoFromCSV = Import-Csv $importfile

    ## add a NoteProperty to the info (later to be a "column" at Export-Csv time) $colVMInfoFromCSV | Add-Member -MemberType NoteProperty macaddress -value $null $colVMInfoFromCSV | %{    $vname = $($_.Hostname)    New-VM -name $vname -VMhost $vmhost -ResourcePool $pool -DiskMB $disksize -MemoryMB $memsize -NumCpu $cpu -DiskStorageFormat thin -Datastore $datastore -GuestID $os -NetworkName $nic0 -Location $location      $macnic1 = Get-NetworkAdapter -vm $vname | where {$_.type -match "e1000"} | select-object MacAddress

    ## put the MAC address in this "row" of the collection of info (this object in the array of objects)    $_.macaddress = $macnic1      New-NetworkAdapter -VM $vname -NetworkName $nic1 -StartConnected -Type vmxnet3    New-NetworkAdapter -VM $vname -NetworkName $nic2 -StartConnected -Type vmxnet3 }

    Regards,
    Milton



  • 6.  RE: PowerCLI script to change the MAC address

    Posted Nov 25, 2011 03:34 PM

    The problem is that the New-NetworkAdapter cmdlet only excepts the 00:50:56:00:00:00 and 00:50:56:3f:ff:ff. MAC address range.



  • 7.  RE: PowerCLI script to change the MAC address

    Posted Nov 26, 2011 12:34 AM

    Hello, iefke-

    Yes, that is a limitation in the New-NetworkAdapter and Set-NetworkAdapter cmdlets (likely for a reason -- say, maybe to keep a sub-range of MAC addresses for manual manipulation, separate from auto-assigned addresses).  I encountered, a while back, a need similar to yours to set a MAC address for a NIC for a VM, a MAC address that is outside of that given range.

    You can use the API to set the NIC's MAC address to any value you want.  I wrote about it at vNugglets.com in my Setting MAC Address for VM NICs using PowerShell post.  Give that a look -- it should do just for what you are looking.



  • 8.  RE: PowerCLI script to change the MAC address

    Posted Nov 26, 2011 12:43 AM

    That might indeed work to change the MAC address to any value, but the disadvantage is that you can't use the PowerCLI cmdlets or the vSphere Client anymore to change anything on that NIC.



  • 9.  RE: PowerCLI script to change the MAC address

    Posted Nov 26, 2011 12:49 AM

    Very true, LucD -- that is a drawback of setting the NIC's MAC address to something outside of the "acceptable" range as expected by both PowerCLI cmdlets and the vSphere client.

    Hopefully the need for manually specifying a MAC address outside of the default range is limited -- say, a not-so-great piece of software that licenses based on the MAC address of the NIC of a machine...



  • 10.  RE: PowerCLI script to change the MAC address

    Posted Nov 25, 2011 03:39 PM
    The following script start a search based on the MAC address of the VM.
    $tgtMAC = ""
    $vms = Get-VM foreach($vm in $vms){ 
    $vmMAC =
    $vm | Get-NetworkAdapter | select MacAddress
    foreach($mac in $vmMAC){     if($mac.MacAddress -eq $tgtMAC) {       Write-Host "Found the VM!"       $vm.Name  
              }
          }
    }
    You can also add the script to the Eco Shell. Just change the
    $tgtMAC line to:  $tgtMAC = Read-Host "enter MAC address"



  • 11.  RE: PowerCLI script to change the MAC address

    Posted Nov 26, 2011 12:40 AM

    Hello, srnhpp-

    Yes, that is one way to find a VM based on the MAC address of one of its NICs.  Another way that should be quite a bit quicker is to use the Get-View cmdlet instead of the Get-VM cmdlet.

    I wrote about this in my Find VM by NIC MAC Address with PowerShell -- Fast Like! post at vNugglets.com a bit ago.  You might find it interesting.