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 Chris Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Get the Exchange display name 2

Status
Not open for further replies.

JPJeffery

Technical User
May 26, 2006
600
GB
I bet this one is easy but I've spent hours trying to get it and keep falling just short.

How do I find out programmatically what the current user's Exchange Display Name is from the user's logon name?

So, I logon as jeffery_j, how do I confirm from that that my full Exchange Display name is 'Jeff Jeffery' (by the way, should it be important, our 'Simple Display Names' do not take the same format as our logon names, for instance mine is 'jefferyj' rather than 'jeffery_j'...I don't know why this is our standard, so please don't ask, but I also need to point out that even this 'standard' isn't consistent so I wouldn't want to rely on it!)?


JJ
[small][purple]Variables won't. Constants aren't[/purple][/small]
 
Code:
Set WSHNetwork = CreateObject("Wscript.Network")
UserName = WSHNetwork.UserName

UserDisplayName = ReturnDisplayName(UserName)
Wscript.Echo UserDisplayName

Function ReturnDisplayName(strLN)
    Dim oRootDSE, oConnection, oCommand, oRecordSet
    Set oRootDSE = GetObject("LDAP://rootDSE")
    Set oConnection = CreateObject("ADODB.Connection")
    oConnection.Provider = "ADSDSOOBJECT"
    oConnection.Open "ADs Provider" 
    Set oCommand = CreateObject("ADODB.Command")
    oCommand.ActiveConnection = oConnection
    oCommand.CommandText = "<LDAP://" & oRootDSE.Get("DefaultNamingContext") & _ 
        ">;(&(objectCategory=User)(samAccountName=" & strLN & "));displayName;subtree"
	oCommand.Properties("Page Size") = 99 
    Set oRecordSet = oCommand.Execute
    On Error Resume Next
    
    ReturnDisplayName = oRecordSet.Fields("displayName")
    
    On Error GoTo 0
    oConnection.Close
    Set oRecordSet = Nothing
    Set oCommand = Nothing
    Set oConnection = Nothing
    Set oRootDSE = Nothing

End Function

I hope you find this post helpful.

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.
 
Thanks both.

JJ
[small][purple]Variables won't. Constants aren't[/purple][/small]
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top