Add IP from hostname to Exchange Receive Connector


<# .SYNOPSIS Adds the IP address of a server to the SMTP relay .DESCRIPTION Gets the IP address of a list of servers and adds them to the Receive connector on the UK CAS servers. .USAGE The input CSV should have one server per line, no header and use the FQDN. .NOTES Version: 1.1 Authors: Jon Hadden Script: Add-IP-to-SMTP-Relay.ps1 Creation date: 06-02-2018 2018-02-06: 1.0 - Inital Script Creation 2018-02-08: 1.1 - Updated logging to record Computer name, IP address and any errors generated #>

<# .VARIABLES These variables are the only ones that may require editing. #>
$CASServer01 = "CAS-Server-01\Receive Connector"
$CASServer02 = "CAS-Server-02\Receive Connector"
$CASServer03 = "CAS-Server-03\Receive Connector"
$CASServer04 = "CAS-Server-04\Receive Connector"
$CASServer05 = "CAS-Server-05\Receive Connector"
$CASServer06 = "CAS-Server-06\Receive Connector"
$Domain1 = ".Organisation.com"
$Domain2 = ".subdomain.Organisation.com"
$Domain3 = ".TrustedDomain.com"

<# .FUNCTIONS Define the functions for later use in the script. #>

# Create the function for getting the csv file
Function Fn-Get-FileName($initialDirectory)
{
[System.Reflection.Assembly]::LoadWithPartialName("System.windows.forms") | Out-Null
$OpenFileDialog = New-Object System.Windows.Forms.OpenFileDialog
$OpenFileDialog.initialDirectory = $initialDirectory
$OpenFileDialog.filter = "CSV (*.csv)| *.csv"
$OpenFileDialog.ShowDialog() | Out-Null
$OpenFileDialog.filename
}

# Create a function for getting the error and writing to log
Function Fn-Get-Error
{
if ($Error -ne $null)
{
$ErrMsg = $Error[0].Exception.Message
$ErrTgt = $Error[0].CategoryInfo.TargetName
Write-Log "Error:$ErrMsg : $ErrTgt"
$Error.Clear()
}
}

# Function for writing to a log file
Function Write-Log {
[CmdletBinding()]
param(
[Parameter()]
[ValidateNotNullOrEmpty()]
[string]$Message
)
[pscustomobject]@{
Time = (Get-Date -f g)
Message = $Message
} | Export-Csv -Path "C:\Temp\$((Get-Date).ToString('yyyy-MM-dd'))_SMTP_Relay.log" -Append -NoTypeInformation
}

<# .SCRIPT Now start the script #>

# Check if C:\Temp exists and create if not
if (-not (Test-Path "C:\Temp"))
{
New-Item -ItemType directory -Path C:\Temp
}

# Import the csv
$path = Fn-Get-Filename
$ComputerList = Get-Content $path

# Initiate the Exchange 2010 tools & exit if not found
if (Test-Path $env:ExchangeInstallPath\bin\RemoteExchange.ps1)
{
Write-Host "Loading Exchange Management Tools."
Add-PSSnapin Microsoft.Exchange.Management.PowerShell.E2010;
. $env:ExchangeInstallPath\bin\RemoteExchange.ps1
}
else
{
Write-Host "Exchange Management Tools required. Please install the tools."
Exit
}

# Build an array with the Computer name and IP address
$SMTPAddresses = @()
foreach ($ComputerName in $ComputerList)
{
# Check if the FQDN is correct in the input file
if ($ComputerName -match $Domain1 -or $ComputerName -match $Domain2 -or $ComputerName -match $Domain3)
{
# Get the IP address from the Test-Connection result
$IPInfo = Test-Connection "$ComputerName" -count 1 | select @{Name="Computername";Expression={$_.Address}},Ipv4Address
$SMTPAddresses += $IPInfo
}
# If the FQDN is not found, exit the script
else
{
Write-Host "FQDN not found. Please use the FQDN in the input file."
Exit
}
}

# Get the Receive Connector information
$CAS01 = Get-ReceiveConnector $CASServer01
$CAS02 = Get-ReceiveConnector $CASServer02
$CAS03 = Get-ReceiveConnector $CASServer03
$CAS04 = Get-ReceiveConnector $CASServer04
$CAS05 = Get-ReceiveConnector $CASServer05
$CAS06 = Get-ReceiveConnector $CASServer06

# Get each IP and add it to the Receive connector on each Exchange CAS server
foreach ($Computer in $SMTPAddresses)
{
if ($CAS01.RemoteIPRanges -notcontains $($Computer.Ipv4Address.IPAddressToString))
{
Write-Log "Adding $($Computer.Computername) - $($Computer.Ipv4Address.IPAddressToString) to $CASServer01"
$CAS01.RemoteIPRanges += "$($Computer.Ipv4Address.IPAddressToString)"
Set-ReceiveConnector $CASServer01 -RemoteIPRanges $CAS01.RemoteIPRanges -ErrorAction SilentlyContinue
Fn-Get-Error
}
else
{
Write-Log "$($Computer.Computername) - $($Computer.Ipv4Address.IPAddressToString) already exists on $CASServer01"
}
if ($CAS02.RemoteIPRanges -notcontains $($Computer.Ipv4Address.IPAddressToString))
{
Write-Log "Adding $($Computer.Computername) - $($Computer.Ipv4Address.IPAddressToString) to $CASServer02"
$CAS02.RemoteIPRanges += "$($Computer.Ipv4Address.IPAddressToString)"
Set-ReceiveConnector $CASServer02 -RemoteIPRanges $CAS02.RemoteIPRanges -ErrorAction SilentlyContinue
Fn-Get-Error
}
else
{
Write-Log "$($Computer.Computername) - $($Computer.Ipv4Address.IPAddressToString) already exists on $CASServer02"
}
if ($CAS03.RemoteIPRanges -notcontains $($Computer.Ipv4Address.IPAddressToString))
{
Write-Log "Adding $($Computer.Computername) - $($Computer.Ipv4Address.IPAddressToString) to $CASServer03"
$CAS03.RemoteIPRanges += "$($Computer.Ipv4Address.IPAddressToString)"
Set-ReceiveConnector $CASServer03 -RemoteIPRanges $CAS03.RemoteIPRanges -ErrorAction SilentlyContinue
Fn-Get-Error
}
else
{
Write-Log "$($Computer.Computername) - $($Computer.Ipv4Address.IPAddressToString) already exists on $CASServer03"
}
if ($CAS04.RemoteIPRanges -notcontains $($Computer.Ipv4Address.IPAddressToString))
{
Write-Log "Adding $($Computer.Computername) - $($Computer.Ipv4Address.IPAddressToString) to $CASServer04"
$CAS04.RemoteIPRanges += "$($Computer.Ipv4Address.IPAddressToString)"
Set-ReceiveConnector $CASServer04 -RemoteIPRanges $CAS04.RemoteIPRanges -ErrorAction SilentlyContinue
Fn-Get-Error
}
else
{
Write-Log "$($Computer.Computername) - $($Computer.Ipv4Address.IPAddressToString) already exists on $CASServer04"
}
if ($CAS05.RemoteIPRanges -notcontains $($Computer.Ipv4Address.IPAddressToString))
{
Write-Log "Adding $($Computer.Computername) - $($Computer.Ipv4Address.IPAddressToString) to $CASServer05"
$CAS05.RemoteIPRanges += "$($Computer.Ipv4Address.IPAddressToString)"
Set-ReceiveConnector $CASServer05 -RemoteIPRanges $CAS05.RemoteIPRanges -ErrorAction SilentlyContinue
Fn-Get-Error
}
else
{
Write-Log "$($Computer.Computername) - $($Computer.Ipv4Address.IPAddressToString) already exists on $CASServer05"
}
if ($CAS06.RemoteIPRanges -notcontains $($Computer.Ipv4Address.IPAddressToString))
{
Write-Log "Adding $($Computer.Computername) - $($Computer.Ipv4Address.IPAddressToString) to $CASServer06"
$CAS06.RemoteIPRanges += "$($Computer.Ipv4Address.IPAddressToString)"
Set-ReceiveConnector $CASServer06 -RemoteIPRanges $CAS06.RemoteIPRanges -ErrorAction SilentlyContinue
Fn-Get-Error
}
else
{
Write-Log "$($Computer.Computername) - $($Computer.Ipv4Address.IPAddressToString) already exists on $CASServer06"
}
}