"dsquery computer -name" to print *all* results trimmed

Discussion forum for all Windows batch related topics.

Moderator: DosItHelp

Post Reply
Message
Author
Rob
Posts: 3
Joined: 01 May 2014 13:31

"dsquery computer -name" to print *all* results trimmed

#1 Post by Rob » 01 May 2014 14:11

Hey all. I'm new to Batch scripting and I'm toying around with creating a simple tool to unlock domain accounts, reset passwords, and provide the names of all computers containing the user's name.

I'm working on using the "dsquery" command to list all the computer names within Active Directory that are related to the user's name. We have a standard naming policy of

Code: Select all

[3 character location][Username][Computer number]

For 'John Doe' from a certain location this would look like 'FAIJDOE' or 'FAIJDOE1'.

The "dsquery computer -name" command spits out a lot of information that I'm really not interested in, though. It lists the entire Active Directory OU that the computer is in. I've figured out how to trim off the excess 'fat', so to speak, but I'm still struggling to get the script to print out all computers that show up through "dsquery computer -name [Name]".

Currently, the way I'm trimming the results makes only the last result get printed through an echo command. Here's how I'm doing it:

Code: Select all

:LOOKUP
   ::Look up all computers with FAI+the username given at the beginning
   ::dsquery computer prints information I do not want (such at the OU), so use a For loop to put the output of the command into a variable trimming everything that comes after the first comma
cls
for /f "delims=," %%a in ('dsquery computer -name FAI%User%*' ) do set CompName=%%a1234
   ::Trim off the first and last 4 characters ("CN= and 1234) and then print
set CompName=%CompName:~4,-4%
echo %CompName%


My For loop leaves me with something looking like this (using the John Doe example):
"CN=FAIJDOE

I added the 1234 at the end of %%a to allow me to use "set CompName=%CompName:~4,-4%" to remove the first four ("CN=) and the last four (1234) characters. I was trying to remove just the "CN= at the beginning with a different command, but the = was causing some problems. This was my workaround.

The 'User' variable is read in at the beginning of the file. I added the * at the end of FAI%User% so that dsquery would print all computers (FAIJDOE and FAIJDOE1, instead of just FAIJDOE).

Like I said, my For loop only prints out the final line. If a user has 3 computers in the system (FAIJDOE, FAIJDOE1, and FAIJDOE2) only the last one to show up, FAIJOE2, will be printed. I'm stumped on how to go about fixing this, as I cannot be sure of the number of computers that will come up under any given user's name. Because of this I can't specify an exact number of variables to fill in the For loop.

Any help would be greatly appreciated!!

Rob.

Here is my full code thus far:

Code: Select all

@echo off
:BEGIN
   ::Enter the user's account here
set /p User= Enter user ID:
cls

   ::Get information regarding user's account
net user %User% /domain| FIND /I "Account Active"
net user %User% /domain| FIND /I "Password expires"

   ::If an error was thrown, start over
IF ERRORLEVEL 1 (
   cls
   echo Invalid user.
   GOTO BEGIN )

:MENU1

   ::Display a Menu of things to do
echo.
echo Current Account: %User%
echo.
echo What would you like to do?
echo 1) Unlock account
echo 2) Reset password
echo 3) Enter new account
echo 4) Look up computer name
echo 5) End script

set /p Choice= Enter 1, 2, 3, or 4:

IF "%Choice%"=="1" (
   GOTO UNLOCK )

IF "%Choice%"=="2" (
   GOTO RESETPW )

IF "%Choice%"=="3" (
   cls
   GOTO BEGIN )

IF "%Choice%"=="4" (
   GOTO LOOKUP )

IF "%Choice%"=="4" (
   exit )


:UNLOCK
   ::Simply unlock the account
cls
set /p Unlock= Are you sure you want to unlock the account? (Y/N):
IF "%Unlock%"=="Y" (
   net user %User% /domain /active:YES
   echo User account unlocked )
Pause
GOTO MENU2


:RESETPW
   ::Change the password
cls
set /p Reset= Are you sure you want to reset the password? (Y/N):
IF "%Reset%"=="Y" (
   net user %User% * /domain )
pause
GOTO MENU2


:LOOKUP
   ::Look up all computers with FAI+the username given at the beginning
   ::dsquery computer prints information I do not want (such at the OU), so use a For loop to put the output of the command into a variable trimming everything that comes after the first comma
cls
for /f "delims=," %%a in ('dsquery computer -name FAI%User%*' ) do set CompName=%%a1234
   ::Trim off the first and last 4 characters ("CN= and 1234) and then print
set CompName=%CompName:~4,-4%
echo %CompName%

pause


:MENU2
   ::Second menu
cls
echo What would you like to do?
echo 1) Enter new account
echo 2) View main menu
echo 3) End script
set /p Choice2= Enter 1, 2, or 3:


IF "%Choice2%"=="1" (
cls
GOTO BEGIN)

IF "%Choice2%"=="2" (
cls
GOTO MENU1)

IF "%Choice2%"=="3" (
exit )

Squashman
Expert
Posts: 4488
Joined: 23 Dec 2011 13:59

Re: "dsquery computer -name" to print *all* results trimmed

#2 Post by Squashman » 01 May 2014 14:23

Code: Select all

:LOOKUP
   ::Look up all computers with FAI+the username given at the beginning
   ::dsquery computer prints information I do not want (such at the OU), so use a For loop to put the output of the command into a variable trimming everything that comes after the first comma
cls
for /f "tokens=2 delims==" %%a in ('dsquery computer -name FAI%User%*' ) do echo CompName=%%~a

Rob
Posts: 3
Joined: 01 May 2014 13:31

Re: "dsquery computer -name" to print *all* results trimmed

#3 Post by Rob » 01 May 2014 14:29

Hey, thanks for the incredibly fast response!

This works great, only it spits out
CompName=FAIJDOE,OU
CompName=FAIJDOE1,OU
etc.

Is there an easy way to get rid of the ',OU' at the end?

Rob
Posts: 3
Joined: 01 May 2014 13:31

Re: "dsquery computer -name" to print *all* results trimmed

#4 Post by Rob » 01 May 2014 14:37

Scratch my last reply - I figured it out. I added , to delims

It now looks like this:

Code: Select all

for /f "tokens=2 delims=,=" %%a in ('dsquery computer -name FAI%User%*' ) do echo %%~a


Thanks so much for taking the time to help me out with this! Turns out it was a whole lot easier to do than I had thought.

Squashman
Expert
Posts: 4488
Joined: 23 Dec 2011 13:59

Re: "dsquery computer -name" to print *all* results trimmed

#5 Post by Squashman » 01 May 2014 14:42

Oops. Sorry about that. I was so focused on the equals sign for the delimiter. Forgot to keep the comma in there.

Squashman
Expert
Posts: 4488
Joined: 23 Dec 2011 13:59

Re: "dsquery computer -name" to print *all* results trimmed

#6 Post by Squashman » 01 May 2014 14:48

If you want some really powerful AD Tools check out JoeWare.
http://www.joeware.net/freetools/

Post Reply