query Active directory display name

Discussion forum for all Windows batch related topics.

Moderator: DosItHelp

Message
Author
confuseis
Posts: 11
Joined: 11 Dec 2011 09:59

query Active directory display name

#1 Post by confuseis » 15 Feb 2014 19:58

Hi

Im looking to create a logon script that will query the output the user and the computer they are using.

The %username% is not enough as I cant tell from this who the user is without having to look it up.

I need the friendly name, Displayname of the user so I can output it to the 3rd colum of my csv file.

I have tried solutions I found online but none worked. Im thinking dsquery

Is their a simple way to do this??

Thanks for reading

booga73
Posts: 108
Joined: 30 Nov 2011 16:16

Re: query Active directory display name

#2 Post by booga73 » 15 Feb 2014 22:04

Hi,

At the command prompt of your computer, you can try this for starters:

Code: Select all

for /f "skip=1 tokens=2 delims=\" %a in ('wmic /node:xxx.xxx.xxx.xxx ComputerSystem Get UserName') do @echo %a


the xxx.xxx.xxx.xxx is the ip address of the remote computer.

this, I beleive will help with finding who's logged in remotely.

hope that helps to start off with what you're asking.

v/r Booga73

confuseis
Posts: 11
Joined: 11 Dec 2011 09:59

Re: query Active directory display name

#3 Post by confuseis » 16 Feb 2014 06:05

Ive tried that and manually replaced the xxx.xxx.xxx.xxx with the ip address of the machine. I get the error 192.168.1.10 was unexpected at this time. I begs the question how do I get the Ip address into that line? I have looked to see if there is an enviornmental variable for ip address but couldnt find one.

This command Im using would become a batch file and then copied to all network users start up folders so it would start at boot and output the "friendly" name of the user back to a csv file on a network share

e.g. dummy command below

dsquery %username% -displayname >> \\server\share\Usernamelist.txt

Any inkling?

Thanks

Ed Dyreen
Expert
Posts: 1569
Joined: 16 May 2011 08:21
Location: Flanders(Belgium)
Contact:

Re: query Active directory display name

#4 Post by Ed Dyreen » 16 Feb 2014 09:32

confuseis wrote:Ive tried that and manually replaced the xxx.xxx.xxx.xxx with the ip address of the machine. I get the error 192.168.1.10 was unexpected at this time. I begs the question how do I get the Ip address into that line? I have looked to see if there is an enviornmental variable for ip address but couldnt find one.
even if I don't replace xxx.xxx.xxx.xxx it works here.

Code: Select all

Microsoft Windows XP [versie 5.1.2600]
(C) Copyright 1985-2001 Microsoft Corp.

>for /f "skip=1 tokens=2 delims=\" %a in ('wmic /node:xxx.xxx.xxx.xxx ComputerSystem Get UserName') do @echo %a
Knooppunt - xxx.xxx.xxx.xxx
Fout:
Code = 0x800706ba
Beschrijving = De RPC-server is niet beschikbaar.
Faciliteit = Win32

>
try this

Code: Select all

wmic /node:xxx.xxx.xxx.xxx ComputerSystem Get UserName
confuseis wrote:Im looking to create a logon script that will query the output the user and the computer they are using.
this script must be run from client side

Code: Select all

@echo off &setlocal enableDelayedExpansion

set "$computerName=%computerName%"
echo.$computerName=%$computerName%_

set "$name=%userName%"
echo.$name=%$name%_

set "$=" &for /f "skip=1 delims=" %%? in (

   'net.EXE user !$name!'

) do    if not defined $ set "$=%%~?"
::
set "$fullName=" &set "$fullName=!$:~41!" ||set "$fullName=!$name!"
echo.$fullName=%$fullName%_

pause
exit

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

Re: query Active directory display name

#5 Post by Squashman » 16 Feb 2014 11:08

look at the NET USER command. That is what I use in all my scripts to get the person's real name.

confuseis
Posts: 11
Joined: 11 Dec 2011 09:59

Re: query Active directory display name

#6 Post by confuseis » 17 Feb 2014 11:18

Regarding the net user command I have found

net user %username% /domain | find "display name"

I get no error so I gather that it worked however I need to output the display name that it grabs I think perhaps a third pipe?

Ill test the other suggestion's and report back

Thanks

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

Re: query Active directory display name

#7 Post by Squashman » 17 Feb 2014 12:25

Code: Select all

For /f "tokens=2* delims= " %%G in ('net user %username% ^/domain ^|find "Full Name"') do set fullname=%%G

foxidrive
Expert
Posts: 6031
Joined: 10 Feb 2012 02:20

Re: query Active directory display name

#8 Post by foxidrive » 17 Feb 2014 17:47

I think the forward slash is fine without escaping, but mainly the %username% may contain spaces or & so it would have to be quoted.

Code: Select all

For /f "tokens=2* delims= " %%G in ('net user "%username%" /domain ^|find "Full Name"') do set "fullname=%%G"

confuseis
Posts: 11
Joined: 11 Dec 2011 09:59

Re: query Active directory display name

#9 Post by confuseis » 18 Feb 2014 16:40

So far I have net user /domain | find "display name"

this runs but gives no output

Running on its own it gives useful but to much output.

I need to grab just the full user name name / display name only & export this to a text file

Thanks

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

Re: query Active directory display name

#10 Post by Squashman » 18 Feb 2014 17:11

confuseis wrote:So far I have net user /domain | find "display name"

That is not the Example I provided.

confuseis
Posts: 11
Joined: 11 Dec 2011 09:59

Re: query Active directory display name

#11 Post by confuseis » 19 Feb 2014 18:30

Ah I see the net user is enclosed. Doh

I have tried all suggestions I'm get "%%G was unexpected at this time."

Im trying to interpret the command in english

Im seeing
for the variable %%G perfrom the operation in brackets and then have %%G contain the answer/output of that operation.

I dont understand the tokens & delim but ill look it up on the weekend.

Thanks

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

Re: query Active directory display name

#12 Post by Squashman » 20 Feb 2014 09:41

confuseis wrote:I have tried all suggestions I'm get "%%G was unexpected at this time."

Because I assume you tried running that code from the cmd prompt.
If you read the help for the FOR command you will see this.
To use the FOR command in a batch program, specify %%variable instead
of %variable. Variable names are case sensitive, so %i is different
from %I.


Which basically means you use TWO percent symbols in a batch file and only one percent symbol at the cmd prompt.

booga73
Posts: 108
Joined: 30 Nov 2011 16:16

Re: query Active directory display name

#13 Post by booga73 » 20 Feb 2014 10:08

here's another option to identify who's logged in:

Code: Select all

for /f "tokens=1-2 delims=\" %g in ('whoami') do @echo %h


I've tried that here on my end at the command prompt, and returned the user name.

v/r Booga73

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

Re: query Active directory display name

#14 Post by Squashman » 20 Feb 2014 10:46

booga73 wrote:here's another option to identify who's logged in:

Code: Select all

for /f "tokens=1-2 delims=\" %g in ('whoami') do @echo %h


I've tried that here on my end at the command prompt, and returned the user name.

v/r Booga73


The OP wants the person's full name. Not the Active Directory user name.

Just realized I was using the wrong token in my code.
This is from the cmd prompt.

Code: Select all

H:\>For /f "tokens=2* delims= " %G in ('net user "%username%" /domain ^|find "Full Name"') do echo %H

H:\>echo John Smith
John Smith

booga73
Posts: 108
Joined: 30 Nov 2011 16:16

Re: query Active directory display name

#15 Post by booga73 » 21 Feb 2014 06:31

agh, yes, that's correct, ty; Great job on the scripting.

v/r Booga73

Post Reply