Thursday, 1 March 2012

Using PowerShell to find a local Domain Controller

I have a few scripts which perform a bunch of actions in series, and in order for the script to succeed it's important that all the actions are performed against the same Active Directory domain controller.

To ensure that this would happen I wrote a small function which finds all the DCs in the local site, tests to make sure each one is accessible, and then selects one of the available ones at random and returns the FQDN.