NIC Bindings Order
I did a lot of research on how to change NIC Binding Order using powershell. I am changing the binding Order to meet the requirement of Always On server setup. I created the following powershell script which changes registry of NIC binding. After you run the script you will not see the binding order is changed in UI but it is actually updated when you run IP config. I believe that the setting not being updated in UI is a bug in Windows.
In Adapters and Bindings tab, I move Public NIC on top of Private NIC. The powershell script will update 3 places in - Bind , Export and Route under CurrentControl set in Registry.
#-------------------------Public Setting ID------------------------------------------------#
$PublicSettingID = Get-WmiObject -Class Win32_NetworkAdapterConfiguration -Filter "IPenabled = $true" | where {$_.IpAddress -like '10.*'}|select Settingid
$PubSettingID = $PublicSettingID.Settingid
Write-host $PubSettingID
#-------------------------Private Setting ID------------------------------------------------#
$PrivateSettingID = Get-WmiObject -Class Win32_NetworkAdapterConfiguration -Filter "IPenabled = $true" | where {$_.IpAddress -like '192.*'}|select Settingid
$PriSettingID = $PrivateSettingID.Settingid
Write-host $PriSettingID
#---------------------------------BIND------------------------------------------------------#
$BindNewOrder = @()
$writereg = $null
$Bindkey = 'HKLM:\SYSTEM\CurrentControlSet\Services\Tcpip\Linkage'
$Bindvalue = 'Bind'
#get values in registry
$BindOldOrder = (Get-ItemProperty $Bindkey $Bindvalue).$Bindvalue
write-host $BindOldOrder
$BindPubSettingID='\Device\'+$PubSettingID
$BindPriSettingID='\Device\'+$PriSettingID
#Order
if ($BindOldOrder -contains $BindPubSettingID) {$BindNewOrder += $BindPubSettingID}
if ($BindOldOrder -contains $BindPriSettingID) {$BindNewOrder += $BindPriSettingID}
if ($BindOldOrder.count -gt $BindNewOrder.count) {$BindNewOrder += $BindOldOrder}
$BindNewOrder = $BindNewOrder | select -unique
Write-host $BindNewOrder
#Change registry valules
Set-ItemProperty -path $Bindkey -Name $Bindvalue -Value $BindNewOrder
#>
#----------------------------EXPORT----------------------------------------------------#
$ExportNewOrder = @()
$Exportkey = 'HKLM:\SYSTEM\CurrentControlSet\Services\Tcpip\Linkage'
$Exportvalue = 'Export'
$ExportPubSettingID = '\Device\Tcpip_' + $PubSettingID
$ExportPriSettingID = '\Device\Tcpip_' + $PriSettingID
Write-host $ExportPubSettingID
Write-host $ExportPriSettingID
#get values in registry
$ExportOldOrder = (Get-ItemProperty $Exportkey $Exportvalue).$Exportvalue
write-host $ExportOldOrder
#Order
if ($ExportOldOrder -contains $ExportPubSettingID) {$ExportNewOrder += $ExportPubSettingID}
if ($ExportOldOrder -contains $ExportPriSettingID) {$ExportNewOrder += $ExportPriSettingID}
if ($ExportOldOrder.count -gt $ExportNewOrder.count) {$ExportNewOrder += $ExportOldOrder}
$ExportNewOrder = $ExportNewOrder | select -unique
Write-host $ExportNewOrder
Set-ItemProperty -path $Exportkey -Name $Exportvalue -Value $ExportNewOrder
#-----------------------------ROUTE-------------------------------------------------------#
$RouteNewOrder = @()
$Routekey = 'HKLM:\SYSTEM\CurrentControlSet\Services\Tcpip\Linkage'
$Routevalue = 'Route'
$RoutePubSettingID = '"' + $PubSettingID + '"'
$RoutePriSettingID = '"' + $PriSettingID + '"'
Write-host $RoutePubSettingID
Write-host $RoutePriSettingID
#get values in registry
$RouteOldOrder = (Get-ItemProperty $Routekey $Routevalue).$Routevalue
write-host $RouteOldOrder
#Order
if ($RouteOldOrder -contains $RoutePubSettingID) {$RouteNewOrder += $RoutePubSettingID}
if ($RouteOldOrder -contains $RoutePriSettingID) {$RouteNewOrder += $RoutePriSettingID}
if ($RouteOldOrder.count -gt $RouteNewOrder.count) {$RouteNewOrder += $RouteOldOrder}
$RouteNewOrder = $RouteNewOrder | select -unique
Write-host $RouteNewOrder
Set-ItemProperty -path $Routekey -Name $Routevalue -Value $RouteNewOrder
After you run the script, you will see the output similar to below.
The changes takes affect on NIC Binding but UI is not updated. It think it is a bug in Microsoft windows. The UI does not change even after rebooting the server.When you run Ipconfig /all on the server, it returns public ip address then private ip address.
I was just looking to do this very thing today when I stumbled on your script and saved a lot of time. Thanks.
ReplyDelete