Programmatically Controlling DNS Dynamic Registration on Windows. She may not look like much, but she’s got it where it counts, kid.

I had someone ask me the other day about an issue they were having with regards to multi-homed Windows servers and the Microsoft DNS Server product with dynamic registration enabled. I created a VBScript file to address the issue, which I’ll explain in this post. As for the quote in the title, this quote I like as you can write very simple VBScript/PowerShell/Perl/Shell scripts that don’t look like much but are incredibly effective.

This issue has existed for years and there have been many ways of solving it in that time. I’ve created a script to fix this issue, which can be added as a startup script via Group Policy.

By default, Windows will attempt to register the IP address of all interfaces dynamically. When you have an enterprise environment with multiple server interfaces (Data, Management, Backup perhaps), there is a need to instruct the non-primary interface to not register its IP address in DNS. As a server will only have a single default gateway/route, the script below assumes that this is the primary interface and that all other interfaces should have DNS registration disabled.

This is generally fixable by making a change to each interface. A configuration change on the DNS tab of the Advanced screen in TCP/IP v4 interface properties needs to be made, by simply deselecting ‘Register this connection’s addresses in DNS.’ Ideally this should be fixed in the auto-provisioning tool, but if you don’t have that option then this is a perfectly valid approach.

The VBScript uses WMI to determine which interface has a default gateway/route configured (I say gateway or route as you can use either an interface default gateway or a static route to to achieve the same purpose). Once the interface is determined, it has its per interface registry entry updated to enable DNS registration. If the interface does not have a gateway, then DNS registration is disabled.

Just a couple of points to mention on the script.

  1. Always use Option Explicit. Option Explicit forces you to define all variables. The default behaviour without this command is for variables to be defined on first use. So if someone makes a typo on a variable name (never!), it will not complain, it will just create a new variable. That can be difficult to track down. This command will tell you if you’ve failed to define a variable.
  2. On Error Resume Next. This command is necessary as the script is being executed automatically in a Group Policy and I don’t want errors displayed if something goes wrong.
  3. WMI. It’s awesome. Use it as often as you can. I use it here to find the interface with the default gateway/route.
  4. The ipconfig /registerdns at the end forces a DNS registration to occur (as you’d expect). This will also instruct the host to deregister any records it has previously created for interfaces no longer configured to register their address. So in fact this script will fix existing registrations without you needing to manually delete records. Handy.
Option Explicit

On Error Resume Next

Const HKEY_LOCAL_MACHINE = &H80000002

Dim oRegistry, oNetworks, oNetwork, oShell
Dim strInterfaceKey, strData

strInterfaceKey = "SYSTEM\CurrentControlSet\Services\Tcpip\Parameters\Interfaces"

Set oShell = CreateObject("WScript.Shell")
Set oRegistry = GetObject("winmgmts:{impersonationLevel=impersonate}!\\.\root\default:StdRegProv")
Set oNetworks = GetObject("winmgmts:\\.\root\CIMV2").ExecQuery("SELECT * FROM Win32_NetworkAdapterConfiguration WHERE IPEnabled = TRUE")

For Each oNetwork In oNetworks
	If Not IsNull(oNetwork.IPAddress) Then
		If Not IsArray(oNetwork.DefaultIPGateway) Then
			strData = "0" ' Disabled
			strData = "1" ' Enabled
		End If

		oRegistry.SetDWORDValue HKEY_LOCAL_MACHINE, strInterfaceKey & "\" & oNetwork.SettingID, "RegistrationEnabled", strData
	End If

oShell.Run "ipconfig /registerdns"

Set oRegistry = Nothing
Set oNetworks = Nothing
Set oShell = Nothing

I’ve uploaded the script here.

~ Mike

3 thoughts on “Programmatically Controlling DNS Dynamic Registration on Windows. She may not look like much, but she’s got it where it counts, kid.

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s