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

process
{
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"
}
else
{
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.