Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
?
Solved

dsquery - using LIKE in FIND

Posted on 2011-10-19
25
Medium Priority
?
607 Views
Last Modified: 2012-08-13
dsquery computer -limit 0 | find /i "*POS*DEV*"
is not bringing back proper results..

can i use LIKE within FIND?
0
Comment
Question by:25112
  • 9
  • 9
  • 6
  • +1
25 Comments
 
LVL 33

Accepted Solution

by:
knightEknight earned 572 total points
ID: 36993112
use findstr:

dsquery computer -limit 0 | findstr /i " POS | DEV " 
0
 
LVL 43

Assisted Solution

by:Steve Knight
Steve Knight earned 1144 total points
ID: 36993118
No but you can do similar with two find commands, or use FINDSTR instead:

dsquery computer -limit 0 | find /i "POS" | find / "DEV"

That will find all ones with POS in, then look for ones from those that have DEV in.

Steve
0
 
LVL 33

Expert Comment

by:knightEknight
ID: 36993121
note the spaces in the search string - they are necessary.
0
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 33

Expert Comment

by:knightEknight
ID: 36993125
e.g.
  findstr /i " POS | DEV " 
instead of
  findstr /i "POS | DEV"

The former will allow all lines with either POS or DEV thru the filter.
0
 
LVL 43

Expert Comment

by:Steve Knight
ID: 36993132
Sorry KnightEKnight ... cross posted there, though completely failed to post the findstr line anyway, just the double find way, must have hit undo after pasting

Steve
0
 
LVL 39

Assisted Solution

by:Krzysztof Pytko
Krzysztof Pytko earned 284 total points
ID: 36993149
I would resign from using slow syntax like DS Tool + FIND :) DS Tools and other OS commands are limited in usage
It's better to use DSQUERY * -filter ... or Quest PowerShell module for AD :)

... but that's my oppinion :]

Regards,
Krzysztof
0
 
LVL 33

Expert Comment

by:knightEknight
ID: 36993158
no worries ... here's some experimental commands I ran just to check.  It looks like I am wrong about the leading spaces, but the spaces around the pipe symbol are required to do an "or" search.  Run the following commands to see the distinctions:


  dir | findstr  "bytes"

  dir | findstr  "Directory"

  dir | findstr  "bytes | Directory"
0
 
LVL 5

Author Comment

by:25112
ID: 36993772
iSiek, thanks for the reco. at this time, we are using the current syntax, and need to make the small change.. in the new implementations, i will recommend filter option also.
0
 
LVL 5

Author Comment

by:25112
ID: 36993775
knightEknight, i am getting extra results that are not correct finds...with the space.. how would you change it?
0
 
LVL 5

Author Comment

by:25112
ID: 36993778
Steve, your syntax says  "invalid switch"
0
 
LVL 33

Assisted Solution

by:knightEknight
knightEknight earned 572 total points
ID: 36993805
using findstr with pipe, you will likely get results for rows that contain "POS" but not "DEV", which may not be what you want.

Using Steve's suggestion will produce better results in this respect.  (The invalid switch is because of a missing "i" at the end, e.g.  find /i "DEV"  instead of  find / "DEV"  ... but this will also produce extra results where the string "DEV" comes before "POS".
0
 
LVL 33

Expert Comment

by:knightEknight
ID: 36993828
in other words,  | find /i "POS" | find /i "DEV"  will match "xxxDEVxxxPOSxxx"  as well as "xxxPOSxxxDEVxxx"
but this is probably better than using the findstr "or" search.

I don't think find or findstr can filter for exactly what you need. e.g. *POS*DEV*
0
 
LVL 43

Assisted Solution

by:Steve Knight
Steve Knight earned 1144 total points
ID: 36994239
thanks KnighteKnight... Yes missed the i from /i earlier sorry.

So you need to dind entries that always have pos and dev in but may be anywhere in the names is that right, e.g

posxxxxdevxxx
xxposxdevxxx
ssposdevxxxx
etc




0
 
LVL 5

Author Comment

by:25112
ID: 36994610

OK.. works.. another thing the above searches in all fields.. could we restrict only to CN and not OU
0
 
LVL 43

Expert Comment

by:Steve Knight
ID: 36995110
You could pipe the dsquery into dsgetm or I guess just this would work.... not on domain PC at the moment to check:

dsquery computer -limit 0 -name "*POS*DEV*"  -o samid

Steve
0
 
LVL 5

Author Comment

by:25112
ID: 36995162
it comes back empty.. don't you have to use FIND?
0
 
LVL 43

Expert Comment

by:Steve Knight
ID: 36995951
Ok try:

dsquery computer -limit 0 -o samid

for starters... does that give you just the basic name you are after?  That can then push through find etc. if needed.
0
 
LVL 5

Author Comment

by:25112
ID: 36996654
yes, it is getting it.. thanks..

but it is attaching a $ sign at the end of the server name.. why is that.. can i avoid it?
0
 
LVL 43

Assisted Solution

by:Steve Knight
Steve Knight earned 1144 total points
ID: 36996735
Hmm, I suppose it would... it is the account name for the computer.  Try this:

dsquery computer -limit -o samid -name *POS*DEV*

@echo off
(for /f "delims=$" %%a in ('dsquery computer -limit 0 -o samid -name *POS*DEV*') do echo %%~a)> output.txt
notepad output.txt

SHOULD work, but if not:

@echo off
(for /f "delims=$" %%a in ('dsquery computer -limit 0 -o samid ^| find "POS" ^| find "DEV"') do echo %%~a)> output.txt
notepad output.txt

0
 
LVL 5

Author Comment

by:25112
ID: 36997029
thanks Steve.. it is the first one that worked..

I assumed the first one will get the combination only where POS is in front of DEV, right..

the second one would have got it like

posxxxxdevxxx
xxposxdevxxx
ssposdevxxxx
etc
?
which would be more advanced and good  for the purpose..
0
 
LVL 43

Expert Comment

by:Steve Knight
ID: 36997912
yes that is correct.  not sure why it didnt work when we got you to try that earlier, was trying to see if extra quotes or whatever would get in the way but always worked for me... Anyway if it is working good.  have we solved your problem then?

Thanks

Steve
0
 
LVL 5

Author Comment

by:25112
ID: 37000217
thanks.. could you help the following, finally.. would be the perfect list..:

dsquery computer -limit 0 -o samid | find /i "POS" | find /i "DEV"
works fine..

but the following does not..
@echo off
(for /f "delims=$" %%a in
('dsquery computer -limit 0 -o samid | find /i "POS" | find /i "DEV" ')
do echo %%~a)> output1.txt
notepad output1.txt
0
 
LVL 43

Assisted Solution

by:Steve Knight
Steve Knight earned 1144 total points
ID: 37000274
ok you need to escaspe any special chars within the for.  change each | top ^| and it ashould do fine.

Steve
0
 
LVL 5

Author Comment

by:25112
ID: 37001957
look good.. thanks much for your help.
0
 
LVL 43

Expert Comment

by:Steve Knight
ID: 37002371
no problem
Steve
0

Featured Post

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.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Let's recap what we learned from yesterday's Skyport Systems webinar.
High user turnover can cause old/redundant user data to consume valuable space. UserResourceCleanup was developed to address this by automatically deleting user folders when the user account is deleted.
This tutorial will walk an individual through the steps necessary to join and promote the first Windows Server 2012 domain controller into an Active Directory environment running on Windows Server 2008. Determine the location of the FSMO roles by lo…
This video shows how to use Hyena, from SystemTools Software, to bulk import 100 user accounts from an external text file. View in 1080p for best video quality.

564 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