Rob van der Woude's Scripting Pages
Powered by GeSHi

Source code for oumember.bat

(view source code of oumember.bat as plain text)

  1. @ECHO OFF
  2. :: Use local variables
  3. IF "%OS%"=="Windows_NT" SETLOCAL
  4.  
  5. :: Check Windows version
  6. IF NOT "%OS%"=="Windows_NT" GOTO Syntax
  7.  
  8. :: Check number of command line arguments and validity of optional switch
  9. IF "%~1"=="" GOTO Syntax
  10. IF NOT "%~2"=="" IF /I NOT "%~2"=="/B" IF /I NOT "%~2"=="/C" IF /I NOT "%~2"=="/U" IF /I NOT "%~2"=="/V" GOTO Syntax
  11.  
  12. :: Interpret the optional switch's value
  13. IF /I "%~2"=="/C" (SET ListU=0) ELSE (SET ListU=1)
  14. IF /I "%~2"=="/U" (SET ListC=0) ELSE (SET ListC=1)
  15.  
  16. :: Run a query to check availability of DSQUERY and validity of the specified OU
  17. DSQUERY.EXE OU -name "%~1" -limit 2 2>NUL | FIND.EXE "=" >NUL
  18. IF ERRORLEVEL 1 GOTO Syntax
  19.  
  20. :: The actual commands
  21. FOR /F "tokens=*" %%A IN ('DSQUERY.EXE OU -name "%~1"') DO (
  22. 	IF /I "%~2"=="/V" ECHO.%%~A
  23. 	IF "%ListU%"=="1" DSQUERY.EXE User     "%%~A" -name * -limit 10000 2>NUL | DSGET.EXE User     -samid -display 2>NUL | FIND.EXE "  " | FINDSTR.EXE /R /B /V /I /C:"  samid " | SORT.EXE
  24. 	IF "%ListC%"=="1" DSQUERY.EXE Computer "%%~A" -name * -limit 10000 2>NUL | DSGET.EXE Computer -samid -desc    2>NUL | FIND.EXE "  " | FINDSTR.EXE /R /B /V /I /C:"  desc "  | SORT.EXE
  25. )
  26.  
  27. :: Done
  28. ENDLOCAL
  29. GOTO:EOF
  30.  
  31.  
  32. :Syntax
  33. ECHO.
  34. ECHO OUMember.bat,  Version 1.03 for Windows 2000 / XP / Server 2003
  35. ECHO List all members, users and/or computers, of the specified OU(s).
  36. ECHO.
  37. :: Pipe symbols cannot be ECHOed in Windows 9x, so in that
  38. :: case we need to use an alternative character (ASCII 179)
  39. IF     "%OS%"=="Windows_NT" ECHO Usage:  OUMEMBER  ou_name  [ /B ^| /C ^| /U ^| /V ]
  40. IF NOT "%OS%"=="Windows_NT" ECHO Usage:  OUMEMBER  ou_name  [ /B  /C  /U  /V ]
  41. ECHO.
  42. ECHO Where:  "ou_name" is the name of the OU to be queried
  43. ECHO         /B        lists both users and computers (default)
  44. ECHO         /C        lists computers only
  45. ECHO         /U        lists users only
  46. ECHO         /V        like /B, and also lists DNs of matching OUs
  47. ECHO.
  48. ECHO Notes:  This batch file uses DSGET and DSQUERY (native in Windows
  49. ECHO         Server 2003, will work in Windows 2000 and XP if available).
  50. ECHO         Increase DSQUERY's "-limit" value for networks where OUs
  51. ECHO         may contain over 10000 members.
  52. ECHO.
  53. ECHO Written by Rob van der Woude
  54. ECHO http://www.robvanderwoude.com
  55.  
  56. :: Discard of local variables
  57. IF "%OS%"=="Windows_NT" ENDLOCAL
  58.  

page last uploaded: 2017-08-21, 14:26