Search a computer across all Domain Controllers

Search a computer across all Domain Controllers

Search a computer across all Domain Controllers isn’t something I had to do in the past, but once, I had a little issue with a machine saying it was joined to a domain but it wasn’t in AD, even if the machine was joined 5 minutes earlier. I thought that perhaps it hit another Domain Controller and it was slowly replicating (this wasn’t the case). Still, out of curiosity I thought it’d be good to search a computer across all Domain Controllers; this is also a good option to see how fast the DCs are replicating content against each other.

Here’s the little script in Powershell:

#Get a list of all domain controllers
$dcs=Get-ADDomainController -Filter * | Select-Object name

#Computer to search
$computername = "mycomputer"

#Run a foreach cycle to Search a computer across all Domain Controllers
foreach ($dc in $dcs.Name) {
	#Try to run Get-ADComputer and write Computername - DC name, if it fails use Catch to write something else
	Try {	
			$temp = Get-ADComputer $computername -Server $dc | select Name
			write-host "$($temp.Name) - $($dc)"
		}
	
	Catch{write-host "Does not exist on $($dc)."}
}

Note: -ErrorAction Silently continue cannot be used with Get-ADComputer. To avoid getting errors every time the computer isn’t found on a certain Domain Controller I used Try/Catch. As you can see it tries (Try{}) to run the Get-ADComputer command, but if it returns an error, it’ll execute whatever is in Catch{}.

IT Droplets

IT Droplets