get first and last name from AD based on logged in name

390 views
Skip to first unread message

sumGirl

unread,
Apr 16, 2006, 10:35:42 PM4/16/06
to VBScript
Hey all. I know how to get current logged in user account, but I want
to grab that info and look it up in AD and get the first and last name
of that user...anyone help?

j@ckle

unread,
Apr 18, 2006, 9:18:33 AM4/18/06
to VBScript
Here is a script I use in our logon script to log user information.

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

Set objRootDSE = GetObject("LDAP://RootDSE")

If Err.Number = 0 Then
strNamingContext = objRootDSE.Get("defaultNamingContext")
WScript.Echo("Connected to:...... " & strNamingContext)
Else
WScript.Echo("Unable to connect to Active Directory." & vbCrLf &
"Exiting Script." & vbCrLf)
objShell.Popup line, 10, vbTab & "Logon Script", 48
objLogFile.Close
Wscript.Quit
End If

Set objADSysInfo = CreateObject("ADSystemInfo")
strUserDN = objADSysInfo.username
WScript.Echo("DN:................ " & strUserDN)

' Bind to user object
Set objUser = Getobject("LDAP://" & strUserDN)
WScript.Echo "UserID:............ " & objUser.Get("name")
WScript.Echo "First name:........ " & objUser.Get("givenName")
WScript.Echo "Initials:.......... " & objUser.Get("initials")
WScript.Echo "Last name:......... " & objUser.Get("sn")
WScript.Echo "Display name:...... " & objUser.Get("displayName")
WScript.Echo "Description:....... " & objUser.Get("description")
WScript.Echo "Office:............ " &
objUser.Get("physicalDeliveryOfficeName")
WScript.Echo "Telephone number:.. " & objUser.Get("telephoneNumber")
WScript.Echo "E-Mail:............ " & objUser.Get("mail")

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

If you get any errors about information not found in the cache just
delete/comment out the line that is giving the error. Some companies
don't store information in all of the fields.

-= j@ckle =-

sumGirl

unread,
Apr 18, 2006, 5:48:24 PM4/18/06
to VBScript
Hi j@ckle, thanks for the help. Where in this script do I specify the
account name I want to look up?

j@ckle

unread,
Apr 19, 2006, 6:40:55 AM4/19/06
to VBScript
This is to be run on the machine doing the lookup (via logon script,
etc.). It will use the username of whoever is logged on (or
logging-in).

If you want to specify the username, I will have to modify. Don't have
time right now...I post modified code later today.

Reply all
Reply to author
Forward
0 new messages