Using information from tip 7714 and tip 7717,
I have scripted DN2Acct.bat to return a user's sAMAccountName, or distinguishedName, by providing their
displayName.
The syntax for using DN2Acct.bat is:
DN2Acct "Display Name" \[/D\]
Where "Display Name" is the displayName value, like "Jerold Schulman", and /D is an optional parameter that outputs the user's distinguishedName instead of the default sAMAccountName.
The output is displayed on the console, but you can return it in a variable using:
for /f "Tokens=*" %%u in ('DN2Acct "Display Name"') do ( set variable=%%u )
Examples:
To return the sAMAccountName of Jerold Schulman: DN2Acct "Jerold Schulman" Output: Jerry To return the distinguishedName of Jennifer Schulman: DN2Acct "Jennifer Schulman" /D Output: CN=Jennifer Schulman,CN=Users,DC=JSIINC,DC=COM To return the distinguishedName of all users whose last name is Schulman: dn2acct *Schulman /d Output: CN=Jerold Schulman,CN=Users,DC=JSIINC,DC=COM CN=Jennifer Schulman,CN=Users,DC=JSIINC,DC=COM To return the sAMAccountName of all users whose given name starts with a Je: dn2acct Je* Output: Jerry JenniferDN2Acct.bat contains:
@echo off if \{%1\}==\{\} @echo Syntax DN2Acct "Display Name" \[/D\]&goto :EOF setlocal set fn=%1 set fn=%fn:"=% set attr=sAMAccountName if /i "%2" EQU "/d" set attr=distinguishedName set qry=dsquery * domainroot -filter "(&(objectCategory=Person)(objectClass=User)(displayName=%fn%))" -attr %attr% -limit 0 for /f "Skip=1 Tokens=*" %%u in ('%qry%') do ( @echo %%u ) endlocal
0 comments
Hide comments