Q. How can I convert a user's logon name (sAMAccountName) into their distinguished name (distinguishedName)?

Jerold Schulman

August 17, 2006

1 Min Read
ITPro Today logo in a gray background | ITPro Today

I have scripted NTName2DN.bat to convert a user's logon name (sAMAccountName) into their distinguished name (distinguishedName).

The syntax for using NTName2DN.bat is:

[Call] NTName2DN NTName NetBIOSDomain DN

Where:

NTName        is the user's logon name, like Jerry.NetBIOSDomain is the NetBIOS domain name, like JSIINCDN            is a call directed environment variable that will contain the user's distinguishedName.

Example:

If you wanted to return the current logged on user's distinguished name:

call NTName2DN %UserName% %USERDOMAIN% DN
@echo %DN%

NTName2DN.bat contains:

@echo offif {%3}=={} @echo Syntax: NTName2DN NTName NetBIOSDomain DN&goto :EOFsetlocalset NTName=%2%1set NTName="%NTName:"=%"if exist "%TEMP%NTName2DN.VBS" goto [email protected] objArguments>"%TEMP%NTName2DN.VBS"@echo.Set objArguments = Wscript.Arguments>>"%TEMP%NTName2DN.VBS"@echo.Set objTrans = CreateObject("NameTranslate")>>"%TEMP%NTName2DN.VBS"@echo.objTrans.Init "3", "">>"%TEMP%NTName2DN.VBS"@echo.objTrans.Set "3", objArguments(0)>>"%TEMP%NTName2DN.VBS"@echo.Wscript.Echo objTrans.Get("1")>>"%TEMP%NTName2DN.VBS":getDNset DN="NOTFOUND"for /f "Tokens=*" %%d in ('cscript //nologo "%TEMP%NTName2DN.VBS" %NTName%') do ( set DN="%%d")endlocal&set %3=%DN%


Sign up for the ITPro Today newsletter
Stay on top of the IT universe with commentary, news analysis, how-to's, and tips delivered to your inbox daily.

You May Also Like