How to add bulk number of IP's into the receive connector of exchange server using powershell

Surya kumar 340 Reputation points
2023-10-27T13:12:49.8133333+00:00

Hello,

I am looking for PowerShell command where I can add bulk number of IP's into the receive connector of exchange server.

I am having problem with below command where the alignment is great concern. All the IP's have to separated by comma.

$recCon = get-receiveconnector "EX1\relay"

$recCon.remoteIPRanges += "1.1.1.1", "1.1.1.2"
set-receiveconnector $recCon -remoteIPranges $recCon.remoteIPRanges

Is there any other way where I can use if I have 50 ips in a csv file / txt file?

Exchange Server
Exchange Server
A family of Microsoft client/server messaging and collaboration software.
1,173 questions
Exchange Server Development
Exchange Server Development
Exchange Server: A family of Microsoft client/server messaging and collaboration software.Development: The process of researching, productizing, and refining new or existing technologies.
526 questions
Exchange Server Management
Exchange Server Management
Exchange Server: A family of Microsoft client/server messaging and collaboration software.Management: The act or process of organizing, handling, directing or controlling something.
7,469 questions
Microsoft Exchange
Microsoft Exchange
Microsoft messaging and collaboration software.
446 questions
Microsoft Exchange Hybrid Management
Microsoft Exchange Hybrid Management
Microsoft Exchange: Microsoft messaging and collaboration software.Hybrid Management: Organizing, handling, directing or controlling hybrid deployments.
1,981 questions
{count} votes

Accepted answer
  1. Yuki Sun-MSFT 41,006 Reputation points
    2023-10-30T06:02:00.8866667+00:00

    Hi @Surya kumar ,

    Prepare the CSV file for the IPs you want to add like below:

    User's image

    Then you can use the following scripts to import the IPs without overwriting the existing ones. Be sure to replace the first two lines with the file path and connector identity in your environment:

    $Csv = "C:\temp\IPs.csv" 
    $RC = "EX19\TEST" 
    $IPs = Import-Csv $Csv   
    
    $RCon = Get-ReceiveConnector $RC 
    
    $RemoteIPRanges = $RCon.RemoteIPRanges 
    
    foreach ($IP in $IPs) {     
    
      $IPEx = $IP.Expression       
    
        if ($RemoteIPRanges -contains $IPEx) {
    
           Write-Host "IP address $($IPEx) already exist in receive connector $($RC)" -ForegroundColor Red     
    
      }
    
       else {
    
          $RemoteIPRanges += $IPEx
    
                 Set-ReceiveConnector $RC -RemoteIPRanges $RemoteIPRanges  
    
          Write-Host "IP address $($IPEx) added to receive connector $($RC)" -ForegroundColor Green 
    
       } 
    
    }
    

    I tried testing in my lab environment and can verify that it can work properly:

    User's image

    User's image

    Here's a blog with more detailed instructions and script samples for your reference: Import remote IP addresses to Exchange receive connector.
    (Please Note: Since the web site is not hosted by Microsoft, the link may change without notice. Microsoft does not guarantee the accuracy of this information.)


    If the answer is helpful, please click "Accept Answer" and kindly upvote it. If you have extra questions about this answer, please click "Comment".

    Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.

    1 person found this answer helpful.

2 additional answers

Sort by: Most helpful
  1. Andy David - MVP 144.4K Reputation points MVP
    2023-10-27T16:15:46.26+00:00

    Put them in a column in a text file and call it IPs.txt for example

    Then:

    set-receiveconnector $recCon -remoteIPranges (Get-Content .\IPs.txt)

    TEST FIRST with a few IPs

    1 person found this answer helpful.

  2. Andy David - MVP 144.4K Reputation points MVP
    2023-10-27T16:57:35.4666667+00:00

    Yes, you didnt mention you have existing :)

    So either add those to the text file as well or:

    $RecvConn = Get-ReceiveConnector -Identity "EX1\relay"
    Get-Content "IPs.txt" | foreach {$RecvConn.RemoteIPRanges += "$_"}
    Set-ReceiveConnector "EX1\relay" -RemoteIPRanges $RecvConn.RemoteIPRanges

    TEST FIRST

    1 person found this answer helpful.