Quick Tip–adding multiple Remote IP Addresses to Receive Connectors…

by Andy Grogan on January 5, 2013 · 21 comments

in Exchange 2007 (Admin), Exchange 2007 (Hub Transport), Exchange 2010 (Admin), Exchange 2010 (Hub Transport), Exchange 2013 (Admin), Exchange 2013 (Transport), Quick Tips

A couple of weeks back an online buddy of mine contacted me to ask some advice. In essence his company was performing some major upgrades to their MPLS network which required some relatively significant changes to hosts that were allowed to relay off of the Exchange Server that was located on his site.

We had a discussion and agreed that they best way to do this was probably to add a new Receive Connector to his Exchange server dedicated to the MPLS and add the relevant Remote IP Ranges to his Receive Connector. Of course this is where it got a little complicated. Well I say complicated – more labour intensive.

You see, my buddies parent company who was managing the MPLS upgrade, had supplied him with a large Excel Spread sheet which contained all of the IP addresses of the hosts which needed to relay off this connector – about 180 entries in total.

Luckily for my buddy (Phil) the Exchange Management Shell was his friend – and by using a simple Text file – a few loops and the Set-ReceiveConnector cmdlet it was pretty easy to put together a Powershell script that would process all of the Remote IP Ranges for him.

Now, I should point out that there are a number of ways in PowerShell that this can be accomplished if you have a look around the Internet – but pretty much all of the examples (including mine) involve concatenating new Remote Remote IP ranges to the existing list on the receive connector and then committing them. My version of the script is designed to make the process of getting multiple IP values out of a text file and added to a specific Receive Connector more straight forward.

The Script

The following is a preview of the script – you can copy and paste it from below to a blank PS1 file on your Exchange Server – or download it from the download section of this post.

<#

.DESCRIPTION

   Simple Powershell script that can bulk import remote IP ranges from a text file in a determined Exchange Receive Connector.

   The Import of the Remote IP ranges maintains the original values which are already present on the Selected Connector.

.PARAMETERS

   None - execute directly from the Exchange Management Shell

.Version

    0.1

.Author

    Andy Grogan

    http://www.telnetport25.com

.Compatibility

   Exchange 2007

   Exchange 2010

   Exchange 2013

.Release Date

    Jan 2013

#>

 

function Select-FileDialog 

{

      param([string]$Title,[string]$Directory,[string]$Filter="Text Files (*.txt)|*.txt")

      [System.Reflection.Assembly]::LoadWithPartialName("System.Windows.Forms") | Out-Null

      $objForm = New-Object System.Windows.Forms.OpenFileDialog

      $objForm.InitialDirectory = $Directory

      $objForm.Filter = $Filter

      $objForm.Title = $Title

      $objForm.ShowHelp = $true

      

      $Show = $objForm.ShowDialog()

      

      if ($Show -eq "OK")

      {

            return $objForm.FileName

      }

      else

      {

            exit

      }

}

 

function get_RecConnector{

 

    $RecConns = Get-ReceiveConnector | Select -ExpandProperty Identity

    $Count = 0;

    Write-Host "Bulk Import of Remote IP Addresses for Exchange Receive Connectors" -ForegroundColor Green

    Write-Host "Version 0.1" -ForegroundColor Green

    Write-Host "www.telnetport25.com" -ForegroundColor Green

    Write-Host ""

    Write-Host "Detected Receive Connectors: " -ForegroundColor Cyan

    Write-Host ""

    foreach($Connector in $RecConns){

        Write-Host $Count "." $Connector -ForegroundColor White

        $Count ++

    }

    Write-Host ""

    $Choice = Read-Host "Please select the Receive Connector that you wish to work with."

    Write-Host ""

    import_RemoteIPRanges $RecConns[$Choice]

}

 

function import_RemoteIPRanges{

 

    param($ConnectorID)

    

    $FileName = Select-FileDialog "Open IP Range Text File..."

    $IPs = Get-Content $FileName

    

        foreach($IP in $IPs){

            Write-Host "Adding IP Address :" $IP " to "$ConnectorID -ForegroundColor Cyan

              $Rcnn = Get-ReceiveConnector "$ConnectorID"

              $Rcnn.RemoteIPRanges += $IP

              Set-ReceiveConnector "$ConnectorID" -RemoteIPRanges $Rcnn.RemoteIPRanges

 

        }

}

 

get_RecConnector

Write-Host ""

Write-Host "Script Completed." -ForegroundColor Yellow

Download

Exchange 2010 and Exchange 2013

[ BulkImportRemoteIPonRecConn.ps1 – 2.31KB ]

Exchange 2007

[ BulkImportRemoteIPonRecConnEx27.ps1 – 2.2KB ]

Using the script

Download the script file which is relevant to your version of Exchange to one of your Exchange Servers.

In order to run the script you will need to ensure that your Powershell execution policies are set correctly. Review the following post that I prepared last year: http://www.telnetport25.com/2012/02/quick-tip-running-exchange-based-powershell-script-files-from-the-command-line-or-a-batch-file/ to check your settings.

Before you use the script you should ensure that you have all of the IP addresses that you wish to add to a particular Receive Connector stored within a text file.

Each host should appear on a separate line. You can also use the CDIR address notation for an entire subnet if you wish to allow all hosts in a range to relay (for example add a line for 172.31.253.0/24) .

An example file is shown below:

05-01-201312-19-09

Open the Exchange Management Shell, navigate to the directory where you have downloaded the script file and type:

<path>.\BulkImportRemoteIPonRecConn.ps1

You will then be presented with a list of all the detected receive connectors that the script has located. Choose the connector via its numerical identifier (the numbers on the left hand side).

05-01-201312-21-03

You will then be prompted to locate your IP Range text file – browse to it and then click on the “Open” button.

05-01-201312-21-22

The script will then process each host entry and add it to the selected Receive Connector.

05-01-201312-21-53

After the script has completed – if you check the [ Network –> Receive Mail from remote servers that these IP addresses ] in the Exchange Management Console, you should see that your addresses have been added.

05-01-201312-28-16

I hope that you find this post and script useful, and that it might help automate this process for someone else in the future.

Social

{ 21 comments… read them below or add one }

Skip January 29, 2013 at 10:05 pm

trying the script out and getting the following error when it gets to adding the IP.. Any ideas? its getting the IP from the txt file fine it seems…

Adding IP Address : 172.20.112.139 to AMATLHUB01\Relay Connector (TEST)
Exception setting “RemoteIPRanges”: “Cannot convert value “System.Object[]” to
type “Microsoft.Exchange.Data.MultiValuedProperty`1[Microsoft.Exchange.Data.IPR
ange]”. Error: “Conversion from System.Management.Automation.PSObject to Micros
oft.Exchange.Data.IPRange has not been implemented.””
At C:\Users\ad_skip.koen\AppData\Local\Temp\2\2bf56965-6a13-4bf7-8c78-aac7a4b5f
50b.ps1:72 char:15
+ $Rcnn. <<<< RemoteIPRanges += $IP
+ CategoryInfo : InvalidOperation: (:) [], RuntimeException
+ FullyQualifiedErrorId : PropertyAssignmentException

Thanks…

Reply

Andy Grogan March 29, 2013 at 3:12 pm

Hiya Skip, sorry for the delay – I have now modified the article to include a script which is compatible with Exchange 2007 – there are some subtle differences between with the way the RemoteIPRanges property can be written to between the versions of Exchange which I was not aware of at the time.
Cheers
A

Reply

Duane January 31, 2013 at 1:41 pm

Hi,

Thank you for your script. It will be very useful for me 😀
But i got same error as Skip !
Can you help please?

Thanks ….

Reply

Andy Grogan March 29, 2013 at 3:13 pm

Hiya Duane, please see the comment that I have added onto Skip’s. Cheers

Reply

Duane March 31, 2013 at 10:15 pm

Hi Andy,

Thank you very much for your reply. It’s very helpful for my work 😀

Cheers.

Reply

Wade February 1, 2013 at 5:24 pm

This is an awesome Script!! Thank you for saving me so much time!!!!

Reply

AnonMan March 26, 2013 at 11:38 am

Like the script, but it doesn’t work for me – get the same error as Skip and Duane.
This is on Exchange 2007 SP3.

Reply

Andy Grogan March 29, 2013 at 3:14 pm

Hi AnonMan – please see the comment that I have added to Skip’s – there is now another version of the script for Exchange 2007.
Cheers
A

Reply

Paul April 11, 2013 at 6:35 pm

When running this script i get
Adding IP Address : to DRHTC01P\Anonymous Relay
Exception setting “RemoteIPRanges”: “Cannot convert value “System.Object[]” to type “Microsoft.Exchange.Data.MultiValue
dProperty`1[Microsoft.Exchange.Data.IPRange]”. Error: “Failed to convert from System.Management.Automation.PSObject to
Microsoft.Exchange.Data.IPRange. Error: Error while converting string ” to result type Microsoft.Exchange.Data.IPRang
e: The format of the IP address is invalid. Example of a valid IP address: 192.168.1.10″”
At C:\BulkImportRemoteIPonRecConn.ps1:72 char:15
+ $Rcnn. <<<< RemoteIPRanges += $IP
+ CategoryInfo : InvalidOperation: (:) [], RuntimeException
+ FullyQualifiedErrorId : PropertyAssignmentException

my txt file is:
172.x.x.x,
10.x.x.x,

and so forth? Not sure what is wrong?

Reply

Andy Grogan April 11, 2013 at 7:18 pm

Hiya Paul, which version of Exchange are you using?

There are two versions of the script for 2007 and 2010/13 within the article:

2007

http://www.telnetport25.com/downloads/scripts/BulkImportRemoteIPonRecConnEx27.ps1

2010

http://www.telnetport25.com/downloads/scripts/BulkImportRemoteIPonRecConn.ps1

Cheers

A

Reply

Andy Grogan April 11, 2013 at 7:21 pm

Also, whilst I think about it – are the IP Addresses in the file on a new line? Also in your example above each IP address line has a , – if this is how it looks in the file – then you should not include the commas.
Cheers
A

Reply

Steve September 26, 2013 at 2:07 pm

I owe this guy a beer! I just piped over 300 IP’s into our Exchange 2013 receive connectors across multiple servers in the matter of minutes. Put each IP on a new line and watch for unnecessary spaces in your text file. The only time when this didn’t work for me was when it was my own fault (typos or addresses already existed). Thanks

Reply

Terry October 21, 2013 at 7:31 pm

This has been a wonderful script and saved SO much time when moving from 2003 to 2010!! Thank you.

Reply

Ron July 2, 2014 at 5:41 pm

Thanks for the script, it saved me time in importing the IP addresses into our CAS Array. I had about 200 servers which I would have had to enter in 2x, once for each server. We are moving away from Exchange 2003 to Exchange 2010

Reply

Mo July 25, 2014 at 5:56 am

Hi with this script does it remove existing IP addresses when adding the new ones ?
I just want to make sure before i run it in my environment

Reply

ccampo October 1, 2014 at 9:18 pm

Very useful … also works perfect in Exchange 2013

Reply

Jerry May 21, 2015 at 12:40 pm

Great Script but it will run much faster if you move the Get-ReceiveConnector and Set-ReceiveConnector from the for each loop so it only does it once instead of every pass through the loop.

function import_RemoteIPRanges{

param($ConnectorID)

$FileName = Select-FileDialog “Open IP Range Text File…”
$IPs = Get-Content $FileName

$Rcnn = Get-ReceiveConnector “$ConnectorID”

foreach($IP in $IPs){
Write-Host “Adding IP Address :” $IP ” to “$ConnectorID -ForegroundColor Cyan

$Rcnn.RemoteIPRanges += $IP

}

Set-ReceiveConnector “$ConnectorID” -RemoteIPRanges $Rcnn.RemoteIPRanges

}

Reply

Andy Grogan November 27, 2016 at 11:41 pm

Glad that you thought of that … rather than the actual script that saved people time. Easy to critique, rather than actually contribute.

Reply

Gypsy Morrison June 9, 2017 at 4:16 pm

This is freakin great!!! Saved me so much time. Thank you soooooooooo much!!!

Reply

Jamie Chisholm September 5, 2017 at 4:35 pm

Had to update 882 IPs on a relay connector for MFCs to get rid of legacy values, confirmed this worked under Exchange 2016 CU3, nice script thank you.

Reply

Mike Cusumano September 21, 2017 at 8:15 pm

Thanks bro! Bad ass script. I only had about 40 entries but this saved tons of time. This worked on Exchange 2016 BTW.
Best,
Mike

Reply

Leave a Comment

Previous post:

Next post: