JSI Tip 7340. How can I create a telephone directory from my Active Directory domain?

Jerold Schulman

October 19, 2003

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

Using the Active Directory command-line tools, in a Windows 2000 domain, or Windows Server 2003 domain, I have scripted TelDir.bat to produce a telephone directory.

The syntax for using TelDir.bat is:

teldir ReportFile Lines [/x:exlusions] [/n:nn] [/o:nn] [/t:nn] [/i:nn] [/e:nn]

where:

ReportFile      is the file name of the telephone directory report.Lines           is the number of lines to print per page.                 It must be less than the number that can be printed before a Print ReportFile command auto-paginates.                If you print the report landscaped, the number should reflect this./x:exlusions    is a list of fields to exclude, using the following table:
Exclude  Field            ExplanationB        N/A              Only the first heading line is printed.                        E        E-mail Address                        F        Fax Number                        H        Home Phone                        I        IP Phone                        M        Mobile Phone                         O        Office Name                        P        Pager Number
/n:nn           will set the number of characters in the Name column. The default is 33. The minimum is 15./o:nn           will set the number of characters in the Office column. The default is 12. The minimum is 2./t:nn           will set the number of characters in all telephone number columns. The default is 15. The minimum is 7./i:nn           will set the number of characters in the IpTel column. The default is 24. The minimum is 7./e:nn           will set the number of characters in the Email column. The default is 24. The minimum is 10.

Sample Usage:

TelDir "%UserProfile%Documents and SettingsTelDir.txt" 60  Will generate the complete report.TelDir teldir.txt 58 /x:b                                    Will generate the complete report. Only the first record has column headings.TelDir c:folderreport.txt 58 /x:IHF                        The report will exclude the IpTel, Home, and Fax columns.TelDir c:folderreport.txt 58 /x:I /N:30 /O:10 /E:17 /T:14  The report will exclude the IpTel column.                                                             The Name column will contain 30 characters,                                                             the Office column will contain 10 characters,                                                             the Email column will contain 17 characters,                                                             and all the telephone number columns will contain 14 characters.

Here is a portion of a complete report, generated with no exclusions or column sizing changes:

............ S ............Name                              Office       Phone           Mobile          Pager           Fax             Email                    IpTel                    Home           Schulman, Jennifer V.             Corporate    1 770 475 XXXX  1 XXX XXX XXXX  1 XXX XXX XXXX  1 XXX XXX XXXX  [email protected]                               1 XXX XXX XXXXSchulman, Jerold                  Corporate    1 770 475 3820  1 678 362 4762  1 XXX XXX XXXX  1 770 442 3820  [email protected]                                  1 XXX XXX XXXX

NOTE: You must download ff.com and place it in the %SystemRoot% folder. This file contains a form feed.

TelDir.bat contains:

@echo offif not exist "%systemroot%ff.com" @echo "%systemroot%ff.com" is missing.&goto :EOFsetlocalif {%2}
{} goto syntaxif exist "%TEMP%teldir.tm1" del /q "%TEMP%teldir.tm1"set report=%1if exist %report% del /q %report%set /a lp=1000%2%%1000set /a lc=0shiftshiftset /a nl=33set /a ol=12set /a tl=15set /a il=24set /a el=24set exclude=#:paramif {%1}
{} goto beginset p=%1if /i "%p:~0,3%" EQU "/x:" goto ex if /i "%p:~0,3%" EQU "/o:" goto offif /i "%p:~0,3%" EQU "/t:" goto telif /i "%p:~0,3%" EQU "/i:" goto ipif /i "%p:~0,3%" EQU "/e:" goto emif /i "%p:~0,3%" EQU "/n:" goto nmgoto syntax:nmset wrk=%p:~3%set /a nl=100%wrk%%%100if %nl% LSS 15 goto syntaxgoto paramend:emset wrk=%p:~3%set /a el=100%wrk%%%100if %el% LSS 10 goto syntaxgoto paramend:ipset wrk=%p:~3%set /a il=100%wrk%%%100if %il% LSS 7 goto syntaxgoto paramend:telset wrk=%p:~3%set /a tl=100%wrk%%%100if %tl% LSS 7 goto syntaxgoto paramend:offset wrk=%p:~3%set /a ol=100%wrk%%%100if %ol% LSS 2 goto syntaxgoto paramend:exset exclude=%p:~3%#:paramendshiftgoto param:syntax@echo Syntax: TelDir ReportFile Lines [/x:exlusions] [/n:nn] [/O:nn] [/t:nn] [/i:nn] [/e:nn]endlocalgoto :EOF:beginset EB=Nset EE=Nset EF=Nset EH=Nset EI=Nset EM=Nset EO=Nset EP=Nset wrk=%exclude:B=%if "%wrk%" NEQ "%exclude%" set EB=Yset wrk=%exclude:E=%if "%wrk%" NEQ "%exclude%" set EE=Yset wrk=%exclude:F=%if "%wrk%" NEQ "%exclude%" set EF=Yset wrk=%exclude:H=%if "%wrk%" NEQ "%exclude%" set EH=Yset wrk=%exclude:I=%if "%wrk%" NEQ "%exclude%" set EI=Yset wrk=%exclude:M=%if "%wrk%" NEQ "%exclude%" set EM=Yset wrk=%exclude:O=%if "%wrk%" NEQ "%exclude%" set EO=Yset wrk=%exclude:P=%if "%wrk%" NEQ "%exclude%" set EP=Yset wrk=Name                                             #call set title=%%wrk:~0,%nl%%%set wrk=Office                      #if "%EO%" NEQ "Y" call set title=%title% %%wrk:~0,%ol%%%set wrk=Phone                       #call set title=%title% %%wrk:~0,%tl%%%set wrk=Mobile                      #if "%EM%" NEQ "Y" call set title=%title% %%wrk:~0,%tl%%%set wrk=Pager                       #if "%EP%" NEQ "Y" call set title=%title% %%wrk:~0,%tl%%%set wrk=Fax                         #if "%EF%" NEQ "Y" call set title=%title% %%wrk:~0,%tl%%%set wrk=Email                                #if "%EE%" NEQ "Y" call set title=%title% %%wrk:~0,%el%%%set wrk=IpTel                                #if "%EI%" NEQ "Y" call set title=%title% %%wrk:~0,%il%%%set wrk=Home                        #if "%EH%" NEQ "Y" call set title=%title% %%wrk:~0,%tl%%%for /f "Tokens=*" %%u in ('dsquery user domainroot -name * -limit 0') do ( for /f "Tokens=*" %%t in ('dsget user %%u  -fn -mi -ln -tel -email -hometel -pager -mobile -fax -iptel -office -c -l') do set line=%%t&call :parse )sort "%TEMP%teldir.tm1" /O "%TEMP%teldir.tm2"if "%EB%" EQU "Y" @echo %title%>%report%&set /a lp=999999set prev=for /f "Tokens=*" %%t in ('type "%TEMP%teldir.tm2"') do set line=%%t&call :reportitdel /q "%TEMP%teldir.tm1"del /q "%TEMP%teldir.tm2"endlocalgoto :EOF:reportitif "%EB%" EQU "Y" goto rpt1if not defined prev goto newif %lc% EQU %lp% goto newif /i "%prev%" EQU "%line:~0,1%" goto rpt1:newif exist %report% call :copys>nul 2>&1set /a lc=4set prev=%line:~0,1%@echo ............ %prev% ............>>%report%@echo.>>%report%@echo %title%>>%report%@echo.>>%report%:rpt1@echo %line%>>%report%set /a lc=%lc% + 1goto :EOF:parseset line=%line:)= %set line=%line:(= %if /i "%line%" EQU "dsget succeeded" goto :outfor /f "Tokens=1* Delims=: " %%p in ('@echo %line%') do set ti=%%p&set data=%%qif /i "%ti%" EQU "fn" set fn=%data%&goto :EOFif /i "%ti%" EQU "mi" set mi=%data%&goto :EOFif /i "%ti%" EQU "ln" set ln=%data%&goto :EOFif /i "%ti%" EQU "office" set office=%data%&goto :EOFif /i "%ti%" EQU "tel" set tel=%data%&goto :EOFif /i "%ti%" EQU "email" set email=%data%&goto :EOFif /i "%ti%" EQU "hometel" set hometel=%data%&goto :EOFif /i "%ti%" EQU "pager" set pager=%data%&goto :EOFif /i "%ti%" EQU "mobile" set mobile=%data%&goto :EOFif /i "%ti%" EQU "fax" set fax=%data%&goto :EOFif /i "%ti%" EQU "iptel" set iptel=%data%&goto :EOFgoto :EOF:copyscopy %report%+"%systemroot%ff.com" %report%goto :EOF:outif "%ln%" EQU "" goto :EOFset name=%ln%, %fn% %mi%                                         #call set text=%%name:~0,%nl%%%set office=%office%                      #if "%EO%" NEQ "Y" call set text=%text% %%office:~0,%ol%%%set tel=%tel%                            #call set text=%text% %%tel:~0,%tl%%%set mobile=%mobile%                           #if "%EM%" NEQ "Y" call set text=%text% %%mobile:~0,%tl%%%set pager=%pager%                             #if "%EP%" NEQ "Y" call set text=%text% %%pager:~0,%tl%%%set fax=%fax%                                 #if "%EF%" NEQ "Y" call set text=%text% %%fax:~0,%tl%%%set email=%email%                                     #if "%EE%" NEQ "Y" call set text=%text% %%email:~0,%el%%%set iptel=%iptel%                                     #if "%EI%" NEQ "Y" call set text=%text% %%iptel:~0,%il%%%set hometel=%hometel%                           #if "%EH%" NEQ "Y" call set text=%text% %%hometel:~0,%tl%%%@echo %text%>>"%TEMP%teldir.tm1"



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