PowerCLI function Change-Mac

As we needed to change a lot of MAC addresses because we needed to move a lot of VM’s to another vCenter. I created a function to change the MAC-Address of a VM trough PowerCLI.
Because the address is based on the vCenter ID we had some troubles in the past with SCCM and double MAC Addresses so we needed to take this opportunity to fix it to be compliant with the vCenter ID.

function Change-Mac{
Generates a new Mac-Address based on the existing vCenter ID
This function creates a new MAC Address while the NIC
configuration is maintained
Source: The Interwebz
Authors: Me, Myself and I
Specify the virtual machine to change the MAC Address from
PS> Change-Mac -VM VM001
PS> (GC import.txt)|change-mac
Param (
Valuefrompipeline = $true,
ParameterSetName = "VM",
Mandatory = $true,
HelpMessage = "Enter VM name")]

foreach ($vmnic in $VM){
$objState = Get-VM $vmnic | Select-Object PowerState}
If ($objState -match "off")
$thisAdapter = Get-NetworkAdapter -Vm $vmnic
foreach ($nic in $thisAdapter)
$nic.ExtensionData.AddressType = "Generated"
$nic.ExtensionData.MacAddress = ""
Set-NetworkAdapter $thisAdapter -confirm:$false
Write-Host -ForegroundColor Green $vmnic "succesfully edited"
Write-Host -ForegroundColor red $vm "is not powered off, please turn off!"

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.