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

combine three dsquery statements

is it possible to combine the 3 statements into one?
dsquery computer -limit 0 -o samid | find /i "POS" | find /i "DEV"
dsquery computer -limit 0 -o samid | find /i "DEPT" | find /i "DEV"
dsquery computer -limit 0 -o samid | find /i "PROGRAM"
0
25112
Asked:
25112
  • 4
  • 4
1 Solution
 
Krzysztof PytkoActive Directory EngineerCommented:
Yes, it is. Try this syntax

dsquery * -filter "(&(objectcategory=computer)(|(name=*POS*DEV*)(name=*DEPT*DEV*)(name=*PROGRAM*)))" -attr name

Regards,
Krzysztof
0
 
25112Author Commented:
Krzysztof, thanks. i appreciate it.

what i liked with the
dsquery computer -limit 0 -o samid | find /i "POS" | find /i "DEV"
syntax is that with one line, it will find servers who have POS and DEV regardless of which one is first.. but with filter, it will take 6 filters, in this example..

can you confirm if it is perhaps not possible with the "computer -limit 0 -o samid" syntax?
0
 
Krzysztof PytkoActive Directory EngineerCommented:
Yes, you're right! Find looks for pattern in whole line then it is executed once again for other pattern. In my syntax you need 6 statements but previous method is very slow according to that more advanced with dsquery * -filer ... :)

Krzysztof
0
Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

 
25112Author Commented:
hmm.. yes, we will try filter for the production servers where we need to find them quickly..

but with FIND is it possible to put the 3 searches in one shot.. can you just confirm that, please?
0
 
Krzysztof PytkoActive Directory EngineerCommented:
Actually, no. Let's see how FIND works :)

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

in this syntax, you can see pipe character (|) which separates commands. Output from one command is redirected as input to another one. So

1) dsquery computer -limit 0 -o samid
the only role of this command is to get all computers in a domain where it was executed and create list of them. Then one by one is send over pipe to another command. -o samid switch changes its default output. By default DSQUERY returns Distinguished Name of an object. This switch changes it to computer name but with extra character at the end ($)

2) find /i "POS"
find with /i switch searches any pattern with POS in a line. If line contains the pattern then it is redirected over pipe to another command or it's displayed (in this case it's redirected). When pattern is not found, line is dropped and another command doesn't receive it to analyze.

3) find /i "DEV"
This part receives only lines selected by previous find command (there are POS pattern for sure)
find with /i switch searches any pattern with DEV in a line. If line contains the pattern then it is redirected over pipe to another command or it's displayed (in this case it's redirected). When pattern is not found, line is dropped and another command doesn't receive it to analyze.

so, this syntax is looking only for *POS*DEV* When computer name is *DEV*POS* it's not found because the first find filtered it out.

To summarize: FIND looks only for specified pattern in a line. If it's not found line is dropped and is not analyzed anymore. If pattern was found, then another FIDN syntax checks it using its own filter aso.

Hope it helps a little

Krzysztof
0
 
25112Author Commented:
thanks for the detailed explanation..

with filter option, i can list as many to fitler by:
name=*POS*DEV*
name=*DEPT*DEV*
name=*DEV*DEPT*
name=*PROGRAM*
etc all in one line..

In find, I can only use i string search, right?
find /i "POS" | find /i "DEV" is equivalent to name=*POS*DEV* and also name=*DEV*POS*

but can i have 3 find from one "dsquery computer -limit 0 -o samid" command, and then output all results in one shot.. like how you showed you can do with filter in #37002125.. can the same be done just with FINDs and all in one statement?
0
 
Krzysztof PytkoActive Directory EngineerCommented:
unfortunately, you can't do that in one shot using FIND. This works a little bit different. Pipe | separates commands and they use results of previous one.

Thta means if DSQUERY gives a computer name to the first FIND command, it looks for the first pattern. If it's found then the same line (as from DSQUERY) is used by the second one. But if the first pattern wasn't found in a string then it returns NULL value. Then the second FIND command has nothing to analyze. The input is empty, even if in that string was the second pattern. The first didn't exist and there is nothing to check by next command.

So, that's why you cannot use FIND in one syntax line to get all required filters together

If you wish more detailed answer with some exaples, please let me know, I will write short "live" example for you, tomorrow

Krzysztof
0
 
25112Author Commented:
your explanation was very superbe..
thanks a lot Krzysztof
0

Featured Post

Upgrade your Question Security!

Add Premium security features to your question to ensure its privacy or anonymity. Learn more about your ability to control Question Security today.

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