Sync BackConnectionHostNames across the farm

Published on Tuesday, 24 October 2023

How to sync BackConnectionHostNames across the farm

Sometimes a customer does not have a GPO in place for setting BackConnectionHostNames. In these cases one has to manually connect to each server in the farm, open RegEdit and set the values accordingly, every time a dns name of the farm changes.

I threw together a hacky script to sync BackConnectionHostNames from one server in the farm to all the others. So, now, one needs to only set BackConnectionHostNames on one server, then "sync" it to all others.

This script has no error checks whatsoever. If you are going to use it, keep in mind that there are most likely many cases which I have not forseen and the script will most likely blow up in your face.

#requires -pssnapin Microsoft.SharePoint.PowerShell
	Sync BackConnectionHostNames from one server in the farm to all others

	Does all the hard work

$errorActionPreference = "Stop"

$fqdn = [System.Net.Dns]::GetHostByName($env:computerName).HostName.ToLower()
$parts = $fqdn.split('.', 2)
$hostName = $parts[0]
$domain = $parts[1]

$val = Get-ItemPropertyValue -Path HKLM:\SYSTEM\CurrentControlSet\Control\Lsa\MSV1_0 -PSProperty BackConnectionHostNames
$backConnectionHostNames = $val | ? { -not ($_ -like "$hostName") -and -not ($_ -like "$($hostName).*") }
Write-Host "Current BackConnectionHostNames:"
write-host $backConnectionHostNames

$servers = (Get-SPFarm).Servers | ? { $_.Role -ne "Invalid" } | select -ExpandProperty Address | ? { -not ($_ -like $hostname) }
Write-Host "Other servers:"
write-host $servers

$servers | % {
	$s = $_
	$block = {
		$exists = ((Get-ItemProperty -Path HKLM:\SYSTEM\CurrentControlSet\Control\Lsa\MSV1_0) | select -ExpandProperty name) -contains "BackConnectionHostNames"
		if ( -not $exists ) {
			New-ItemProperty -Path HKLM:\SYSTEM\CurrentControlSet\Control\Lsa\MSV1_0 -PSProperty BackConnectionHostNames -Type MultiString -Value "" | Out-Null
		$old = Get-ItemPropertyValue -Path HKLM:\SYSTEM\CurrentControlSet\Control\Lsa\MSV1_0 -PSProperty BackConnectionHostNames
		$localFqdn = [System.Net.Dns]::GetHostByName($env:computerName).HostName.ToLower()
		$localHostName = $localFqdn.split('.', 2)[0]
		$newVal = @($localHostName, $localFqdn)
		$using:backConnectionHostNames | % { $newVal += $_ }
		Write-Host ""
		Write-host "-= $($using:s) =-"
		write-Host "old val: $old"
		write-Host "new val: $newVal"
		Set-ItemProperty -Path HKLM:\SYSTEM\CurrentControlSet\Control\Lsa\MSV1_0 -PSProperty BackConnectionHostNames -Value $newVal

	Invoke-command -computerName $s -ScriptBlock $block