Which Servers Are Using a Particular DNS Server

Powershell, WMI

Which Servers Are Using a Particular DNS Server

I have to change the IP address of one of our domain controllers. This process in itself is pretty straight forward. Change the IP, run ‘ipconfig /registerdns’ and then run ‘dcdiag /fix’.

Changing the IP address of the DHCP scopes DNS configuration is pretty straight forward too. There are only 7 DHCP ranges using this DNS server so I will just manually edit the scopes.

But what about the servers that have been configured to use the IP address of the DC for DNS? How do I even know what servers are using that DNS server? I wrote the following script to query the OU where all servers are stored and then run a wmi query against each one, so here is the run down.

1
2
3
4
if (!(Get-PSSnapin Quest.ActiveRoles.ADManagement -ErrorAction Silentlycontinue)) 
  {Add-PSSnapin Quest.ActiveRoles.ADManagement} 
 
$allServers = Get-QADComputer -SearchRoot "OU=Servers,DC=domain,DC=com"

Firstly we load the Quest ActiveRoles Management snapin and do a search using Get-QADComputer, and drop it into the varilable $allServers.

Next we are setting up the loops to search through the results, run a WMI query and put the results in a variable. I have been reading about try, catch, finally and I thought I would give it a go here. It isn’t really necessary as a standard foreach-object loop will give the error and continue anyway. But this way I get to learn a bit more!

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
$serversToChange = @()
 
foreach ($server in $allservers){
	try {
		Write-Host "Testing" $server.Name -ForegroundColor Green
		if (Get-WmiObject -class Win32_networkAdapterConfiguration -filter IPEnabled=TRUE -computername $server.Name -EA stop | where {$_.DNSServerSearchOrder -match "192.168.0.1"}) {
			Write-Host $server.Name "is using server01 as a DNS server" -ForegroundColor Green
			$serversToChange += $server.Name
		}
		else {
			Write-Host $server.Name "is not using yourDNSServer as a DNS server" -ForegroundColor RED
		}
	}
	Catch [System.Management.Automation.ActionPreferenceStopException] {
		Write-Host "Caught a StopExecution with" $server.name
		$Error[0]
	}
	finally {
		continue
	}
}

At the end of the day I only had 1 server I needed to change, but at least I know it is only one!

Leave a Reply

Your email address will not be published. Required fields are marked *