A simple script to compliment a previous post
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 |
<# .NAME Remove-RelayIP-Exchange2013.ps1 .DESCRIPTION Powershell script that allows you to remove a single IP address from all Relay Receive Connectors in your Exchange environment. .PARAMETERS None - execute directly from the Exchange Management Shell. .Version 0.1 .Author Kyle McDonald .Compatibility Exchange 2013 .Release Date July 2014 #> cls $MailHost = "mailhost.contoso.com" # Text in Receive Connector name to filter on. Leave blank to apply to all Connectors # EG $RcvConnectorFilter = "" # EG $RcvConnectorFilter = "relay" $RcvConnectorFilter = "relay" # Check for an open connection to Exchange $OpenSession = Get-PSSession | Where-Object {$_.ComputerName -eq "$MailHost" -and $_.State -eq "Opened"} | select ComputerName -expandproperty ComputerName IF ([string]::IsNullOrEmpty($OpenSession)) {} ELSE { $Session = New-PSSession -ConfigurationName Microsoft.Exchange -ConnectionUri http://$MailHost/PowerShell/ -Authentication Kerberos ; Import-PSSession -disablenamechecking $Session } # Get all Receive Connectors Write-Host "" Write-Host "Getting list of Receive Connectors..." IF ([string]::IsNullOrEmpty($RcvConnectorFilter)) { $RcvConnectors = Get-ReceiveConnector | Select -ExpandProperty Identity } ELSE { $RcvConnectors = Get-ReceiveConnector | Where-Object {$_.Identity -like "*$RcvConnectorFilter*"} | Select -ExpandProperty Identity } # List Receive Connectors Found Write-Host "Receive Connectors Found:" -ForegroundColor Cyan foreach ($element in $RcvConnectors) {Write-Host $element} # Ask for IP that needs to be removed Write-Host "" $IPAddress = Read-Host "Please enter the IP Address to remove from relay" # Test IP is valid $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]?)$' $IPOK = $IPAddress -match $pattern IF ($IPOK -eq $false) { Write-Warning ("IP Address {0} is not a valid IPv4 address." -f $IPAddress) $IPAddressCont = "n" } ELSE {Write-host "IP Address"$ipAddress "is valid." -ForegroundColor Green # Prompt to continue Write-Host "Are you sure you would like to remove " -NoNewLine ; Write-Host $IPAddress -ForegroundColor Green -NoNewLine ; $IPAddressCont = Read-Host " from the above receive connectors? (Y/N)" } Write-Host "" IF ($IPAddressCont -eq "y") { Write-Host "Starting ..." -ForegroundColor Green foreach ($element in $RcvConnectors) { Write-Host "Removing IP Address:" $IPAddress " from "$element -ForegroundColor Cyan $Rcnn = Get-ReceiveConnector "$element" $Rcnn.RemoteIPRanges -= $IPAddress Set-ReceiveConnector "$element" -RemoteIPRanges $Rcnn.RemoteIPRanges } Write-Host "Done!" -ForegroundColor Cyan } ELSE { Write-Host "Stopping!" -ForegroundColor Red } #Give the user a choice to run the script again do { Write-Host "" $userMenuChoice = read-host -prompt "Would you like to do this again? (Y/N)" } until ($userMenuChoice -eq "y" -or $userMenuChoice -eq "n") } while ($userMenuChoice -eq "y") # LoopMain End Write-Host "Bye!" Write-Host "" |
Hi,
it is a good script.
Thanks.