Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

vbscript to get the full name of the current user

Status
Not open for further replies.

CodeGrunt

Technical User
May 4, 2010
3
US
I am trying to get the full name of the current user. So far I have been able to figure out how to talk to the Active Directory server and how to get the current users login name, but I am not sure how to query the Active Directory for the users full name. Can anyone help me out? Here is my code thus far.


Set WSHNetwork = CreateObject("WScript.Network")
userName = WSHNetwork.UserName
Set objRootDSE = GetObject("LDAP://RootDSE")
domain = objRootDSE.Get("defaultNamingContext")

Set oConnection = CreateObject("ADODB.Connection")
oConnection.Open "Provider=ADsDSOObject;"
Set oCommand = CreateObject("ADODB.Command")
oCommand.ActiveConnection = oConnection
oCommand.CommandText = "<LDAP://" & domain & _
">;(&(objectCategory=User)(UserName=" & userName & "));name;subtree"
Set oRecordSet = oCommand.Execute
On Error Resume Next
SearchGivenName = oRecordSet.Fields("name")
On Error GoTo 0

oConnection.Close
Set oRecordSet = Nothing
Set oCommand = Nothing
Set oConnection = Nothing
Set oRootDSE = Nothing

The result I get is
Microsoft VBScript runtime error: Object required: ''
 
[0]>Microsoft VBScript runtime error: Object required: ''
Pointing to where?

[1] Fullname is somewhat ambiguous, check out those attributes see cn, or name or combination of sn and givenName meet you've in mind.
>oCommand.CommandText = "<LDAP://" & domain & _
> ">;(&(objectCategory=User)(UserName=" & userName & "));name;subtree"
[tt]oCommand.CommandText = "<LDAP://" & domain & _
">;(&(objectCategory=person)(objectClass=user)(sAMAccountName=" & userName & "));name,cn,displayName,sn,givenName;subtree"[/tt]
 
Thanks for the reply.
The original error "Object required" was on line 16:
oCommand.CommandText = "<LDAP://" & domain & _
">;(&(objectCategory=User)(UserName=" & userName & "));name;subtree"

When I substitute the line you suggested:
oCommand.CommandText = "<LDAP://" & domain & _
">;(&(objectCategory=person)(objectClass=user)(sAMAccountName=" & userName & "));name,cn,displayName,sn,givenName;subtree"

I get the following error:
getName.vbs(18, 1) Active Directory: An invalid directory p
athname was passed

I also tried this and received the same error:
oCommand.CommandText = "<LDAP://" & domain & _
">;(&(objectCategory=person)(objectClass=user)(samAccountName=" & userName & "));CN,sn,GivenName,UserPrincipalName;subtree"

The error occurs on this line:
Set oRecordSet = oCommand.Execute

Thanks for the help!
 
After more research I discovered this is easier than I thought all you need is the following command:
dsquery user domainroot -samid %USERNAME%
You can run it from a batch file and it will return the current users first, last name and middle initial which is what I was looking for. I would still like to know why the above doesn't work incase I need to use something like that in the future.
Thanks for the help.
 
It is easy with VBScript as well.

Have a look at the following FAQ:

The GetDistinguishedName function is easily customized to return whatever AD fields you want. I use this function all the time and you don't need to run a separate process (bat) to get the data you need.

I hope that helps.

Regards,

Mark

Check out my scripting solutions at
Work SMARTER not HARDER. The Spider's Parlor's Admin Script Pack is a collection of Administrative scripts designed to make IT Administration easier! Save time, get more work done, get the Admin Script Pack.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top