?
Solved

How to convert a username into their "Display Name"

Posted on 2009-04-07
7
Medium Priority
?
2,827 Views
Last Modified: 2013-12-24
I am trying to write a script to lookup and display a users information (Display Name, Phone Number, etc.) based on their username.  I found the code below which will do an LDAP lookup based on the display name, and that works great.  To get that to work though, I would need to convert "JJJohnson" into "Johnson, John J" or else be able to do the LDAP lookup based on their logon name rather then their display name.  (Either way would be fine with me)

Any assistance would be greatly appriciated!

Rick
Set objUser = GetObject ("LDAP://cn=""Johnson, John J"",ou=hpusers,ou=users,ou=accounts,dc=mycompanyname,dc=int")
 
objUser.GetInfo
 
'Get user information
strDisplayName = objUser.Get("displayName")
strPhysicalDeliveryOfficeName = objUser.Get("physicalDeliveryOfficeName")
strTelephoneNumber = objUser.Get("telephoneNumber")
strMail = objUser.Get("mail")
 
 
'Display user information
Wscript.Echo strDisplayName
Wscript.Echo strPhysicalDeliveryOfficeName
Wscript.Echo strTelephoneNumber
Wscript.Echo strMail

Open in new window

0
Comment
Question by:HealthPartners
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 4
  • 3
7 Comments
 
LVL 76

Expert Comment

by:David Lee
ID: 24091220
Hi, Healthpartners.

No need to convert names.  There's an easier way.
strName = InputBox("Enter a username")
Set adoCon = CreateObject("ADODB.Connection")
adoCon.Provider = "ADsDSOObject"
adoCon.CursorLocation = 3
adoCon.Open "ADSI"
Set adoCmd = CreateObject("ADODB.Command")
adoCmd.ActiveConnection = adoCon
'Change the domain name on the next line'
adoCmd.CommandText = "SELECT displayName,physicalDeliveryOfficeName,telephoneNumber,mail FROM 'LDAP://mycompany.int' Where objectClass='user' AND objectCategory='Person' AND samAccountName='" & strName & "'"
adoCmd.Properties("Size Limit") = 5000
adoCmd.Properties("Page Size") = 100
adoCmd.Properties("Timeout") = 30
adoCmd.Properties("Cache Results") = False
Set adoRS = adoCmd.Execute()
Do Until adors.EOF
    Wscript.Echo adoRS.Fields("displayName")
    Wscript.Echo adoRS.Fields("physicalDeliveryOfficeName")
    Wscript.Echo adoRS.Fields("telephoneNumber")
    Wscript.Echo adoRS.Fields("mail")
    adors.MoveNext
Loop

Open in new window

0
 

Author Comment

by:HealthPartners
ID: 24091953
Thanks BlueDevilFan, that is exactly what I am looking for!   However, when I converted it to VB.Net, I am getting an "Argument 'Prompt' cannot be converted to type 'String'."  Exception on the Msgbox lines.   How can I display that info in VB.net?

Thanks again!

Rick
        strName = "JJJohnson"
 
        Dim adoCon = CreateObject("ADODB.Connection")
        adoCon.Provider = "ADsDSOObject"
        adoCon.CursorLocation = 3
        adoCon.Open("ADSI")
        Dim adoCmd = CreateObject("ADODB.Command")
        adoCmd.ActiveConnection = adoCon
        'Change the domain name on the next line'
        adoCmd.CommandText = "SELECT displayName,physicalDeliveryOfficeName,telephoneNumber,mail FROM 'LDAP://healthpartners.int' Where objectClass='user' AND objectCategory='Person' AND samAccountName='" & strName & "'"
        adoCmd.Properties("Size Limit") = 5000
        adoCmd.Properties("Page Size") = 100
        adoCmd.Properties("Timeout") = 30
        adoCmd.Properties("Cache Results") = False
        Dim adoRS = adoCmd.Execute()
        Do Until adoRS.EOF
            MsgBox(adoRS.Fields("displayName"))
            MsgBox(adoRS.Fields("physicalDeliveryOfficeName"))
            MsgBox(adoRS.Fields("telephoneNumber"))
            MsgBox(adoRS.Fields("mail"))
 
            adoRS.MoveNext()
        Loop

Open in new window

0
 
LVL 76

Expert Comment

by:David Lee
ID: 24092066
I'm not very good with VB.net.  That aside, what line does the error occur on?  It might be that one of the fields is returning a null value.  

Out of curiosity, why convert this to VB.net?  From the zones I thought you wanted VBScript.
0
Get free NFR key for Veeam Availability Suite 9.5

Veeam is happy to provide a free NFR license (1 year, 2 sockets) to all certified IT Pros. The license allows for the non-production use of Veeam Availability Suite v9.5 in your home lab, without any feature limitations. It works for both VMware and Hyper-V environments

 

Author Comment

by:HealthPartners
ID: 24092145
Sorry about that, I added VB.net as a tag, but forgot to add it as a zone.  I am building this functionality into an existing vb.net app.  

It occurs on any of the Msgbox lines 17-20.
0
 
LVL 76

Accepted Solution

by:
David Lee earned 2000 total points
ID: 24092218
No problem.  

Try adding .Value to each of those lines.  Something like this

   MsgBox(adoRS.Fields("displayName").Value)

Value is the default property, but perhaps VB.net doesn't see it that way.  Unfortunately I don't have VS on the computer that accesses a domain, so I can't test in .Net.  If adding Value doesn't do it, then try setting the value into a variable and then use the variable name in the MsgBox.  Something like

    Dim strName as String
    strName = adoRS.Fields("displayName").Value
    MsgBox(strName)
0
 

Author Closing Comment

by:HealthPartners
ID: 31567657
Worked like a charm, thanks!
0
 
LVL 76

Expert Comment

by:David Lee
ID: 24093186
You're welcome.  Glad I could help.
0

Featured Post

On Demand Webinar: Networking for the Cloud Era

Did you know SD-WANs can improve network connectivity? Check out this webinar to learn how an SD-WAN simplified, one-click tool can help you migrate and manage data in the cloud.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

A Stored Procedure in Microsoft SQL Server is a powerful feature that it can be used to execute the Data Manipulation Language (DML) or Data Definition Language (DDL). Depending on business requirements, a single Stored Procedure can return differe…
Recently I was talking with Tim Sharp, one of my colleagues from our Technical Account Manager team about MongoDB’s scalability. While doing some quick training with some of the Percona team, Tim brought something to my attention...
This is a high-level webinar that covers the history of enterprise open source database use. It addresses both the advantages companies see in using open source database technologies, as well as the fears and reservations they might have. In this…
In this video, Percona Solution Engineer Rick Golba discuss how (and why) you implement high availability in a database environment. To discuss how Percona Consulting can help with your design and architecture needs for your database and infrastr…
Suggested Courses

752 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question