Q. How can my batch retrieve the Common Name of the current user?
Jerold Schulman
December 17, 2006
1 Min Read
Every object in Active Directory has a unique Distinguished Name, as in CN=Jerold Schulman,CN=Users,DC=JSIINC,DC=COM, where the first CN= defines the Common Name, Jerold Schulman.
Using standard commands, I have scripted UserCN.bat to retrieve the Common Name of the current user.
The syntax for using UserCN.bat is:
[call] UserCN CN
Where CN is a call directed environment variable that will contain the user's Common Name.
UserCN.bat contains:
@echo offif {%1}=={} @echo Syntax: UserCN CN&goto :EOFif exist "%TEMP%UserCN.vbs" goto [email protected] objSysInfo = CreateObject("ADSystemInfo") >"%TEMP%UserCN.vbs"@echo.strUserDN = objSysInfo.UserName>>"%TEMP%UserCN.vbs"@echo.Set objUser = GetObject("LDAP://" ^& strUserDN)>>"%TEMP%UserCN.vbs"@echo.Wscript.Echo objUser.cn>>"%TEMP%UserCN.vbs":doitset %1=Not Foundfor /f "Tokens=*" %%a in ('cscript //nologo "%TEMP%UserCN.vbs"') do ( set %1=%%a)
About the Author
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