Getting the DN for an individual computer is not a difficult task in PowerShell, ex:
Import-Module activedirectory
$(Get-ADComputer $env:COMPUTERNAME).distinguishedName
Doing this fast for a long list of machines in a big forest is less easy. Get-ComputerDN.ps1 gets the distinguished name of a list of computers using WorkFlows. I spent some time trying out how to figure out the best way to do Active Directory Global Catalog searches in an Active Directory forest with many domains, such as level2.level1.root.com. I don’t want to query root.com when I know that the computer account exists under level1.root.com. Similarly, I don’t want to query outside my domain, level2.levels1.root.com when I know the computer account exists there.
What this script did for me was to get some hands on with parallel processing using workflows and to help me to decide how I want to manage Global Catalog queries in the future. The original version of this script looked pinged the computer name and looked for the DN. This version is much more sophisticated. If you give it a list of NetBIOS names it will figure out the FQDN based on the LDAP query. It will figure out the closest Global Catalog Server for the domain you are querying by using the ping response time. I am using ParameterSetName to manage the choice of the domain level to query. You can see from the commented code I default to the forest, but let the user select the current domain or the parent domain. I also accept either a computer name or a file path as input. Unlike prior versions, the script will always ping the computer even if it is not in AD. It requires PS Version 3.
Updated: 1/30/14 to fix ping problems. 2/15/14 major rewrite.