We help IT Professionals succeed at work.

Check out our new AWS podcast with Certified Expert, Phil Phillips! Listen to "How to Execute a Seamless AWS Migration" on EE or on your favorite podcast platform. Listen Now

x

Returning just the value

Medium Priority
713 Views
Last Modified: 2014-05-16
Hi guys hope you are all well and can assist.
I'm using adfind to return an AD attribute.
The problem is, is that it is returning more than I want, for example:

C:\>adfind <command>

Blah blah joeware.net etc etc
[20] cn:auyxx56


What I want to return is the following:
C:\>adfind <command>
auyxx56

That is,

Auyxx56


Any help greatly appreciated.

Comment
Watch Question

Steve KnightIT Consultancy
CERTIFIED EXPERT

Commented:
Something like this batch file should pull out the right bit for you.. this searches for a line in the output of the command with cn: in iit then takes the text to the right of the :

@echo off
REM Runs your command from the command line, i.e. call this
REM findad.cmd and run it with findad yourcommand
echo Running adfind with command %*
for /f "tokens=1* delims=:" %%a in ('adfind %* ^| find "cn:"') do set answer=%%b
echo The answer you wanted was %answer%.
echo You can do away with rest of that line and the other echo of course.
pause
may be if you want a one liner then..

for /f "tokens=1* delims=:" %a in ('adfind <command> ^| find "cn:"') do echo %b

Open in new window

Bill PrewTest your restores, not your backups...
CERTIFIED EXPERT
Expert of the Year 2019
Top Expert 2016

Commented:
Hmm, isn't that what Steve said?  But he added some explanatory comments around the command in the batch file.

I will mention that your version needs to be run at a command line, not in a batch script, since the FOR variables have the form %X rather than %%X.

~bp
Agreed, its same as Steve said above. Infact i copied from his script itself ..
I though the OP was looking for a command instead of a batch.
Steve already explained the script,so might a one line command would be preferable for him in AD environment.

Author

Commented:
Hi guys thanks so much for your responses.

Here is the command Im running:

D:\testss>adfind -default -f "&(objectcategory=user)(samAccountName=%USERNAME%)" roomNumber | find /N "room"

AdFind V01.45.00cpp Joe Richards (joe@joeware.net) March 2011

[6]>roomNumber: AUYHOA06


What I want to return is:

D:\testss>adfind -default -f "&(objectcategory=user)(samAccountName=%USERNAME%)" roomNumber | find /N "room"
AUYHOA06


Just the  AUYHOA06
Bill PrewTest your restores, not your backups...
CERTIFIED EXPERT
Expert of the Year 2019
Top Expert 2016

Commented:
Okay, seems like you're in good hands with Steve and subhashchy who have provided ways to get the result you want so I'll leave you to their suggestions.

~bp

try this...not tested though..

@echo off
adfind -default -f "&(objectcategory=user)(samAccountName=%USERNAME%)" roomNumber | find /N "room" | find /v /i "AdFind " | find /i "CN:" >%temp%\adfindresult.txt
for /f "tokens=1* delims=:" %%a in (%temp%\adfindresult.txt) do set answer=%%b
echo %answer%.
pause

Open in new window


@BP and Steve , I want to know will it be easier to put "adfind -default -f "&(objectcategory=user)(samAccountName=%USERNAME%)" roomNumber | find /N "room"  (or any such long commands with special characters ) in the for loop in ( ) field or take the output in text file and then parse the text file in for loop ?  
Also,which approach will take more system resources.


I would be more than happy if you could explain a bit on this.
ThanK you.


Test your restores, not your backups...
CERTIFIED EXPERT
Expert of the Year 2019
Top Expert 2016
Commented:
Unlock this solution and get a sample of our free trial.
(No credit card required)
UNLOCK SOLUTION
Unlock this solution and get a sample of our free trial.
(No credit card required)
UNLOCK SOLUTION
Steve KnightIT Consultancy
CERTIFIED EXPERT
Commented:
Unlock this solution and get a sample of our free trial.
(No credit card required)
UNLOCK SOLUTION

Author

Commented:
Bill that works.

Author

Commented:
Dragon, Bill, sub...thanks guys.
I just happened upon this while looking for something else, but wanted to add input as the above solutions are entirely way more complicated than they need be.  The ADFind tool has the native ability to strip out all that you don't want via the '-nodn' and '-list' parameters.  Perhaps these were not available at time of this post.. but for anyone else.

adfind -default -f "&(objectcategory=user)(samAccountName=%USERNAME%)" roomNumber -nodn -list

Open in new window


Edit:  I am just posting this for benefit of anyone else that runs across this thread.
Unlock the solution to this question.
Thanks for using Experts Exchange.

Please provide your email to receive a sample view!

*This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.

OR

Please enter a first name

Please enter a last name

8+ characters (letters, numbers, and a symbol)

By clicking, you agree to the Terms of Use and Privacy Policy.