Home All Groups Group Topic Archive Search About

Simple AD shell script - discover usernames



Author
13 Aug 2005 9:58 AM
Martin Jones
Hi,
I'm trying to create a script that takes a txt list of users full names, and
does a dsquery to find their username.
Txt file layout :
smith, john
jones, taffy
....etc

Script:

REM Pass Users Full Name to this batch (format: surname, firstname)
REM First create input text file: C:\Temp\userlist.txt


Rem **** Clear temp variables

set FullName=


Rem **** Extract Name

For /f "tokens=2 delims=[" %%a in ('Findstr /I /C:"[" C:\Temp\userlist.txt')
do (set mess=%%a)
For /f "tokens=1 delims=]" %%b in ("%mess%") do (set FullName=%%b)




Rem **** Check if Output file exists and create if not

IF EXIST C:\temp\output.txt Goto Output

Echo Full Name,Username >C:\temp\output.txt




:Output

Rem **** Run Dsquery

dsquery user -name "%FullName%" | dsget samid >>C:\Temp\output.TXT

--------------------------------------------------------------------------

Script is salvaged from an old IP discovery script I part-wrote a while ago.
Obviously its not working, and I suspect its poor use of tokens/delims.

Any ideas/help very welcome.
Thanks in advance.

Author
13 Aug 2005 1:47 PM
Marty List
Show quote
"Martin Jones" <Martin Jo***@discussions.microsoft.com> wrote in message
news:FFEF9125-0F59-440F-AE4E-C0462629822A@microsoft.com...
> Hi,
> I'm trying to create a script that takes a txt list of users full names,
> and
> does a dsquery to find their username.
> Txt file layout :
> smith, john
> jones, taffy
> ...etc
>
> Script:
>
> REM Pass Users Full Name to this batch (format: surname, firstname)
> REM First create input text file: C:\Temp\userlist.txt
>
>
> Rem **** Clear temp variables
>
> set FullName=
>
>
> Rem **** Extract Name
>
> For /f "tokens=2 delims=[" %%a in ('Findstr /I /C:"["
> C:\Temp\userlist.txt')
> do (set mess=%%a)
> For /f "tokens=1 delims=]" %%b in ("%mess%") do (set FullName=%%b)
>
>
>
>
> Rem **** Check if Output file exists and create if not
>
> IF EXIST C:\temp\output.txt Goto Output
>
> Echo Full Name,Username >C:\temp\output.txt
>
>
>
>
> :Output
>
> Rem **** Run Dsquery
>
> dsquery user -name "%FullName%" | dsget samid >>C:\Temp\output.TXT
>
> --------------------------------------------------------------------------
>
> Script is salvaged from an old IP discovery script I part-wrote a while
> ago.
> Obviously its not working, and I suspect its poor use of tokens/delims.
>
> Any ideas/help very welcome.
> Thanks in advance.


I think you need to add a subroutine in your script, but before we tackle
that - something's not making sense here.  You said the text file looks like
this:
smith, john
jones, taffy

But your script is searching for "[" and "]"

Also, just to clarify, is your script going to get a single user name passed
on the command line, find this in the text file, then lookup the account in
AD?
Author
13 Aug 2005 2:28 PM
Martin Jones
.. Thanks for the reply.

Text file is as stated, can't even spot the "[" and "]"  :/

To simplify , all I have is a massive txt file, with a few hundred entries
with just :
surname, firstname

And I want to run the command:
dsquery user -name "%FullName%" | dsget user -samid

With %FullName% being supplied by each line of the txt file.

I know Im a way off yet, the script Im trying to savage was written by
someone way better than me, he did all the clever pulling stuff from a txt
input file, I worked out the rest.

All I want is an output text file with:
surname, firstname discoveredusername

I know the command works, and I know Im on the right lines pulling the name
from the txt file, but am completely lost by the tokens/delims etc.

Thanks.
Martin.
Author
13 Aug 2005 2:54 PM
Marty List
Show quote
"Martin Jones" <MartinJo***@discussions.microsoft.com> wrote in message
news:F38164DC-1B23-4CA8-AF66-45F54AFE0218@microsoft.com...
>. Thanks for the reply.
>
> Text file is as stated, can't even spot the "[" and "]"  :/
>
> To simplify , all I have is a massive txt file, with a few hundred entries
> with just :
> surname, firstname
>
> And I want to run the command:
> dsquery user -name "%FullName%" | dsget user -samid
>
> With %FullName% being supplied by each line of the txt file.
>
> I know Im a way off yet, the script Im trying to savage was written by
> someone way better than me, he did all the clever pulling stuff from a txt
> input file, I worked out the rest.
>
> All I want is an output text file with:
> surname, firstname discoveredusername
>
> I know the command works, and I know Im on the right lines pulling the
> name
> from the txt file, but am completely lost by the tokens/delims etc.
>
> Thanks.
> Martin.


Sorry, I'm still not understanding what you're try to accomplish.  Let's say
a line in the text file is "smith, john", how is this user stored in AD?
Are you trying to convert this name to some other format like "john smith"?
Or what are you trying to do here:

Rem **** Extract Name
For /f "tokens=2 delims=[" %%a in ('Findstr /I /C:"[" C:\Temp\userlist.txt')
do (set mess=%%a)
For /f "tokens=1 delims=]" %%b in ("%mess%") do (set FullName=%%b)


Also, your first post said "REM Pass Users Full Name to this batch (format:
surname, firstname)" which sounds lke you are trying to just get one name
passed on the command line, but then your second post said "%FullName% being
supplied by each line of the txt file" which sounds like you want to process
all users in the file at once.
Author
13 Aug 2005 3:12 PM
Martin Jones
Hi,
Ok, one of my users passed me a txt list of users:

smith, john
jones, bob
etc ...

Which coincides with our "Name" convention in AD.
To do what the user wants me to do, I need to convert those "surname,
firstname"
into their AD username.

The Command:
dsquery user -name "smith, john" | dsget user -samid

at prompt returns:
samid
SMITHJ
dsget succeeded

now I could type that command for each entry in the txt file (200+), but
obviously want to automate it, so it finds the corresponding username for
each entry.
All I want is the SMITHJ appending to the "smith, john" in the text file so
my txt file looks like:
smith, john SMITHJ
jones, bob JONESB

Then I can pull into Excel and populate our printer address books.

Even simpler : User passed me a txt list of names, which I need to batch
convert to AD usernames.

Hope that clears it up, Thanks for the help so far.
Author
13 Aug 2005 3:44 PM
Dean Wells [MVP]
for /f "tokens=*" %a in (<textfile of usernames>) do dsquery user -name
"%a" | dsget user -samid

If you're intending in placing the command above in a batch file don't
forget to replace the single percent (%a) with double percent (%%a) in
both cases.

--
Dean Wells [MVP / Directory Services]
MSEtechnology
[[ Please respond to the Newsgroup only regarding posts ]]
R e m o v e    t h e    m a s k    t o    s e n d    e m a i l

Martin Jones wrote:
Show quote
> Hi,
> Ok, one of my users passed me a txt list of users:
>
> smith, john
> jones, bob
> etc ...
>
> Which coincides with our "Name" convention in AD.
> To do what the user wants me to do, I need to convert those "surname,
> firstname"
> into their AD username.
>
> The Command:
> dsquery user -name "smith, john" | dsget user -samid
>
> at prompt returns:
> samid
> SMITHJ
> dsget succeeded
>
> now I could type that command for each entry in the txt file (200+),
> but obviously want to automate it, so it finds the corresponding
> username for each entry.
> All I want is the SMITHJ appending to the "smith, john" in the text
> file so my txt file looks like:
> smith, john SMITHJ
> jones, bob JONESB
>
> Then I can pull into Excel and populate our printer address books.
>
> Even simpler : User passed me a txt list of names, which I need to
> batch convert to AD usernames.
>
> Hope that clears it up, Thanks for the help so far.
Author
13 Aug 2005 4:36 PM
Martin Jones
Thanks!

I'd got round to removing the delims (though learnt enough to use them to
pull it out of my output file and back into the original file aside the
names!), but was struggling with the tokens.

Now I all I have to do is figure out why our AD list some users by Full
name, and others by username, even though the accounts are set up exactly the
same.

Thanks for all the help guys.
Martin.
Author
13 Aug 2005 5:01 PM
Marty List
Show quote
"Martin Jones" <MartinJo***@discussions.microsoft.com> wrote in message
news:16C9B809-C106-4989-BC47-BD1AA5271517@microsoft.com...
> Thanks!
>
> I'd got round to removing the delims (though learnt enough to use them to
> pull it out of my output file and back into the original file aside the
> names!), but was struggling with the tokens.
>
> Now I all I have to do is figure out why our AD list some users by Full
> name, and others by username, even though the accounts are set up exactly
> the
> same.
>
> Thanks for all the help guys.
> Martin.


Here's a good explanation of AD names (displayName/fullname, cn, sn,
givenName, etc.).  Dispite the name it's useful even if you're not renaming
accounts.

Issue #45 March 2005
How to rename someone in AD
http://www.minasi.com/archive.htm
Author
22 Aug 2005 8:23 PM
Farai Alleyne
I'm  trying to do something similar - except  the reverse - i'm trying to get
the full name of a user based on their username using dsquery (or any other
AD tool possible).

thanks.

Show quote
"Martin Jones" wrote:

> Hi,
> I'm trying to create a script that takes a txt list of users full names, and
> does a dsquery to find their username.
> Txt file layout :
> smith, john
> jones, taffy
> ...etc
>
> Script:
>
> REM Pass Users Full Name to this batch (format: surname, firstname)
> REM First create input text file: C:\Temp\userlist.txt
>
>
> Rem **** Clear temp variables
>
> set FullName=
>
>
> Rem **** Extract Name
>
> For /f "tokens=2 delims=[" %%a in ('Findstr /I /C:"[" C:\Temp\userlist.txt')
> do (set mess=%%a)
> For /f "tokens=1 delims=]" %%b in ("%mess%") do (set FullName=%%b)
>
>
>
>
> Rem **** Check if Output file exists and create if not
>
> IF EXIST C:\temp\output.txt Goto Output
>
> Echo Full Name,Username >C:\temp\output.txt
>
>
>
>
> :Output
>
> Rem **** Run Dsquery
>
> dsquery user -name "%FullName%" | dsget samid >>C:\Temp\output.TXT
>
> --------------------------------------------------------------------------
>
> Script is salvaged from an old IP discovery script I part-wrote a while ago.
> Obviously its not working, and I suspect its poor use of tokens/delims.
>
> Any ideas/help very welcome.
> Thanks in advance.
Author
23 Aug 2005 10:49 PM
Marty List
"Farai Alleyne" <FaraiAlle***@discussions.microsoft.com> wrote in message
news:EE8F14DF-9B01-4A24-93A9-11393C056BCE@microsoft.com...
> I'm  trying to do something similar - except  the reverse - i'm trying to get
> the full name of a user based on their username using dsquery (or any other
> AD tool possible).
>
> thanks.


Try this:

dsquery.exe user domainroot -samid myusername|dsget.exe user -display -l

AddThis Social Bookmark Button