{"id":66,"date":"2014-07-01T20:26:47","date_gmt":"2014-07-01T12:26:47","guid":{"rendered":"http:\/\/kylemcdonald.com.au\/?p=66"},"modified":"2018-08-14T00:37:19","modified_gmt":"2018-08-13T16:37:19","slug":"powershell-script-to-add-ips-to-exchange-2013-receive-connectors","status":"publish","type":"post","link":"https:\/\/kylemcdonald.com.au\/2014\/07\/01\/powershell-script-to-add-ips-to-exchange-2013-receive-connectors\/","title":{"rendered":"Powershell script to add IP’s to Exchange 2013 Receive Connectors"},"content":{"rendered":"
Powershell script that allows you to add a single IP address to all Receive Connectors in your Exchange environment, optionally filtering on specific text in your Connector names. E.g. only Connectors with “relay” in their name.<\/p>\n
<\/p>\n
\r\n\r\n<#\r\n.DESCRIPTION\r\n Powershell script that allows you to add a single IP address to all Relay Receive Connectors in your Exchange environment.\r\n.PARAMETERS\r\n None - execute directly from the Exchange Management Shell.\r\n.Version\r\n 0.6\r\n.Author\r\n Kyle McDonald\r\n.Compatibility\r\n Exchange 2013\r\n.Release Date\r\n July 2015\r\n.Notes\r\nChange Log\r\n v0.1, 20140701 - Initial version\r\n v0.6, 20150721 - Added $MailHost and $RcvConnectorFilter\r\n#>\r\n\r\ncls\r\n\r\n$MailHost = \"mailhost.contoso.com\"\r\n\r\n# Text in Receive Connector name to filter on. Leave blank to apply to all Connectors\r\n# EG $RcvConnectorFilter = \"\"\r\n# EG $RcvConnectorFilter = \"relay\"\r\n$RcvConnectorFilter = \"relay\"\r\n\r\n# Check for an open connection to Exchange\r\n$OpenSession = Get-PSSession | Where-Object {$_.ComputerName -eq \"$MailHost\" -and $_.State -eq \"Opened\"} | select ComputerName -expandproperty ComputerName\r\nIF ([string]::IsNullOrEmpty($OpenSession)) \r\n {}\r\n ELSE \r\n { $Session = New-PSSession -ConfigurationName Microsoft.Exchange -ConnectionUri http:\/\/$MailHost\/PowerShell\/ -Authentication Kerberos ; Import-PSSession -disablenamechecking $Session }\r\n\r\n# Get all Receive Connectors\r\nWrite-Host \"\"\r\nWrite-Host \"Getting list of Receive Connectors...\"\r\nIF ([string]::IsNullOrEmpty($RcvConnectorFilter)) \r\n { $RcvConnectors = Get-ReceiveConnector | Select -ExpandProperty Identity }\r\n ELSE \r\n { $RcvConnectors = Get-ReceiveConnector | Where-Object {$_.Identity -like \"*$RcvConnectorFilter*\"} | Select -ExpandProperty Identity }\r\n\r\n# List Receive Connectors Found\r\nWrite-Host \"\"\r\nWrite-Host \"Receive Connectors Found:\" -ForegroundColor Cyan\r\n foreach ($element in $RcvConnectors) {Write-Host $element}\r\n\r\n# LoopMain Start\r\nDo {\r\n$userMenuChoice = \"y\"\r\n\r\n# Ask for IP that needs to be added\r\nWrite-Host \"\"\r\n $IPAddress = Read-Host \"Please enter the IPv4 Address to allow relay\"\r\n\r\n# Test IP is valid\r\n$pattern = '^(?:(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\\.){3}(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)$'\r\n$IPOK = $IPAddress -match $pattern\r\nIF ($IPOK -eq $false) {\r\n Write-Warning (\"IP Address {0} is not a valid IPv4 address.\" -f $IPAddress)\r\n $IPAddressCont = \"n\"\r\n }\r\n ELSE\r\n {Write-host \"IP Address\"$ipAddress \"is valid.\" -ForegroundColor Green\r\n # Prompt to continue\r\n Write-Host \"Are you sure you would like to add \" -NoNewLine ; Write-Host $IPAddress -ForegroundColor Green -NoNewLine ; $IPAddressCont = Read-Host \" to the above receive connectors? (Y\/N)\" \r\n }\r\n\r\nWrite-Host \"\"\r\n IF ($IPAddressCont -eq \"y\")\r\n { Write-Host \"Starting ...\" -ForegroundColor Green\r\n foreach ($element in $RcvConnectors) {\r\n Write-Host \"Adding IP Address:\" $IPAddress \" to \"$element -ForegroundColor Cyan\r\n $Rcnn = Get-ReceiveConnector \"$element\"\r\n $Rcnn.RemoteIPRanges += $IPAddress\r\n Set-ReceiveConnector \"$element\" -RemoteIPRanges $Rcnn.RemoteIPRanges\r\n }\r\n Write-Host \"Done!\" -ForegroundColor Cyan\r\n }\r\n ELSE\r\n { Write-Host \"Stopping!\" -ForegroundColor Red }\r\n\r\n#Give the user a choice to run the script again\r\ndo { Write-Host \"\"\r\n $userMenuChoice = read-host -prompt \"Would you like to do this again? (Y\/N)\"\r\n } until ($userMenuChoice -eq \"y\" -or $userMenuChoice -eq \"n\")\r\n\r\n}\r\nwhile ($userMenuChoice -eq \"y\")\r\n# LoopMain End\r\nWrite-Host \"Bye!\"\r\nWrite-Host \"\"\r\n\r\n<\/pre>\n","protected":false},"excerpt":{"rendered":"Powershell script that allows you to add a single IP address to all Receive Connectors in your Exchange environment, optionally filtering on specific text in your Connector names. E.g. only Connectors with “relay” in their name.<\/p>\n","protected":false},"author":3,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":[],"categories":[30],"tags":[],"yoast_head":"\n
Powershell script to add IP's to Exchange 2013 Receive Connectors - Kyle McDonald<\/title>\n\n\n\n\n\n\n\n\n\n\n\n\n\n