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

Returning just the value

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.

0
Simon336697
Asked:
Simon336697
  • 4
  • 3
  • 3
  • +2
3 Solutions
 
Steve KnightIT ConsultancyCommented:
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
0
 
subhashchyCommented:
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

0
 
Bill PrewCommented:
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
0
Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

 
subhashchyCommented:
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.

0
 
Simon336697Author 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
0
 
Bill PrewCommented:
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
0
 
subhashchyCommented:

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.


0
 
Bill PrewCommented:
Okay, I think this would get the job done.  I prefer to place the command that generates the output we want FOR /F to parse right into the ( ) section of the FOR.  It may be ever so slightly more efficient, but not a lot.  In the end I suspect the output of the command executed is still piped to the FOR command, and that is still done with I/O to a temporary file, so not a lot different than the 2 line approach.

I think it can be this simple.  I rerouted STDERR to STDOUT to suppress the adfind header line, and then we just look for the result line we want.

@echo off
for /F "tokens=1* delims=: " %%A in ('adfind -default -f "&(objectcategory=user)(samAccountName=%USERNAME%)" roomNumber 2^>^&1^|find /I "roomNumber"') do set roomNumber=%%B
echo %roomNumber%

Open in new window

~bp
0
 
subhashchyCommented:
make sense..thank you Bill..
0
 
Steve KnightIT ConsultancyCommented:
Looks good.  Basically what I said in post 1 before I went to bed though you changed the goal posts by givibng us the command and different output to search for (room rather than cn).

You should have work solution there by the looks, was trying to decide whether the & would break it and needed escaping but I think it should be OK in the quoted string like that?

Steve
0
 
Simon336697Author Commented:
Bill that works.
0
 
Simon336697Author Commented:
Dragon, Bill, sub...thanks guys.
0
 
mcdonamwIONCommented:
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.
0

Featured Post

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

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