Rob van der Woude's Scripting Pages
Powered by GeSHi

Source code for memberof.bat

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

  1. @ECHO OFF
  2. IF NOT "%OS%"=="Windows_NT" GOTO Syntax
  3. IF     "%~1"=="" GOTO Syntax
  4. IF NOT "%~2"=="" IF /I NOT "%~2"=="/R" GOTO Syntax
  5.  
  6. SETLOCAL ENABLEDELAYEDEXPANSION
  7.  
  8. :: Check both availability of DSQUERY and validity of command line argument
  9. DSQUERY.EXE User -samid %1 2>NUL | FIND.EXE "=" >NUL || GOTO Syntax
  10.  
  11. :: Recursive or not?
  12. IF /I "%~2"=="/R" (SET Expand=-expand) ELSE (SET Expand=)
  13.  
  14. FOR /F "tokens=*" %%A IN ('DSQUERY.EXE User -samid "%~1"') DO (
  15. 	ECHO.
  16. 	FOR /F "skip=1 tokens=*" %%B IN ('ECHO.%%A ^| DSGET.EXE User -samid   ^| FINDSTR.EXE /R /B /C:"  "') DO SET SAMID=%%B
  17. 	FOR /L %%B IN (1,1,16) DO (IF "!SAMID:~-1!"==" " SET SAMID=!SAMID:~0,-1!)
  18. 	FOR /F "skip=1 tokens=*" %%B IN ('ECHO.%%A ^| DSGET.EXE User -display ^| FINDSTR.EXE /R /B /C:"  "') DO SET Display=%%B
  19. 	FOR /L %%B IN (1,1,16) DO (IF "!Display:~-1!"==" " SET Display=!Display:~0,-1!)
  20. 	ECHO !SAMID! ^(!Display!^)
  21. 	ECHO.%%A | DSGET.EXE User -memberof %Expand% | DSGET.EXE Group -samid | MORE +1 | SORT.EXE | FINDSTR.EXE /R /B /C:"  "
  22. )
  23.  
  24. ENDLOCAL
  25. GOTO:EOF
  26.  
  27.  
  28. :Syntax
  29. ECHO.
  30. ECHO MemberOf.bat,  Version 2.01 for Windows 2000 and later
  31. ECHO (Recursively) list all groups of which the specified user is a member.
  32. ECHO.
  33. ECHO Usage:  MEMBEROF  user_id  [/R]
  34. ECHO.
  35. ECHO Where:  "user_id" is/are the (SAM) user account name(s) to be queried
  36. ECHO         /R        lists groups recursively
  37. ECHO.
  38. ECHO Note:   This batch file uses DSGET and DSQUERY (native in Windows
  39. ECHO         Server 2003, will work in Windows 2000 and XP if available).
  40. ECHO.
  41. ECHO Written by Rob van der Woude
  42. ECHO http://www.robvanderwoude.com
  43.  

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