Techmentor: DSQuery and DSMod examples

During the command line scripting session, I demonstrated how to use the directory service command line tools like dsmod, dsquery and dsget. You can get syntax help by running ‘dsquery /?’ (or dsmod,dsadd,dsget). There’s a lot of help information so you’ll probably want to pipe the results using More (dsquery /? | more) You can also get help on each subcommand by using ‘dsmod user /?’

Here’s my list of demos. You can change the distinguished name of the starting path to match a container or domain root in your network. The character after -limit is a zero.

display all users in the domain by DN:
dsquery user -limit 0

display all users in the sales ou by samid
dsquery user ou=sales,dc=matrix,dc=local -limit 0 -o samid

query all users in the sales ou by dn and get their display name and dept
dsquery user ou=sales,dc=matrix,dc=local -limit 0 -o dn|dsget user -display -dept

query all users in the sales ou by dn and set their department
dsquery user ou=sales,dc=matrix,dc=local -limit 0 -o dn|dsmod user -dept “Sales”

Technorati Tags:


CLI 101 – FOR

I’ve long maintained that the FOR command is one of most basic commands every administrator should know. I have a short tutorial you can download at http://www.jdhitsolutions.com/tutorials.htm

Here are some other examples on using the FOR command. Let’s say you have some command line utility that will take a computer name as a parameter, such as ping, and you want to run it against a list of computers. List the computer names in a text file like servers.txt. Then open a command prompt where the file is and run:

FOR /F %x in (servers.txt) do @ping -n 1 %x

If you want to verify you have the basic syntax correct, run this:

FOR /F %x in (servers.txt) do @Echo %x

This should display each server name in the list.

If you want to save the output to a text file for any command you can use console redirection, like this:

FOR /F %x in (servers.txt) do @ping -n 1 %x >>pingresults.txt

Be sure to use >> and not > or you will only get results for the last computer in the text file.

If you want to take the FOR command and put it in a batch file, then remember to use %% instead of %. Here’s a quick batch file version of the ping command example. To avoid errors, I recommend specifying the full path to the text file.

@echo off
::PINGCHECK.BAT
REM Delete pingresults.txt if it already exists so we get a new log
if Exist c:\scripts\pingresults.txt DEL c:\scripts\pingresults.txt
FOR /F %%x in (c:\scripts\servers.txt) do @ping -n 1 %%x >>c:\scripts\pingresults.txt
::END OF SCRIPT

IMPORTANT: The FOR command is only case sensitive when it comes to variables. %x is different than %X. If you run:

FOR /F %x in (servers.txt) do @Echo %X

You will not get the servers but rather %X.

Technorati Tags: