Thursday, August 15, 2013

Adding or Removing IP's from multiple relay connectors - Exchange 2010

I was working on a project where I had to migrate mailboxes, distribution groups and public folders cross forest from Exchange 2003 to Exchange 2010. When everything got finished,  the last piece of work was to move Exchange 2003 SMTP relay configuration to Exchange 2010.

Though it must have been mentioned in hundreds of articles/blogs over the Internet I thought even I should also share my experience with everyone :)

So, the first thing was to collect the list of IP's from Exchange 2003 from Forest A and for this purpose I used the Microsoft's vbscript which did a wonderful job!
I have mentioned the KB article below from where we can find the vbscript;
How to use the IPsec.vbs program to export an SMTP relay list from a computer that is running Exchange Server 2003

NOTE: command should not be copied and pasted, we must type the command on Exchange 2003 server(s).

As the above article is self explanatory I will not go into it in more details.

Now, the next challenge was to add the extracted IP's on our multiple Exchange 2010 HUB relay connectors in Forest B.

To achieve this task, I tried few things in powershell and finally managed to come up with a script which is doing a great job for us and saving us a lot of time too!

With the help of this script we are able to add/remove IP's on multiple connectors/multiple servers in one go. We have around 20 HUB servers with different types of relay connectors configured like authentication, external relay, internal relay Only.
When we run this script, it displays various options (as shown below) like taking backup of the IP's from relay connectors as it is very important to have a back up of IP's (thousands of IP actually in our scenario) before we make any changes and on what type of connector where we want to add the IP's.



This script codes are more suitable for my requirement but with little modification it can be used by anyone.
The basic function of adding and removing IP's remain the same. We have to just modify the input file and the name of the connectors in the codes and it will be ready to use!

This script codes are basically divided in three important sections;
1. Input files
2. Script code where it adds IP(s) on multiple connectors
3. Script code where it removes IP(s) from multiple connectors

Input file: 
a. Must have all the Hub server names, can easily be collected by running Get-transportserver | ft Name
$Hub = gc c:\Hubservers.txt
b. The other input file would be the list of IP's extracted from Exchange 2003 in below format;
$IPList = import-csv "c:\connectors info\ExternalConnector.csv" #Make a note this is in ".csv" format
LowerBound,UpperBound,RangeFormat
10.10.10.0,10.10.10.85,SingleAddress

NOTE: if we want to specify a range then the Range format should be mentioned as "LoHi"
e.g.: LowerBound,UpperBound,RangeFormat
       10.10.10.0,10.10.10.85,LoHi

Script block where it adds IP(s) on multiple connectors:

  foreach($i in $Hub)
   
     {
 $rc = "$i\SMTP Relay Connector"
 #      $i will be replaced by Hub server name from input file, so it will work on connector named
#      "HUB01\SMTP Relay Connector"   assuming one of the Hub server name is "HUB01"

   write-host "Adding IP's to $i\SMTP Relay Connector" -foregroundcolor Green

    foreach($line in $Iplist)
       {
            $ipAdd = $line.LowerBound
            $conn = Get-ReceiveConnector $rc
            $conn.RemoteIPRanges += $ipAdd

            Set-ReceiveConnector $rc -RemoteIPRanges $Conn.RemoteIPRanges

       }
     }

Script block where it removes IP(s) from multiple connectors:
Similarly, the above script code can also be used to remove IP's from multiple connectors in one go, the only difference is "-";
 
foreach($i in $Hub)
         {
 $rc = "$i\SMTP Relay Connector"

   write-host "Removing IP's from $i\SMTP Relay Connector" -foregroundcolor Green

    foreach($line in $iplist)
       {
            $ipAdd = $line.LowerBound
            $conn = Get-ReceiveConnector $rc
            $conn.RemoteIPRanges -= $ipAdd   # using "-" symbol

            Set-ReceiveConnector $rc -RemoteIPRanges $Conn.RemoteIPRanges

       }
     }

And to verify the list of IP's added, we can always extract the information from a connector by using below command and same can always be run before adding or removing IP's to extract list of IP's as backup;
 
  $hub = Read-host "Enter HUB Server Name"

  (Get-ReceiveConnector "$hub\SMTP Relay Connector").RemoteIPRanges | select Lowerbound,Upperbound,RangeFormat | sort-object Lowerbound | export-csv "c:\SMTP relay connector.txt" –NoTypeInformation