-
1. Re: PowerCLI script to change the MAC address
LucD Nov 25, 2011 5:01 AM (in response to iefke)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:$falseNote that you will have to configure the IP settings of the new NIC
-
2. Re: PowerCLI script to change the MAC address
iefke Nov 25, 2011 5:23 AM (in response to LucD)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 -
3. Re: PowerCLI script to change the MAC address
LucD Nov 25, 2011 6:03 AM (in response to iefke)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.
-
4. Re: PowerCLI script to change the MAC address
orthohin Aug 14, 2013 5:08 AM (in response to LucD)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 -
5. Re: PowerCLI script to change the MAC address
iefke Nov 25, 2011 7:34 AM (in response to orthohin)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.
-
6. Re: PowerCLI script to change the MAC address
srnhpp Nov 25, 2011 7:38 AM (in response to orthohin)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 MacAddressforeach($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" -
7. Re: PowerCLI script to change the MAC address
mattboren Nov 25, 2011 4:34 PM (in response to iefke)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
mattboren Nov 25, 2011 4:40 PM (in response to srnhpp)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.
-
9. Re: PowerCLI script to change the MAC address
LucD Nov 25, 2011 4:43 PM (in response to mattboren)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.
-
10. Re: PowerCLI script to change the MAC address
mattboren Nov 25, 2011 4:48 PM (in response to LucD)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...