Windows Server 2012 - Get Binding Order For NICS in Powershell

I had a requirement the other day to retrieve the binding order for nics across several hosts in Windows Server 2012 but none of the scripts I found online worked!

Here is the code I came up with and ran against 16 hosts .

 

Only real pre-req is the WINRM / Remote Powershell is enabled for a host

 

here is the code!

<#
 
 .SYNOPSIS
 Retrieves the NIC Bindings for a Server 2012 Host (local or remote)
 
 .DESCRIPTION

 This script retrieves NIC's which are TCPIP Bound and outputs a text file per server detailing the bindings
 Input.txt is a text file with a server name per line
 .PARAMETER Serverlist

 .EXAMPLE
 
 .\GetBindings.ps1 -serverlist input.txt  
 
 #>

[CmdletBinding()]
#Validating the file exists before continuing into script
param([ValidateScript({test-path $_})][string]$ServerList)

function getbindings
{
 $bindings = (get-itemproperty "HKLM:\SYSTEM\CurrentControlSet\Services\Tcpip\Linkage").bind
 $returnobj = new-object PSobject
 [array]$bindingorder = $null
 foreach ($bind in $bindings)
 {
  $deviceid = $bind.split("\")[2]
  $adapter = (get-netadapter |where {$_.DeviceID -eq $deviceid}).Name
  $bindingorder += $adapter
 }

  $bindingorder
}

$servers = get-content $serverlist

$outputdir = $env:userprofile + "\Desktop\NICBindings"

$outputexist = test-path $outputdir

if ($outputexist -eq $false)

{

   new-item -type directory -path $outputdir

}

foreach ($srv in $servers)
{
 $results = invoke-command -computername $srv -scriptblock ${function:getbindings}

 $file = $outputdir + "\" + $srv + "_NICBindings.txt"
 $results |out-file $file
}