Translate Active Directory Name Formats Using PowerShell

You can translate Active Directory user names from one format to another with PowerShell using the ADSI NameTranslate utility interface. There are many name formats in active directory including distinguishedName, canonicalName, NT4 style and more. ADSI provides a utility interface called IADSNameTranslate. To use it, you create a NameTranslate object, give it a user name in some format, and then request the name in another format. For the Perl version of this script, see Translating Active Directory Names using Perl.

The following script shows the list of initialization types (which tell how the object should connect to Active Directory), and the list of name types that you can use. The script takes one argument, a user name (such as domainname\username), then returns various name formats.

The script also highlights the problem with PowerShell using some COM interfaces where PowerShell doesn't know the type of object you're using. Since PowerShell doesn't know about the properties and methods of a NameTranslate object, you have to use special invokeMethod syntax to call the Init, Get and Set methods of the object.

#Name Translator Initialization Types
#Name Transator Name Types
$CANONICALNAME         = 2
$NT4NAME               = 3
$DISPLAYNAME           = 4
$DOMAINSIMPLE          = 5
$GUID                  = 7
$UNKNOWN               = 8
$CANONICALEX          = 10
if($args.count -ne 1){ "`nUsage: ./nametranslate.ps1 <userName>`n"; Exit; }
$ns=New-Object -ComObject NameTranslate
$dn = [System.__ComObject].InvokeMember(“Get”,”InvokeMethod”,$null,$ns,$DISTINGUISHEDNAME)
$canon = [System.__ComObject].InvokeMember(“Get”,”InvokeMethod”,$null,$ns,$CANONICALNAME)
$display = [System.__ComObject].InvokeMember(“Get”,”InvokeMethod”,$null,$ns,$DISPLAYNAME)
$nt4name = [System.__ComObject].InvokeMember(“Get”,”InvokeMethod”,$null,$ns,$NT4NAME)
"Distinguished Name:`t$dn"
"    Canonical Name:`t$canon"
"      Display Name:`t$display"
"          NT4 Name:`t$nt4name"


Post a Comment

Related Posts Plugin for WordPress, Blogger...