?
Solved

Searc for User in Active Directory

Posted on 2006-11-12
2
Medium Priority
?
1,096 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
[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
  • 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 2000 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

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Background What I'm presenting in this article is the result of 2 conditions in my work area: We have a SQL Server production environment but no development or test environment; andWe have an MS Access front end using tables in SQL Server but we a…
If you need to start windows update installation remotely or as a scheduled task you will find this very helpful.
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…
Get people started with the utilization of class modules. Class modules can be a powerful tool in Microsoft Access. They allow you to create self-contained objects that encapsulate functionality. They can easily hide the complexity of a process from…
Suggested Courses
Course of the Month11 days, 12 hours left to enroll

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