PowerShell script to set Exchange Static RPC Ports

SNAGHTML1ca684c

I am currently working on an Exchange migration from 2003 to 2010. For the implementation of a load balancing solution for the CAS/HUB servers I needed to set Static RPC Ports for the RPC Client Access Service and the Exchange Address Book Service.

The procedure of changing these ports is described on the Technet Wiki: Configure Static RPC Ports on an Exchange 2010 Client Access Server

Since I am lazy I decided to do this with a PowerShell script that would automatically do this for all CAS/HUB servers in my 2010 environment.

First we need to load the Exchange Management SnapIns:

# Add Exchange Snapins
if ((Get-PSSnapin | where {$_.Name -match "Exchange.Management"}) -eq $null) { Add-PSSnapin Microsoft.Exchange.Management.* }

Then I have defined the registry keys and port numbers (Microsoft recommends any port between 59531 and 60554, I am using 60200 and 60201):

Update: The address book service uses a REG_SZ key and not a REG_DWORD so I changed $ABPort to a string

# The keys, see http://social.technet.microsoft.com/wiki/contents/articles/configure-static-rpc-ports-on-an-exchange-2010-client-access-server.aspx
$RPCKey = "SYSTEM\CurrentControlSet\Services\MSEXchangeRPC\ParametersSystem"
$ABKey = "SYSTEM\CurrentControlSet\Services\MSEXchangeAB\Parameters"

# Value Names
$RPCValue = "TCP/IP Port"
$ABValue = "RpcTcpPort"

# The port numbers
$RPCPort = 60200
$ABPort = "60201"  # Address Book uses REG_SZ and not REG_DWORD!

Then I use the Get-ClientAccessServer cmdlet to get all the CAS servers and set the values in a foreach loop:

foreach ($CasServer in Get-ClientAccessServer)
{
	# Open Remote Registry
	$reg = [Microsoft.Win32.RegistryKey]::OpenRemoteBaseKey('LocalMachine', $CasServer)
	# Open Subkey with Write Access
	$regKey = $reg.OpenSubKey($RPCKey, $True)

	# If the key doesn't exist we will create it!
	if (!$regKey)
	{
		$reg.CreateSubKey($RPCKey)
		$regKey = $reg.OpenSubKey($RPCKey, $True)
	}
	# Write Value
	$regKey.SetValue($RPCValue, $RPCPort)

	# Dump Written Value (check)
	Write-Host $CasServer.Name $regKey.Name $RPCValue $regKey.GetValue($RPCValue)

	$regKey = $reg.OpenSubKey($ABKey, $True)
	if (!$regKey)
	{
		$reg.CreateSubKey($ABKey)
		$regKey = $reg.OpenSubKey($ABKey, $True)
	}
	$regKey.SetValue($ABValue, $ABPort)

	# Dump Written Value (check)
	Write-Host $CasServer.Name $regKey.Name $ABValue $regKey.GetValue($ABValue)
}