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 )