Is your server running slowly? Check with SolarWinds ipMonitor
Get a free evaluation copy of ipMonitor
Contents for Ezine 66 - DSQuery
Let me explain how I came to schedule DSQuery as this week's topic.
Firstly, did you know that each Windows process has a priority? Well, one day last week I was pondering how a process with a lower priority ever got a chance to run on the processor. For
example, if a process with a priority of 24 was running,
how on earth could a program with a priority of 7 ever run? Surely the process with a priority of 24 always bullied the program which only had a priority of 7?
The answer is there is a secondary scheduler which boosts a process priority every time it misses a turn on the processor. So after missing 18 turns the process which started with a priority of 7 is
boosted to 25 and so has its chance to run on the CPU.
Well, this week I feel that DSQuery is like that program with a priority of 7. As it has missed 24 ezines, its priority has been boosted and so it is DSQuery's turn to feature.
Firstly, where does DSQuery come from? Windows 2000 - no, XP - no. The answer is Windows Server 2003. Actually, Windows 2003 has a whole family of DS commands, the other members are: DSAdd, DSMod, DSRm and DSMove.
The DS family remind me of UNIX commands in that they operate at the command line, use powerful verbs, and produce plenty of action. One pre-requisite for getting the most from this DS
family is a
working knowledge of LDAP.
If you are looking for handy network utilities, try some of the free downloads at
Tools4Ever
How many Organizational Units are there in your domain? Let us get a listing of all OUs with this command.
dsquery ou dc=cp,dc=com or dsquery ou domainroot
Learning Points
Note 1: dc means domain context, not domain controller.
Note 2: The dc commands are not case sensitive, but they dislike spaces. dc=cp, dc=com will draw an error.
Note 3: If you haven't got any OUs (Organizational Units), I seriously suggest that you create some to organize your users.
Note 4: Best of all, in this scenario, you can substitute domainroot for dc=cp.
In this example we just want to trawl the users folder and find out who is in that container.
dsquery user cn=users,dc=cp,dc=com
Learning Points
Note 1: The default users' folder is actually a container object called cn=users. My point is if you try ou=users, the command fails.
Note 2: I queried users, however dsquery requires the singular user, not userS. Other objects that you can query are computer (not computers!), group or even contact.
Challenge 1: Substitute OU=xyz for cn=users, where xyz is the name of your OU. Unfortunately, cn=users domainroot does not work.
Challenge 2: Substitute computer for user
Suppose you want to list all of your domain controllers, (not computers). Which command do you think would supply the information?
dsquery server dsquery server domainroot dsquery server dc=cp,dc=com
Learning Points
Note 1: Amazingly, dsquery server The simplest command get the job done.
Note 2: I thank Kevin K for pointing out that this command is the singular server (not the plural servers).
Here is a wonderful command to find the FSMO roles (Flexible Single Master Roles) -hasfsmo. The arguments, which correspond to the 5 roles are: schema, rid, name, infr and pdc.
dsquery servers -hasfsmo schema
Learning Points
Note 1: The command is -hasfsmo not ?hasfsmo as in some documents.
If you are looking for handy network utilities, try some of the free downloads at
Tools4Ever
This example shows two ways to filter your output and so home in on what you are looking for. Let us pretend that we know the user's name but have no idea which OU they are to be found. Moreover,
we are not sure whether their name is spelt Smith, Smithy or Smithye.
dsquery user domainroot -name smith* or dsquery user dc=cp,dc=com -name smith* or plain dsquery user -name smith*
Learning Points
Note 1: Remember to type the singular user.
Note 2: Probably no need to introduce *, you probably realize it's a wildcard.
Note 3: -name is but one of a family of filters. -desc or -disabled are others.
The purpose of -o rdn is to reduce the output to just the relative distinguished name. In a nutshell rdn strips away the OU=, DC= part which you may not be interested in.
dsquery user -name smith* -o rdn
Learning Points
Note 1: o is the letter oh (not a number). In my minds eye o stands for output.
Note 2: There is a switch -o dn, but this is not a switch I use.
Summary - DSQuery
Knowledge is power. The DS family in general and DSQuery in particular are handy commands for interrogating Active Directory from the command line. Perhaps the day will come when you need to
find a user, computer or group without calling for the Active Users and Computers GUI. See more on DS Commands - Whole Section Here
Their topics and material are ideal for getting you started with VBScript. The
videos are easy to follow and you can control the pace. Try their free demo material and then see if you want to buy the full package.
See more about VB Script Training CD.
|