JSI Tip 8239. How do I enumerate local and/or domain groups in a script?

I have scripted Groups.bat to return the local and/or domain groups to your script.

The syntax for using Groups.bat is any of the following:

Groups \[L|D|B\]>FileName.txt    - To write them to a file in a format like:
                                   D Domain Admins
                                   D Domain Users
                                   L Administrators
                                   L Backup Operators
                                   L Users


for /f "Tokens=1*" %%a in ('Groups \[L|D|B\]') do (
 set type=%%a
 set grp=%%b
 call :process
)
...
...
goto :EOF
:process
...
...
...
Where parameter L returns only local groups, D returns only domain groups, and B returns both local and domain groups.

Groups.bat contains:

@echo off
if /i \{%1\}

\{L\} goto OK if /i \{%1\}

\{D\} goto OK if /i \{%1\}==\{B\} goto OK @echo Syntax: Groups \[L/D/B\]&goto :EOF :OK setlocal set type=%1 if /i "%type%" EQU "D" call :getd if /i "%type%" EQU "B" call :getd if /i "%type%" EQU "B" call :getl if /i "%type%" EQU "L" call :getl endlocal goto :EOF :getd for /f "Tokens=* Delims=*" %%g in ('net group /domain ^|findstr /l /c:"*"') do ( call :fix D "%%g" ) goto :EOF :getl for /f "Tokens=* Delims=*" %%g in ('net localgroup ^|findstr /l /c:"*"') do ( call :fix L "%%g" ) goto :EOF :fix set ld=%1 set grp=%2 set grp=%grp:"=% for /f "Tokens=1-3 Delims=*" %%a in ('@echo %grp%') do ( call :fix1 "%%a" "%%b" "%%c% ) goto :EOF :fix1 set grp=%1 set grp=%grp: "=% set grp=%grp:"=% if "%grp%" NEQ "" @echo %ld% %grp% set grp=%2 set grp=%grp: "=% set grp=%grp:"=% if "%grp%" NEQ "" @echo %ld% %grp% set grp=%3 set grp=%grp: "=% set grp=%grp:"=% if "%grp%" NEQ "" @echo %ld% %grp%



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