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

Find userID from full name using AD 1

Status
Not open for further replies.

Nitrous270

Technical User
Jul 27, 2002
67
0
0
US
Okay I was looking at the script in this thread and wondering is there a way to go backwards. If I have a user's full name is there a way to go backwards and retreive their user ID?



Also as an FYI from that thread I tried the following and received an error on line 6, "There is no such object on the server" I don't know if my problems using this script below are going to cause me problems trying to do what I asked above.

Set oNetwork = CreateObject("WScript.Network")
sDomain = oNetwork.UserDomain
sUser = oNetwork.UserName
Set oRootDSE = GetObject("LDAP://RootDSE")
sDomain = oRootDSE.Get("defaultNamingContext")
Set oUser = GetObject("LDAP://CN=" & sUser & ",CN=Users," & sDomain)
WScript.Echo oUser.DisplayName


When I used the following I had no problem.

Set oNetwork = CreateObject("WScript.Network")
sDomain = oNetwork.UserDomain
sUser = oNetwork.UserName
sADSPath= sDomain & "/" & sUser
Set oUser = GetObject("WinNT://" & sADSPath & ",user")
WScript.Echo oUser.FullName

I'm familar with vbs but do not have any experience in Active Directory, but I'm trying to read up on it any help is appreicate thanks.
 
To find the user from fullname, you can only do it by enum all users and match the fullname. But have to note that fullname is only optional which might not be set.
 
From tsuji's post I think I want to clarify how I currently retrieve this information.

I'm using MS Outlook, connected to an exchange server.

I get a list of names but I need their user ID so I enter the list into Outlook as if I'm going to address the email to these users. Sometimes the name is spelled incorrectly and it will not resolve automatically so I use the address book to resolve it. After the list is resolved I look at the alias field using the address book. That field has the person's user ID.

Also in our environment the full name is always populated unless it is a special user ID that doesn't belong to one person. I'm not really concerned about those accounts so that is not a issue. Thank you tsuji that is a helpful hint.

Meanwhile, I'm going to keep reading on the subject.
 
If you were only ever going to search for one name then this would be ok

Set objDomain = GetObject("WinNT://rmplc,domain")
objDomain.Filter = Array("User")
str2Find = "Mr P J Wilson"
For Each aUser In objDomain
If LCase(str2Find) = LCase(aUser.FullName) Then
Wscript.Echo str2Find & "=" & aUser.Name
End If
Next
Set objDomain = Nothing

If you were going to search for multiple names then i would say you would only want to query AD once then hold the results in memory

Set dicNetBiosUsers = CreateObject("Scripting.Dictionary")
Set objDomain = GetObject("WinNT://rmplc,domain")
objDomain.Filter = Array("User")

For Each aUser In objDomain
Wscript.Echo aUser.Name & "=" & aUser.FullName
dicNetBiosUsers.Add UCase(aUser.Name), aUser.FullName
Next
Set objDomain = Nothing

'populate an array/dic object from text file some other method???
Set dicResults = CreateObject("Scirption.Dictionary")
dicResults.Add "Mr P J Wilson", ""
dicResults.Add "Mr J Jones", ""
For Each aKey In dicResults
For Each aUser In dicNetBiosUsers
If LCase(aKey) = LCase(dicNetBiosUsers.Item(aUser))
Wscript.Echo aKey & "=" & aUser
dicResults.Item(aKey) = aUser
End If
Next
Next

For Each aKey In dicResults
Wscript.Echo aKey & "=" & dicResults.Item(aKey)
Next


Perhaps some will say taht the dicNetBiosUsers is wasted and should really be an array...would have been nice if the thing you started with was unique but there you go.

this might be nicer? but i am sure in the past i have found that it just runs off to AD each time you loop through and takes forever

For Each aUser In objDomain
Wscript.Echo aUser.Name & "=" & aUser.FullName
dicNetBiosUsers.Add aUser, ""
Next

'this then allows NB you might need a SET in there somewhere
For Each aKey In dicResults
For Each aUser In dicNetBiosUsers
If LCase(aKey) = LCase(aUser.FullName)'''or aUser.Whatever
Wscript.Echo aKey & "=" & aUser.Name
dicResults.Item(aKey) = aUser.Name
End If
Next
Next
 
actully the last more OO based example should be more like

For Each aUser In objDomain
Wscript.Echo aUser.Name & "=" & aUser.FullName
dicNetBiosUsers.Add aUser.Name, aUser
Next

'this then allows NB you might need a SET in there somewhere
For Each aKey In dicResults
For Each aUser In dicNetBiosUsers
Set objUser = dicNetBiosUsers.Item(aUser)
If LCase(aKey) = LCase(objUser.FullName)'''or objUser.Whatever
Wscript.Echo aKey & "=" & aUser
dicResults.Item(aKey) = aUser
End If
Set objUser = Nothing
Next
Next


...i think
 
Kob3 has a good FAQ on the subject.
faq329-5688

I hope you find this post helpful.

Regards,

Mark
 
mrmovie's very first little bit of code helped me come up with this then I put in an excel spreadsheet manipulated the input and output the way i wanted to show on the spreadsheet and tied it to a command button on the spreadsheet.

Works great for me here is my final code for both functions.

Thanks again for the help.


Function strName2UserID(strDomain, strNameInput)

If Len(strDomain) = 0 Or Len(strNameInput) = 0 Then
' exit the function if no input is provided in either of the fields
Exit Function
End If

' get rid of extra spaces
strDomain = Trim(strDomain)
strNameInput = Trim(strNameInput)

Dim objDomain ' domain properties
Dim arrstrUser ' array of users
Dim blnOneUser ' flag for the first result

' get the domain properties and filter the results for users only
Set objDomain = GetObject("WinNT://" & strDomain)
objDomain.Filter = Array("User")

' initialize the varible for the first result found
blnOneUser = True

' initialize the function in case no results are found
strName2UserID = "N/A"

' search for all cases where the full name matches and output the userID
For Each arrstrUser In objDomain
If LCase(strNameInput) = LCase(Left(arrstrUser.FullName, Len(strNameInput))) Then
If blnOneUser Then
strName2UserID = arrstrUser.FullName & "=" & arrstrUser.Name

' concate the any results found after the first result
blnOneUser = False
Else
strName2UserID = strName2UserID & "; " & arrstrUser.FullName & "=" & arrstrUser.Name
End If
End If
Next

' clean up
Set objDomain = Nothing
End Function



Public Function strUserID2Name(strDomain, strID)

If Len(strDomain) = 0 Or Len(strID) = 0 Then
' exit the function if no input was provided
Exit Function
End If

' get rid of extra spaces
strDomain = Trim(strDomain)
strID = Trim(strID)

Dim strADSPath ' users active directory path
Dim objUser ' user properties

' set active directory path for the input
strADSPath = strDomain & "/" & strID

On Error Resume Next

' get the use properties for the provided domain and user
Set objUser = GetObject("WinNT://" & strADSPath & ",user")

' output the results
If Err.Number = -2147024843 Then
strUserID2Name = "N/A"
Else
strUserID2Name = strID & "=" & objUser.FullName
End If

On Error GoTo 0

' clean up
Set objUser = Nothing

End Function
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top