Skip navigation

JSI Tip 9846. How can I return all the user names in my domain, sorted by DN within Container and/or OU?

I have scripted SortADU.bat to return the distinguishedName of every user in the domain, sorted by distinguishedName within container and/or OU.

The syntax for using SortADU.bat is:

for /f "Tokens=*" %%a in ('SortADU') do (
 @echo %%a
)
SortADU.bat contains:
@echo off
setlocal
if exist "%TEMP%\SortADU.vbs" goto start
if exist "%TEMP%\SortADU.tm1" del /q "%TEMP%\SortADU.tm1"
if exist "%TEMP%\SortADU.tm2" del /q "%TEMP%\SortADU.tm2"
@echo.On Error Resume Next>"%TEMP%\SortADU.vbs"
@echo.Dim objConnection, objCommand, objRootDSE, strDNSDomain>>"%TEMP%\SortADU.vbs"
@echo.Dim strFilter, strQuery, objRecordSet>>"%TEMP%\SortADU.vbs"
@echo.dim fso, contents, writefile>>"%TEMP%\SortADU.vbs"
@echo.set fso = CreateObject("Scripting.FileSystemObject") >>"%TEMP%\SortADU.vbs"
@echo.set writefile = fso.CreateTextFile("%TEMP%\SortADU.tm1", 2) >>"%TEMP%\SortADU.vbs"
@echo.Set objConnection = CreateObject("ADODB.Connection")>>"%TEMP%\SortADU.vbs"
@echo.Set objCommand = CreateObject("ADODB.Command")>>"%TEMP%\SortADU.vbs"
@echo.objConnection.Provider = "ADsDSOOBject">>"%TEMP%\SortADU.vbs"
@echo.objConnection.Open "Active Directory Provider">>"%TEMP%\SortADU.vbs"
@echo.Set objCommand.ActiveConnection = objConnection>>"%TEMP%\SortADU.vbs"
@echo.Set objRootDSE = GetObject("LDAP://RootDSE")>>"%TEMP%\SortADU.vbs"
@echo.strDNSDomain = objRootDSE.Get("defaultNamingContext")>>"%TEMP%\SortADU.vbs"
@echo.strBase = "<LDAP://" ^& strDNSDomain ^& ">" >>"%TEMP%\SortADU.vbs"
@echo.strFilter = "(&(objectCategory=person)(objectClass=user))">>"%TEMP%\SortADU.vbs"
@echo.strAttributes = "distinguishedName">>"%TEMP%\SortADU.vbs"
@echo.strQuery = strBase ^& ";" ^& strFilter ^& ";" ^& strAttributes ^& ";subtree">>"%TEMP%\SortADU.vbs"
@echo.objCommand.CommandText = strQuery>>"%TEMP%\SortADU.vbs"
@echo.objCommand.Properties("Page Size") = 99999>>"%TEMP%\SortADU.vbs"
@echo.objCommand.Properties("Timeout") = 300>>"%TEMP%\SortADU.vbs"
@echo.objCommand.Properties("Cache Results") = False>>"%TEMP%\SortADU.vbs"
@echo.Set objRecordSet = objCommand.Execute>>"%TEMP%\SortADU.vbs"
@echo.objRecordSet.MoveFirst>>"%TEMP%\SortADU.vbs"
@echo.Do Until objRecordSet.EOF>>"%TEMP%\SortADU.vbs"
@echo.    strDN = objRecordSet.Fields("distinguishedName")>>"%TEMP%\SortADU.vbs"
@echo.    ouPath = Split(strDN, ",") >>"%TEMP%\SortADU.vbs"
@echo.    ouLen = Len(ouPath(1)) >>"%TEMP%\SortADU.vbs"
@echo.    ouNameLen = ouLen - 3 >>"%TEMP%\SortADU.vbs"
@echo.    ouc = Left (ouPath(1), 3) >>"%TEMP%\SortADU.vbs"
@echo.    oun = Right(ouPath(1), ouNameLen) >>"%TEMP%\SortADU.vbs"
@echo.    contents =  
" ^& ouc ^& oun ^&
^& strDN ^&
">>"%TEMP%\SortADU.vbs" @echo. writefile.writeLine contents>>"%TEMP%\SortADU.vbs" @echo. objRecordSet.MoveNext>>"%TEMP%\SortADU.vbs" @echo.Loop>>"%TEMP%\SortADU.vbs" @echo.objConnection.Close>>"%TEMP%\SortADU.vbs" @echo.writefile.close>>"%TEMP%\SortADU.vbs" @echo.Set objConnection = Nothing>>"%TEMP%\SortADU.vbs" @echo.Set objCommand = Nothing>>"%TEMP%\SortADU.vbs" @echo.Set objRootDSE = Nothing>>"%TEMP%\SortADU.vbs" @echo.Set objRecordSet = Nothing>>"%TEMP%\SortADU.vbs" :start cscript //nologo "%TEMP%\SortADU.vbs" sort "%TEMP%\SortADU.tm1" /O "%TEMP%\SortADU.tm2" del /q %TEMP%\SortADU.tm1" for /f "Tokens=*" %%a in ('type "%TEMP%\SortADU.tm2"') do ( call :out %%a ) del /q %TEMP%\SortADU.tm2" endlocal goto :EOF :out @echo %2



Hide comments

Comments

  • Allowed HTML tags: <em> <strong> <blockquote> <br> <p>

Plain text

  • No HTML tags allowed.
  • Web page addresses and e-mail addresses turn into links automatically.
  • Lines and paragraphs break automatically.
Publish