05 November 2014

Fill the receive connector from text file with PowerShell

Adding multiple ip addresses to a receive connector manually is a pain in the fingers.
That's when Powershell is your best mate:

Get all receive connector names, we need the name of the connector later in our command-let.
Get-ReceiveConnector

 Identity                                        Bindings                           Enabled
--------                                          --------                               -------
SR-xxxxx\Default SR-xxxx       {:::25, 0.0.0.0:25}             True
SR-xxxxx\Client SR-xxxx         {:::587, 0.0.0.0:587}         True
SR-xxxxx\Default SR-xxxx       {:::25, 0.0.0.0:25}             True
SR-xxxxx\Client SR-xxxx         {:::587, 0.0.0.0:587}         True
SR-xxxxx\SMTP relay              {1.1.1.1:25}                       True

If you execute the command below you overwrite all the previous ip addresses:

Set-ReceiveConnector "SMTP relay" -RemoteIPRanges 10.0.0.99

To add multiple IP addresses at once use this command sequence:
$Con = Get-ReceiveConnector "SMTP relay"
$Con.RemoteIPRanges += "10.0.0.99", "10.0.0.100", "10.0.0.101"
Set-ReceiveConnector "SMTP relay" -RemoteIPRanges $Con.RemoteIPRanges

Sometimes the list of IPs being added is too long to type out. To add multiple IP addresses from a text file called C:\Temp\newips.txt use this command sequence instead:
$Con = Get-ReceiveConnector "Relay Connector"
Get-Content C:\Temp\newips.txt | foreach {$Con.RemoteIPRanges += "$_"}
Set-ReceiveConnector "Relay Connector" -RemoteIPRanges $Con.RemoteIPRanges

Source

No comments:

Post a Comment