Translating Active Directory Names Using Perl

You can use the IADsNameTranslate ADSI Utility Interface to translate a user's Active Directory name from one form to another.  To see te Perl version of this script, see Translate Active Directory Names using PowerShell.

Accessing Active Directory objects directly, via the GetObject function, can be a pain since you generally need to know the distinguished name (DN) of the object (e.g. cn=myUser, ou=myOU, dc=myDomain, dc=com). What if you have an OU structure that is eight levels deep? Forget about it! You don't want to be typing DN's in by hand. Who's got time for that?

More likely, you may know the user's logon ID, or their display name, and you want your script to figure out what the DN is. In previous posts, I've shown how to use ADO searches to retrieve information about a user. You certainly can use that technique to search for a user, given their logon ID or display name, and return their DN from the search. However, there's a handy interface in ADSI just for this purpose.

Here's a script that makes use of the IADsNameTranslate utility interface. We input the user's NT4-style logon name (i.e. domain\username) and use the translator to get us other forms of the name, including the DN.

use Win32::OLE;
#Name Translator Initialization Types
#Name Transator Name Types
$CANONICALNAME         = 2;
$NT4NAME               = 3;
$DISPLAYNAME           = 4;
$DOMAINSIMPLE          = 5;
$GUID                  = 7;
$UNKNOWN               = 8;
$CANONICALEX          = 10;
#Create the Translator Object and Initialize (using Global Catalog)
$translator=Win32::OLE->CreateObject ("NameTranslate");
#Let's look for a user using their NT4-style logon ID (domain\user)
#Now let's use the translator to retrieve various forms of their name
print "\nDistinguished Name:\n  ".$translator->Get($DISTINGUISHEDNAME)."\n";
print "\nCanonical Name:\n  ".$translator->Get($CANONICALNAME)."\n";
print "\nNT4 Name:\n  ".$translator->Get($NT4NAME)."\n";
print "\nDisplay Name:\n  ".$translator->Get($DISPLAYNAME)."\n";
print "\nGUID:\n  ".$translator->Get($GUID)."\n";
print "\nUser Principal Name (UPN):\n  ".$translator->Get($USERPRINCIPALNAME)."\n";


Post a Comment

Related Posts Plugin for WordPress, Blogger...