Were you a vbscript programmer? ☺

I don’t see anything wrong. I would simplify a bit…

Take

### Prompt for Server Name ###
[Console]::ForegroundColor = "cyan"
$Hostname = Read-Host 'Enter servername to be added to the SMTP Relay List'
[Console]::ResetColor()

And become

### Prompt for Server Name ###
Write-Host 'Enter servername to be added to the SMTP Relay List' 
–ForeGroundColor Cyan -NoNewLine
$Hostname = Read-Host

Take

[Console]::ForegroundColor = "yellow"
Write-Host "No Hostname entered..."
[Console]::ResetColor()

And become

Write-Host "No Hostname entered..." –ForeGroundColor Yelloow

This just makes the code (much) easier to read.

Also, I really don’t understand the point behind the “delay 10 seconds” prior 
to exiting the script.

From: [email protected] [mailto:[email protected]] On 
Behalf Of Sean Martin
Sent: Friday, December 5, 2014 8:54 PM
To: [email protected]
Subject: Re: [Exchange] PowerShell Help

So a little background might help...

I created the script because we were having continous issues with applications 
needing the ability to relay email not being configured on our specified 
receive connectors. We have 6 servers split between two sites (each hosting MB, 
HT, CAS roles), with one server in each site being designated the relay server. 
We use SRM in our environment to failover/migrate virtual machines between our 
sites. With that process VMs are configured with new IP addresses when moved to 
the alternate site, so we've run into issues where a VM has been failed over 
but the "failover IP address" hasn't been added to the receive connector for 
relay. Although applications only target one server in each site for relay, we 
keep the connector configurations updated on all of the Exchange servers just 
in case one has to stand in for that purpose. So I wanted to create an easy way 
for our less experienced admins to take a request for relay access and ensure 
all of the relevant information was gathered and configured on all of the 
receive connectors.

So with all of that said, here's the script I came up with. I decided to not to 
pursue functionality outside of the EMS since most of the Analysts responsible 
for this task will have the EMS installed.


### Load Exchange Shell ###
. 'C:\Program Files\Microsoft\Exchange Server\V14\bin\RemoteExchange.ps1'
Connect-ExchangeServer -auto

### Variables ###
$ErrorActionPreference = "SilentlyContinue"
$Ping = New-Object System.Net.NetworkInformation.Ping

### Prompt for Server Name ###
[Console]::ForegroundColor = "cyan"
$Hostname = Read-Host 'Enter servername to be added to the SMTP Relay List'
[Console]::ResetColor()

### Prompt for IP Address ###
if ($hostname -eq "") {
 [Console]::ForegroundColor = "yellow"
 Write-Host "No Hostname entered..."
 [Console]::ResetColor()
 $blnIsValid = $False
 Do {
  [Console]::ForegroundColor = "cyan"
  $IPAddr = Read-Host 'Please enter primary IP address'
  [Console]::ResetColor()

### Validate IP Address ###
  $IPAddrObj = [System.Net.IPAddress]::parse($IPAddr)
  $isValidPriIP = [SYstem.Net.IPAddress]::tryparse([string]$IPAddr, 
[ref]$IPAddrObj)
  if ($isValidPriIP) {
   Write-Host "$IPAddr is a valid IP address"
   $blnIsValid = $True
  } else {
   [Console]::ForegroundColor = "red"
   Write-Host "ERROR: $IPAddr is not a valid IP address!"
   $blnIsValid = $False
   [Console]::ResetColor()
   }
 } While ($blnIsValid -eq $False)
} else {

### Obtain IP address from hostname ###
 $HostIP = [System.Net.DNS]::GetHostAddresses("$Hostname")
 if ($HostIP -eq $null) {
  [Console]::ForegroundColor = "red"
  Write-Host "ERROR: The DNS lookup failed!"
  [Console]::ResetColor()
  Write-Host "Script will terminate in 10 seconds..."
  Start-Sleep -s 10
  exit
 } else {
  Write-Host "Converting IP address to string..."
 }
}

### Convert null Host variables to IP ###
if ($Hostname -eq "") {
 $HostIPConv = $IPAddr
 $Hostname = $IPAddr
} else {

### Convert IP Address to String ###
$HostIPConv = $($Ping.Send($Hostname).Address).IPAddressToString
}

### Connectivity test for host IP ###
$HostIPTest = Test-Connection $HostIPConv -count 1 -quiet
if ($HostIPTest -eq $false) {
 [Console]::ForegroundColor = "yellow"
 $HostIPTestResp = Read-Host 'The IP address'$HostIPConv' is not responding, 
would you like to continue? Enter Y or N'
 [Console]::ResetColor()
 }
 if ($HostIPTestResp -eq "N") {
  Write-Host "The script will terminate in 10 seconds..."
  Start-Sleep -s 10
  exit
} else {
 Write-Host "Adding $hostname to relay list..."
}

### Retrieve existing list of relay IPs ###
$RecvConns = Get-ReceiveConnector | where {$_.name -eq "SMTP Relay Connector"}

### Save Changes to SMTP Relay Connectors ###
forEach ($RecvConn in $RecvConns) {
 Write-Host "Updating", $RecvConn.Identity
 $RecvConn.RemoteIPRanges += $HostIPConv
 Set-ReceiveConnector $recvConn -RemoteIPRanges $RecvConn.RemoteIPRanges
}

### Prompt for alternate IP Address ###
$blnIsValid = $False
Do {
 [console]::ForegroundColor = "cyan"
 $AltIP = Read-Host 'Enter failover IP address'
 [Console]::ResetColor()
 if ($AltIP -eq "") {
  [console]::ForegroundColor = "yellow"
  Write-Host "No failover IP address entered..."
  [console]::ForegroundColor = "green"
  Write-Host "Relay list Updates are complete!"
  [Console]::ResetColor()
  Write-Host "Script will terminate in 10 seconds..."
  Start-Sleep -s 10
  exit
 } else {

### Validate IP Address ###
  $AltIPObj = [System.Net.IPAddress]::parse($AltIP)
  $isValidIP = [SYstem.Net.IPAddress]::tryparse([string]$AltIP, [ref]$AltIPObj)
  if ($isValidIP) {
   Write-Host "$AltIP is a valid IP address"
   $blnIsValid = $True
  } else {
   [console]::ForegroundColor = "red"
   Write-Host "ERROR: $AltIP is not a valid IP address!"
   $blnIsValid = $False
   [Console]::ResetColor()
   }
 }
} While ($blnIsValid -eq $False)

### Connectivity test for failover IP (failover IPs should not respond unless 
application is currently failed over) ###
$FailIPTest = Test-Connection $AltIP -count 1 -quiet
if ($FailIPTest -eq $true) {
 [Console]::ForegroundColor = "yellow"
 $AltIPTestResp = Read-Host 'The IP address'$AltIP' is responding to ping, 
please verify you have obtained a valid failover IP address. Would you like to 
continue? Enter Y or N'
 [Console]::ResetColor()
}
 if ($AltIPTestTesp -eq "N") {
  Write-Host "The script will terminate in 10 seconds..."
  Start-Sleep -s 10
  exit
} else {
 Write-Host "Adding $AltIP to relay list..."
}

### Retrieve existing list of relay IPs ###
$RecvConns = Get-ReceiveConnector | where {$_.name -eq "SMTP Relay Connector"}

### Save Changes to connectors ###
forEach ($RecvConn in $RecvConns) {
 Write-Host "Updating", $RecvConn.Identity
 $RecvConn.RemoteIPRanges += $AltIP
 Set-ReceiveConnector $recvConn -RemoteIPRanges $RecvConn.RemoteIPRanges
}
 [Console]::ForegroundColor = "green"
 Write-Host "Relay list updates are complete!"
 [Console]::ResetColor()
 Write-Host "Script will terminate in 10 seconds..."
 Start-Sleep -s 10
 exit

Reply via email to