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
Solved

Searc for User in Active Directory

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

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

Introduction I needed to skip over some file processing within a For...Next loop in some old production code and wished that VB (classic) had a statement that would drop down to the end of the current iteration, bypassing the statements that were c…
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 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…
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…

839 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