Solved

Searc for User in Active Directory

Posted on 2006-11-12
2
1,084 Views
Last Modified: 2010-08-05
How can i search for a user in ad OU and then return a result...

IF objContUser = Getobject("LDAP://CN=" & strUserID & "," & strDomainOU & "," & objRootDSE.Get("defaultNamingContext")) <>"" THEN
    msgbox "User Exist"
Else
    msgbox "User doesn't exist"
End If
0
Comment
Question by:dion_p1
  • 2
2 Comments
 
LVL 16

Expert Comment

by:mdiglio
ID: 17930233
Hello,
Try something like this.

Set a reference to:
Microsoft ActiveX Data Objects 2.7 Library

Private Sub Command1_Click()
If DoesExist("your.domain.com", "username") Then
    MsgBox "User Exist"
Else
    MsgBox "User doesn't exist"
End If
End Sub

Private Function DoesExist(strDomain As String, strUser As String) As Boolean

Const ADS_SCOPE_SUBTREE = 2
Set cnn = CreateObject("ADODB.Connection")
Set cmd = CreateObject("ADODB.Command")
cnn.Provider = "ADsDSOObject"
cnn.Open "Active Directory Provider"
Set cmd.ActiveConnection = cnn
cmd.Properties("Page Size") = 1000
cmd.Properties("Timeout") = 30
cmd.Properties("Searchscope") = ADS_SCOPE_SUBTREE
cmd.Properties("Cache Results") = False

cmd.CommandText = "SELECT name from 'LDAP://" & strDomain & "' WHERE objectCategory = 'user' AND SAMAccountName = '" & strUser & "'"

Set rs = cmd.Execute
If rs.EOF Then
    DoesExist = False
  Else
    DoesExist = True
End If

End Function
0
 
LVL 16

Accepted Solution

by:
mdiglio earned 500 total points
ID: 17930351
Hello again,
After looking at your question history it seems like you use vbscript.
The above code was for VB6.

This is vbscript.
You can also put the message boxes in the function itself instead of returning the true/false then displaying the message


if DoesExist("your.domain.com", "theUserName") then
   wscript.echo "Users Exists"
else
   wscript.echo "Users Doesn't Exist"
end if

function DoesExist(strDomain, strUser )

Const ADS_SCOPE_SUBTREE = 2
Set cnn = CreateObject("ADODB.Connection")
Set cmd = CreateObject("ADODB.Command")
cnn.Provider = "ADsDSOObject"
cnn.Open "Active Directory Provider"
Set cmd.ActiveConnection = cnn
cmd.Properties("Page Size") = 1000
cmd.Properties("Timeout") = 30
cmd.Properties("Searchscope") = ADS_SCOPE_SUBTREE
cmd.Properties("Cache Results") = False

cmd.CommandText = "SELECT name from 'LDAP://" & strDomain & "' WHERE objectCategory = 'user' AND SAMAccountName = '" & strUser & "'"

Set rs = cmd.Execute
If rs.EOF Then
    DoesExist = false
  Else
      DoesExist = true
End If

End function

wscript.quit
0

Featured Post

Master Your Team's Linux and Cloud Stack

Come see why top tech companies like Mailchimp and Media Temple use Linux Academy to build their employee training programs.

Question has a verified solution.

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

If you have ever used Microsoft Word then you know that it has a good spell checker and it may have occurred to you that the ability to check spelling might be a nice piece of functionality to add to certain applications of yours. Well the code that…
Article by: Martin
Here are a few simple, working, games that you can use as-is or as the basis for your own games. Tic-Tac-Toe This is one of the simplest of all games.   The game allows for a choice of who goes first and keeps track of the number of wins for…
Get people started with the process of using Access VBA to control Excel using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Excel. Using automation, an Access application can laun…
Show developers how to use a criteria form to limit the data that appears on an Access report. It is a common requirement that users can specify the criteria for a report at runtime. The easiest way to accomplish this is using a criteria form that a…

770 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