Fill Exchange 2010 receive connector automatically

As a best practice to send SMTP traffic to your Exchange 2010 organization, you should create a receive connector with IP address limitations. Afterwards you add the hosts (servers or clients) which are allowed to send mail traffic to the receive connector. Imagine you have 600 servers and servers are added every day. It will be a challenging task to never forget to add the 1 servers which has been recently installed. So lets make life easy.

Imagine you have all your servers somewhere listed in a SQL database (home-made, our in a 3th party application), you could use them to fill the Receive Connector on a schedule. You will have to add the task on every server which needs the receive connector.

To run the script, a scheduled task can be created on a server which needs the receive connector:

Program/Script: C:\exchangeScripts\serverdb_receiveconnector.ps1
Start In: C:\Program Files\Microsoft\Exchange Server\V14\Bin

The following 2 scripts should be made available on the system:

C:\serverdb_receiveconnector.bat:

Powershell.exe -PSConsoleFile “C:\Program Files\Microsoft\Exchange Server\V14\Bin\exshell.psc1″ -command “C:\serverdb_receiveconnector.ps1″

Now the actual script called “serverdb_receiveconnector.ps1″. Replace the $query parameter with the query which applies to your database, use the correct server/database/user/password to access the database.

#Receive Connector
$rc=”Test Receive Connector”
$query=”SELECT server_name FROM Servers”
$connstring=”server=FQDNSQLServer;database=DatabaseName;trusted_connection=false;UID=UserToAccessDB;PWD=PlainTextPasswordHere”
$Connection = New-Object System.Data.SQLClient.SQLConnection
$Connection.ConnectionString = $ConnString
$Connection.Open()
$Command = New-Object System.Data.SQLClient.SQLCommand
$Command.Connection = $Connection
$Command.CommandText = $Query
$Reader = $Command.ExecuteReader()
$Counter = $Reader.FieldCount

$conn = Get-ReceiveConnector $rc
Set-ReceiveConnector $rc -RemoteIPRanges “255.255.255.255″
while ($Reader.Read()) {
for ($i = 0; $i -lt $Counter; $i++) {
$ipadd=$Reader.GetValue($i)

Try{
$ipaddtranslated=[System.Net.Dns]::GetHostAddresses($ipadd)
}Catch{
write-host “DNS lookup failed for ” + $ipadd
}

Try{
$conn.RemoteIPRanges += “” + $ipaddtranslated
}Catch{
write-host “Add failed for ” + $ipadd
}

}
}
Set-ReceiveConnector $rc -RemoteIPRanges $Conn.RemoteIPRanges
$Connection.Close()

Advertisement

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Connecting to %s

Follow

Get every new post delivered to your Inbox.