Tuesday, May 29, 2012

Creating Server 2012 LBFO Teams grouping by default gateway

Here is where I take my two previous posts and splice them together into something useful.

The previous posts contain the details of picking through the various portions of the script, here I just toss it together with some useful loops and work through the Groups that I created.

Once again, this is all PowerShell v3 and Server 2012. 

Recall that my scenario is:

Someone racked this server, they jacked it to the management network, the VM production network, and the storage network. I want to know what NICs are jacked where and group and summarize them.  And each of these networks has DHCP running as I have no time to manually assign IP addresses any longer. They are divided physically in the top of the rack with three switches.

The meat of the script is:

$ips = Get-NetAdapter -Physical | where {$_.Status -eq "Up"} | Get-NetIPConfiguration | Group-Object -property IPv4DefaultGateway

foreach ($group in $ips) {
    # test for no gateway we expect DHCP to give one, if there isn’t one then this really isn’t useful is it?

    If ([string]$group.Values.nexthop -ne "") {

        $nicList = @()
        foreach ($nic in $group.Group) {
            $nicList += $nic.InterfaceAlias

        $name = [string]$group.Values.nexthop

        $team = New-NetLbfoTeam -Name $name -TeamNicName ($name + "Team") -TeamMembers ($nicList) -TeamingMode SwitchIndependent -LoadBalancingAlgorithm HyperVPort -Confirm:$false 
        Clear-Variable -Name nicList

        Do {$team = Get-NetLbfoTeam -Name $team.Name
            sleep 2}
        until ($team.Status -eq "Up")

        Get-NetIPAddress -InterfaceAlias $team.TeamNics | select IPAddress


The default LoadBalancingAlgorithm is TransportPorts and works in most general cases. 
If the team is specific to supporting VMs and it has an External Virtual Switch attached then HyperVPort should be used.


David said...

I'm trying to figure out how to remove this but tottaly new to powershell. can you point me in the right direction or show me a script that makes it all go away ;)

BrianEh said...

New-NetLBFOTeam was the cmdlet that created the team.
Remove-NetLBFOTeam with a selector will un-create the team.

at the PowerShell prompt type: get-help Remove-NetLBFOTeam -examples

To see all the LBFO related cmdlets: get-command *NetLBFO*