• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 354
  • Last Modified:

modify batch script which outputs dsquery user results

Hey Experts!  It's been awhile since I posted a question on here and would appreciate any assistance you can offer in regards to this question.  Our office is under-going an extensive reorg and users are being moved to different departments.  Because of this, my memory of knowing which users are in which OU is no longer accurate.  I came up with a quick batch script to find the users but I want to clean-up the export to include only a couple pieces of information.

First, the script:
@echo off
cls

set findstring=
set /p "findstring=Enter in user's first name or string: "
DSQUERY USER ou=blah -name *"%findstring%"*

Open in new window


That outputs this at the command prompt:
"CN=x\, y ,OU=1,OU=2,OU=3,OU=4,DC=5,DC=6"

I only want the CN (which is the user name) and the ou that relates to OU=2 as the other information is irrelevant to what I need to gather.  What would I use to output only those two values?

Thanks again for your help!
0
samiam41
Asked:
samiam41
  • 4
  • 2
1 Solution
 
oBdACommented:
Something like this?
@echo off
cls
set findstring=
set /p "findstring=Enter in user's first name or string: "
for /f "tokens=2,6 delims=,=" %%a in ('dsquery.exe USER ou=blah -name *"%findstring%"*') do echo %%a [%%b]

Open in new window

0
 
samiam41Author Commented:
Hey oBdA!  Great to work with you again.

I took your code and replaced "ou=blah" with the full AD context.  I get this error:

Enter in user's first name or string: beth
dsquery failed:'depts' is an unknown parameter.

Depts is the first part of the AD context.

ou=depts,ou=x,dc=y,dc=z
0
 
samiam41Author Commented:
So this is what I'm using.

@echo off
cls
set findstring=
set /p "findstring=Enter in user's first name or string: "
for /f "tokens=2,6 delims=,=" %%a in ('dsquery.exe USER ou=depts,ou=x,dc=y,dc=z, -name *"%findstring%"*') do echo %%a [%%b]

Open in new window

0
Creating Active Directory Users from a Text File

If your organization has a need to mass-create AD user accounts, watch this video to see how its done without the need for scripting or other unnecessary complexities.

 
oBdACommented:
Sorry, too much copy and paste going on. The OU path needs to be enclosed in double quotes:
@echo off
cls
set findstring=
set /p "findstring=Enter in user's first name or string: "
for /f "tokens=2,6 delims=,=" %%a in ('dsquery.exe USER "ou=depts,ou=x,dc=y,dc=z" -name *"%findstring%"*') do echo %%a [%%b]

Open in new window

0
 
samiam41Author Commented:
Amazing work as always!  Thanks for the rapid responses
0
 
samiam41Author Commented:
Oops.  I may have closed it out a bit too quick.  I posted a new question so that you could help (if you have some time) and can earn a few more points in the process.

http://www.experts-exchange.com/OS/Microsoft_Operating_Systems/MS_DOS/Q_28667339.html
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Creating Active Directory Users from a Text File

If your organization has a need to mass-create AD user accounts, watch this video to see how its done without the need for scripting or other unnecessary complexities.

  • 4
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now