Monday, March 2, 2015

Powershell: SkipAsSource Flag

Finally ran into an issue where the removal of the primary ip address after Server 2003 caused an issue. Since if you have a multihomed nic the routing table will be set to use the closest IP address to the gateway as the source interface. Here is a fix from  James Kehr to run on your server to set an ip address to be the "Primary" ie Source. I did edit his script by putting a menu and removing the need to put a interface alias. This should only work with 2012 and above with powershell 3.0 and above. For other OS check the heyscriptingguy link.

Thank you James Kehr
# set some constants
Import-Module NetTCPIP

Get-NetIPAddress | Where {$_.PrefixOrigin -eq "Manual"} | select IPaddress,InterfaceAlias,SkipasSource | format-table

$primaryIP = Read-Host "Which ip address should be set as primary?"
$Alias = Get-NetIPAddress $primaryIP -EA stop | select -expand InterfaceAlias

Write-Host "Setting $Alias primary IP to $primaryIP"

# get all the IP addresses on the interface, filtered by IPv4 and excluding

# the $primaryIP

[array]$IPs = Get-NetIPAddress -InterfaceAlias $Alias | Where-Object {$_.AddressFamily -eq "IPv4" -and $_.IPAddress -ne $primaryIP}


# set primary IP SkipAsSource to false

Set-NetIPAddress -IPAddress $primaryIP -InterfaceAlias $Alias -SkipAsSource $false


# set other IP addresses with SkipAsSource equal to true

Set-NetIPAddress -IPAddress $IPs.IPAddress -InterfaceAlias $Alias -SkipAsSource $true
Write-Host "Please review the output False under SkipasSource means the ip can be a source"
Get-NetIPAddress | Where {$_.PrefixOrigin -eq "Manual"} | select IPaddress,InterfaceAlias,SkipasSource | format-table