Alan's Blog

"Yeah. I wrote a script that will do that."

Get Distinguished Name of Many computers in PowerShell (Revised)

Posted on February 15th, 2014

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.

Filed under Active Directory, PowerShell, Scripting |

Leave a Reply

You must be logged in to post a comment.

Please Note

All the scripts are saved as .txt files. Newer files have a "View Script" button which will let you save or open a script in notepad. For earlier posts, the easiest way to download with IE is to right click on the link and use "Save Target As". Rename file from Name_ext.txt to Name.ext.

To see a full post after searching, please click on the title.

PowerShell Scripts were written with Version 3 or 4.

https connections are supported.

All new users accounts must be approved, as are comments. Please be patient. It is pretty easy to figure out my email address from the scripts, and you are welcome to contact me that way.

Site Search

Categories

Archives

SQL Site

Bad Behavior has blocked 262 access attempts in the last 7 days.