iefke
Enthusiast
Enthusiast

PowerCLI script to change the MAC address

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.

Blog: http://www.ivobeerens.nl
0 Kudos
10 Replies
LucD
Leadership
Leadership

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


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

0 Kudos
iefke
Enthusiast
Enthusiast

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

Blog: http://www.ivobeerens.nl
0 Kudos
LucD
Leadership
Leadership

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.


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

0 Kudos
orthohin
Enthusiast
Enthusiast

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

Never trust a computer you can't throw out a window
0 Kudos
iefke
Enthusiast
Enthusiast

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.

Blog: http://www.ivobeerens.nl
0 Kudos
srnhpp
Enthusiast
Enthusiast

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"

0 Kudos
mattboren
Expert
Expert

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.

0 Kudos
mattboren
Expert
Expert

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.

0 Kudos
LucD
Leadership
Leadership

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.


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

0 Kudos
mattboren
Expert
Expert

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

0 Kudos