Rob van der Woude's Scripting Pages
Powered by GeSHi

Source code for grmember.bat

(view source code of grmember.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"=="/G" IF /I NOT "%~2"=="/U" IF /I NOT "%~2"=="/V" GOTO Syntax
  11.  
  12. :: Reset variables
  13. SET Descri=
  14. SET Member=
  15. SET Prefix=
  16.  
  17. :: Interpret the optional switch's value
  18. IF /I "%~2"=="/G" (SET ListU=0) ELSE (SET ListU=1)
  19. IF /I "%~2"=="/U" (SET ListG=0) ELSE (SET ListG=1)
  20.  
  21. :: Run a query to check availability of DSQUERY and validity of the specified group
  22. DSQUERY.EXE Group -name "%~1" -limit 2 2>NUL | FIND.EXE "=" >NUL
  23. IF ERRORLEVEL 1 GOTO Syntax
  24.  
  25. :: One command for users and one for nested groups, commands grouped before sorting
  26. (
  27. 	IF "%ListG%"=="1" FOR /F "tokens=1*" %%A IN ('DSQUERY.EXE Group -samid "%~1" -limit 10000 2^>NUL ^| DSGET.EXE Group -members 2^>NUL ^| DSGET.EXE Group -samid -desc    2^>NUL ^| FINDSTR.EXE /R /B /I /V /C:"  desc " ') DO CALL :Display %%B %%A *
  28. 	IF "%ListU%"=="1" FOR /F "tokens=1*" %%A IN ('DSQUERY.EXE Group -samid "%~1" -limit 10000 2^>NUL ^| DSGET.EXE Group -members 2^>NUL ^| DSGET.EXE User  -samid -display 2^>NUL ^| FINDSTR.EXE /R /B /I /V /C:"  samid "') DO CALL :Display %%A %%B
  29. ) | FINDSTR.EXE /R /B /I /V /C:"dsget succeeded" | SORT.EXE
  30.  
  31. :: Done
  32. ENDLOCAL
  33. GOTO:EOF
  34.  
  35.  
  36. :Display
  37. SETLOCAL
  38. :: Append 20 spaces after first 2 argument values
  39. SET Member=%1                    
  40. SET Descri=%2                    
  41. :: Then truncate arguments at 20 characters
  42. SET Member=%Member:~0,20%
  43. SET Descri=%Descri:~0,20%
  44. :: Default prefix is a space
  45. IF "%~3"=="" (SET Prefix= ) ELSE (SET Prefix=%~3)
  46. :: Display result in 2 columns
  47. ECHO  %Prefix%%Member%%Descri%
  48. GOTO:EOF
  49.  
  50.  
  51. :Syntax
  52. ECHO.
  53. ECHO GrMember.bat,  Version 0.53 BETA for Windows 2000 / XP / Server 2003
  54. ECHO List all members of the specified group(s), users as well as nested groups.
  55. ECHO.
  56. :: Pipe symbols cannot be ECHOed in Windows 9x, so in that case
  57. :: we need to use an alternative character (like ASCII 179)
  58. IF     "%OS%"=="Windows_NT" ECHO Usage:  GRMEMBER  group_name  [ /B ^| /G ^| /U ^| /V ]
  59. IF NOT "%OS%"=="Windows_NT" ECHO Usage:  GRMEMBER  group_name  [ /B ³ /G ³ /U ³ /V ]
  60. ECHO.
  61. ECHO Where:  "group_name" is the name of the group to be queried
  62. ECHO         /B           lists both users and groups (default)
  63. ECHO         /G           lists groups only
  64. ECHO         /U           lists users only
  65. ECHO         /V           lists both users and groups, same as /B
  66. ECHO.
  67. ECHO Notes:  Nested groups are marked by an asterisk.
  68. ECHO         This batch file uses DSGET and DSQUERY (native in Windows
  69. ECHO         Server 2003, will work in Windows 2000 and XP if available).
  70. ECHO         Increase DSQUERY's "-limit" value for networks where groups
  71. ECHO         may contain over 10000 members.
  72. ECHO.
  73. ECHO Written by Rob van der Woude
  74. ECHO http://www.robvanderwoude.com
  75.  
  76. :: Discard of local variables
  77. IF "%OS%"=="Windows_NT" ENDLOCAL
  78.  

page last modified: 2024-04-16; loaded in 0.0068 seconds