Solved

Searc for User in Active Directory

Posted on 2006-11-12
2
1,093 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 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

On Demand Webinar: Networking for the Cloud Era

Ready to improve network connectivity? Watch this webinar to learn how SD-WANs and a one-click instant connect tool can boost provisions, deployment, and management of your cloud connection.

Question has a verified solution.

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

Introduction While answering a recent question (http://www.experts-exchange.com/Q_27402310.html) in the VB classic zone, I wrote some VB code in the (Office) VBA environment, rather than fire up my older PC.  I didn't post completely correct code o…
Introduction While answering a recent question about filtering a custom class collection, I realized that this could be accomplished with very little code by using the ScriptControl (SC) library.  This article will introduce you to the SC library a…
As developers, we are not limited to the functions provided by the VBA language. In addition, we can call the functions that are part of the Windows operating system. These functions are part of the Windows API (Application Programming Interface). U…
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…

724 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