Solved

list users in groups in OU

Posted on 2015-01-22
10
123 Views
Last Modified: 2015-02-05
Hey Experts.

I would like to show the users in the various groups within a particular OU.

Department (OU)
>Computers (OU)
>Groups (OU)
>>PRT-1 (Group)
>>PRT-2 (Group)
>>PRT-3 (Group)

I want to have the users that are in the groups nested in the "Department OU > Groups OU" output to a log file.  I need to know the users listed in the particular group:

PRT-1
-userA
-userB

PRT-2
-userA
-userC
-userD

Thanks experts!!
0
Comment
Question by:samiam41
  • 5
  • 4
10 Comments
 
LVL 23

Expert Comment

by:NVIT
Comment Utility
Note: Change YourDomainName to your actual domain name.

From the DC, try:
dsquery user "CN=PRT-1,OU=Groups,OU=Computers,DC=YourDomainName,DC=com"

If that works, try this at CMD prompt:
for /f "tokens=1-2 delims==," %a in ('dsquery user "CN=PRT-1,OU=Groups,OU=Computers,DC=YourDomainName,DC=com"') do (echo %~b)
0
 
LVL 13

Expert Comment

by:Gabriel Clifton
Comment Utility
Actually, change that to dsget group "CN=GroupName,DC=domain,DC=name,DC=com" -members
0
 
LVL 13

Expert Comment

by:Gabriel Clifton
Comment Utility
This should get you where you want to be.

@echo off
cls

for /f "tokens=*" %%a in ('dsquery group "OU=Groups,DC=Domain,DC=com" ^|sort') do (call :oumembers %%a)

exit /b

:oumembers
set ougroup=%1
echo %ougroup% >>ou-group-members.txt
::pause
for /f "tokens=1-2 delims==," %%a in ('dsget group %ougroup% -members ^|sort') do (echo %%~b >>ou-group-members.txt)
goto :eof

Open in new window

0
 
LVL 13

Expert Comment

by:Gabriel Clifton
Comment Utility
This will be a little nicer output file.

@echo off
cls

for /f "tokens=*" %%a in ('dsquery group "OU=Groups,DC=Domain,DC=com" ^|sort') do (call :oumembers %%a)

exit /b

:oumembers
set ougroup=%1
echo %ougroup% >>ou-group-members.csv
::pause
for /f "tokens=1-2 delims==," %%a in ('dsget group %ougroup% -members ^|sort') do (echo ,%%~b >>ou-group-members.csv)
goto :eof

Open in new window

0
 
LVL 9

Author Comment

by:samiam41
Comment Utility
Great script!

Here is what I used:

@echo off
cls

for /f "tokens=*" %%a in ('dsquery group "OU=Groups,DC=Domain,DC=com" ^|sort') do (call :oumembers %%a)

exit /b

:oumembers
set ougroup=%1
echo %ougroup% >>ou-group-members.csv
::pause
for /f "tokens=1-2 delims==," %%a in ('dsget group %ougroup% -members ^|sort') do (echo ,%%~b >>ou-group-members.csv)
goto :eof

Open in new window


The log file shows the username as:  lastname\

Not the user name.  How can I (we/you) fix that?
0
Why You Should Analyze Threat Actor TTPs

After years of analyzing threat actor behavior, it’s become clear that at any given time there are specific tactics, techniques, and procedures (TTPs) that are particularly prevalent. By analyzing and understanding these TTPs, you can dramatically enhance your security program.

 
LVL 13

Accepted Solution

by:
Gabriel Clifton earned 500 total points
Comment Utility
There may be a better way, but I do know this works.

@echo off
cls

for /f "tokens=*" %%a in ('dsquery group "OU=Groups,DC=domain,DC=com" ^|sort') do (call :oumembers %%a)

exit /b

:oumembers
set ougroup=%1
echo %ougroup% >>ou-group-members.csv
::pause
for /f "tokens=*" %%b in ('dsget group %ougroup% -members ^|sort') do (call :usersamid %%b)
goto :eof

:usersamid
set userid=%1
for /f "tokens=*" %%c in ('dsquery user %userid% -o samid') do echo ,%%c >>ou-group-members.csv
goto :eof

Open in new window

0
 
LVL 9

Author Closing Comment

by:samiam41
Comment Utility
Brilliant work!!  Thanks for your help with this!
0
 
LVL 13

Expert Comment

by:Gabriel Clifton
Comment Utility
No problem, any time.
0
 
LVL 9

Author Comment

by:samiam41
Comment Utility
Check this question out if you have some time and want some points!

http://www.experts-exchange.com/OS/Microsoft_Operating_Systems/MS_DOS/Q_28602773.html
0
 
LVL 9

Author Comment

by:samiam41
Comment Utility
Hey everyone!  I have a question that received a couple of replies but the suggestions are for powershell and I asked for batch.  If any of you have a minute or two and would like some points, I would appreciate your help.

http://www.experts-exchange.com/OS/Microsoft_Operating_Systems/MS_DOS/Q_28610287.html
0

Featured Post

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

Suggested Solutions

I have published numerous articles here at Experts Exchange that present programs/scripts written in a language called AutoHotkey. Each of those articles has a brief paragraph describing where to download the product and how to install it. I have al…
OfficeMate Freezes on login or does not load after login credentials are input.
In this video, we discuss why the need for additional vertical screen space has become more important in recent years, namely, due to the transition in the marketplace of 4x3 computer screens to 16x9 and 16x10 screens (so-called widescreen format). …
The viewer will learn how to successfully create a multiboot device using the SARDU utility on Windows 7. Start the SARDU utility: Change the image directory to wherever you store your ISOs, this will prevent you from having 2 copies of an ISO wit…

763 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

11 Experts available now in Live!

Get 1:1 Help Now