Quick Tip–adding multiple Remote IP Addresses to Receive Connectors…
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:
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).
You will then be prompted to locate your IP Range text file – browse to it and then click on the “Open” button.
The script will then process each host entry and add it to the selected Receive Connector.
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.
I hope that you find this post and script useful, and that it might help automate this process for someone else in the future.
Skip
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…
Andy Grogan
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
Tony
Thank you so much for providing this script. It saved me lots of valuable time during these stressful hours.
Duane
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 ….
Andy Grogan
Hiya Duane, please see the comment that I have added onto Skip’s. Cheers
Duane
Hi Andy,
Thank you very much for your reply. It’s very helpful for my work 😀
Cheers.
Wade
This is an awesome Script!! Thank you for saving me so much time!!!!
AnonMan
Like the script, but it doesn’t work for me – get the same error as Skip and Duane.
This is on Exchange 2007 SP3.
Andy Grogan
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
Paul
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?
Andy Grogan
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
Andy Grogan
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
Steve
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
Terry
This has been a wonderful script and saved SO much time when moving from 2003 to 2010!! Thank you.
Ron
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
Mo
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
Steve
Did you get an answer on this? Many of these that have been written are for those who do not have current IP’s. I can’t risk losing them.
ccampo
Very useful … also works perfect in Exchange 2013
Jerry
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
}
Gypsy Morrison
This is freakin great!!! Saved me so much time. Thank you soooooooooo much!!!
Jamie Chisholm
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.
Mike Cusumano
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
Chuck Williams
Hi Team,
I am trying the script on 2013 Version 15.0, but it is not working. It stops at the screen “Please select the Recieve Connector that you wish to work with:” Also there are none of our RCs listed under “Detected Receive Connectors”
Any ideas?..thanks a ton.
Chuck Williams
Actually this is the full output:
Get-ReceiveConnector : The term ‘Get-ReceiveConnector’ is not recognized as the name of a cmdlet, function, script
file, or operable program. Check the spelling of the name, or if a path was included, verify that the path is correct
and try again.
At C:\Users\wich6001\Desktop\BulkImportRemoteIPonRecConn.ps1:44 char:14
+ $RecConns = Get-ReceiveConnector | Select -ExpandProperty Identity
+ ~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : ObjectNotFound: (Get-ReceiveConnector:String) [], CommandNotFoundException
+ FullyQualifiedErrorId : CommandNotFoundException
Bulk Import of Remote IP Addresses for Exchange Receive Connectors
Version 0.1
http://www.telnetport25.com
Detected Receive Connectors:
Please select the Receive Connector that you wish to work with.:
Chuck Williams
You can disregard..I was running from regular PS instead of from Exchange Management Shell..now getting the list of RCs.
Adam Campbell
Just added 140 addresses to a EX 2016 connector, thanks a lot!!