Thursday, January 26, 2012

Finding a domaincontroller in powershell (without add ons and AD module)

I was looking for a good way to find the nearest 2008 domain controller in relation to a new server that is being prepared to be promoted.  Since the Active Directory Powershell module had not been installed yet (and was failing to install due to some other pending install), I needed to find an alternative to:

(get-addomaincontroller -mimumdirectoryserviceversion Windows2008 -discover -avoidself -nextclosestsite).hostname

I considered .NET's active directory collection, but the methods to find domain controllers with that, have really limited locator options.  I didn't see any method for finding only 2008 domain controllers and higher, so I fell back to command line tools.

This can be done with the nltest.exe:

(nltest / /DS_6 /avoidself)[0].substring(17)

It is a bit messy, and hackish with the substring, but it works just fine as long as you have a 2008 dc in that domain.  Otherwise, it returns a single string instead of an array, and you can see the "Cannot index into a null array" exception.

No comments:

Post a Comment