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: ''
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: ''