Solved

Searc for User in Active Directory

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

Better Security Awareness With Threat Intelligence

See how one of the leading financial services organizations uses Recorded Future as part of a holistic threat intelligence program to promote security awareness and proactively and efficiently identify threats.

Join & Write a Comment

Introduction In a recent article (http://www.experts-exchange.com/A_7811-A-Better-Concatenate-Function.html) for the Excel community, I showed an improved version of the Excel Concatenate() function.  While writing that article I realized that no o…
Enums (shorthand for ‘enumerations’) are not often used by programmers but they can be quite valuable when they are.  What are they? An Enum is just a type of variable like a string or an Integer, but in this case one that you create that contains…
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…
This lesson covers basic error handling code in Microsoft Excel using VBA. This is the first lesson in a 3-part series that uses code to loop through an Excel spreadsheet in VBA and then fix errors, taking advantage of error handling code. This l…

728 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

Need Help in Real-Time?

Connect with top rated Experts

14 Experts available now in Live!

Get 1:1 Help Now