Skip to content

Instantly share code, notes, and snippets.

@scriptingstudio
Created September 15, 2024 15:11
NIC Team default macaddress setter
<#
.SYNOPSIS
NIC Team default macaddress setter.
.DESCRIPTION
When using DHCP reservation for NIC teaming in Windows Server there is no way of determining which member interface becomes primary at boot. Because it is impossible to determine this, we do not know which member's MAC address will be used by the team. This makes DHCP troublesome.
#>
param (
[alias('name')][string]$teamname,
[string]$macaddress,
[alias('apply')][switch]$run
)
Write-Host "NIC Team default macaddress setter."
Write-Host "USAGE: nicteam [-name <string>] [-macaddress <string>] [-run]`n"
# Look for a NIC team
$nicteam = Get-NetLbfoTeam -ErrorAction Stop
if (-not $teamname) {
$teamname = $nicteam.name
if (-not $teamname) {
Write-Warning 'No NIC team found.'
return
} elseif ($nicteam.count -gt 1) {
Write-Warning "Multiple teams found: $($nicteam.name -join ', '). Select just one target by 'name' parameter."
return
}
} else {
$nicteam = $nicteam | Where-Object Name -eq $teamname
if (-not $nicteam) {
Write-Warning 'No NIC team found with the specified name.'
return
}
}
# Assign the macaddress
$mbr = $nicteam.members
if ($mbr) {
$mac = if ($macaddress) {
$test = $macaddress -replace '[:\-\.]' -replace '(..(?!$))', '$1-'
(Get-NetAdapter | Where-Object {$_.Name -in $mbr -and $_.MacAddress -eq $test}).MacAddress
} else {
(Get-NetAdapter | Where-Object Name -in $mbr | Measure-Object -Property MacAddress -Minimum).Minimum
}
Write-Host ('Team name : {0}' -f $nicteam.name)
#Write-Host ('Team members (name) : {0}' -f ($mbr.name -join ', '))
Write-Host ('Team members : {0}' -f ($mbr.MacAddress -join ', '))
Write-Host ('Target macaddress : {0}' -f $mac)
if (-not $mac) {
Write-Warning "Target MAC Address not determined."
} elseif ($run) {
$mac = $mac -replace '-'
Set-NetAdapterAdvancedProperty -Name $nicteam.name -DisplayName 'MAC Address' -DisplayValue $mac
} else {
Write-Host "`nUse 'run' parameter to apply the selection."
}
} else {
Write-Warning 'Team is empty.'
return
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment