Skip to main content
  1. Articles/

Set source IP for outbound traffic

·551 words·3 mins· loading
Rodrigo Silva
Author
Rodrigo Silva
20+ years in IT. That’s a lot of years.
Table of Contents

If you work with IIS web servers, chances are that at some point you had to set the NIC’s primary IP for outbound traffic. By default, Windows will use the lowest numerical IP, and that is usually ok in most scenarios.

For example, if you have IPs 192.168.0.35, 192.168.0.42 and 192.168.0.44, the network adapter will use 192.168.0.35 as the default source IP for all outgoing connections. But what if you need it to be 192.168.0.42 instead?

Some applications will give you the option to specify the source IP. The command ping, for example, has the parameter /S.

ping google.com /S 192.168.0.42

But that’s more of an exception. Most applications won’t allow for a source IP to be specified. If you have firewall rules restricting access to specific IPs, you will need to make sure your NIC initiates connections with the correct IP.

SkipAsSource
#

Windows will allow you to overwrite the default lowest numerical IP with the -SkipAsSource parameter, but that is not really straightforward. By default, all IPs have that parameter set to $false so any IP could be used as source. If you want to specify a single IP to be the source, you need to manually set all the other IPs to $true. That is counterintuitive.

Set-NetIPAddress -IPAddress 192.168.0.35 -SkipAsSource $true
Set-NetIPAddress -IPAddress 192.168.0.42 -SkipAsSource $false
Set-NetIPAddress -IPAddress 192.168.0.44 -SkipAsSource $true

Set-SourceIP
#

I find myself having to deal with this kind of changes quite often, so I decided to write the Set-SourceIP script to simplify the process.

We are still using Set-NetIPAddress in the background along with the -SkipAsSource parameter, but in a more intuitive way. Instead of having to think about all the IPs that you don’t want to be the source, you just need to know the one you do.

This is how you would use the script to set 192.168.0.42 as the source IP:

Set-SourceIP -IPAddress 192.168.0.42

When you run the script, it queries the NIC for all the IP addresses and sets their-SkipAsSource to $true. Then it gets the IP you specified in -IPAddress and sets it back to $false. It’s pretty simple, but effective.

Parameters
#

I like to use familiar nomenclature when writing my scripts, so they look like an extension of Powershell. I usually name my scripts something like “Get-” or “Set-” in an attempt to mimic Powershell’s own commands. And whenever possible, I use the same name for the parameters.

param(
    [Parameter()]
    [string[]] $IPAddress,
    [Parameter()]
    [switch] $Reset
)

IPAddress
#

The -IPAddresss parameter is used to specify the IP you want to be used as the source. It’s the same name that you see in Set-NetIPAddress. This is the part of the code that runs when you use it:

$IPs = Get-NetAdapter | Get-NetIPAddress | Select-Object -expand IPAddress

if ($IPAddress) {

    foreach ($IP in $IPs) {
        Set-NetIPAddress -IPAddress $IP -SkipAsSource $true
    }

    Set-NetIPAddress -IPAddress $IPAddress -SkipAsSource $false

}

The $IPs variable is used to store all the IPs that were queried from the network adapter.

Reset
#

I have also included a -Reset parameter to easily set the adapter back to its default setting.

if ($Reset) {

    foreach ($IP in $IPs) {
        Set-NetIPAddress -IPAddress $IP -SkipAsSource $false
    }

Install
#

You can fork the final script on Github or install it from PowerShell Galery.

  Fork     Install