Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations bkrike on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

LDAP querie 1

Status
Not open for further replies.

stupman

Technical User
Apr 11, 2003
77
BE
Hi,

Would like to write an LDAP query in a vbscript that spits out all users in the Active Directory. Anyone has any ideas how best to start??

Thx!!

Grtz, Rein.
 
This will help you I think...

Code:
Function GetAllUsers()
	' Description:  Returns all Users in the entire current domain.
	' Retuns:       an array with the users found
    Dim objRootDSE, objConnection, objCommand, objRecordSet, objUser
    Dim OutPut
    
    Set objRootDSE = GetObject("LDAP://rootDSE")
    Set objConnection = CreateObject("ADODB.Connection")
    objConnection.Open "Provider=ADsDSOObject;"
    Set objCommand = CreateObject("ADODB.Command")
    objCommand.ActiveConnection = objConnection
    objCommand.CommandText = "<LDAP://" & objRootDSE.get("defaultNamingContext") & _
    				">;(&(objectCategory=person)(objectClass=user));" & _ "distinguishedName,sAMAccountName;subtree"
    Set objRecordSet = objCommand.Execute
    ReDim OutPut(0)
    While Not objRecordSet.EOF
		On Error Resume next
        Set objUser = GetObject("LDAP://" & objRecordSet.Fields("distinguishedName") & "")
        If Err.Number = 0 then
	        'Do a binary comparison to see if account is disabled
	        If Not objUser.get("userAccountControl") And 2 Then
	            If OutPut(0) <> "" Then ReDim Preserve OutPut(UBound(OutPut) + 1)
' 	            OutPut(UBound(OutPut)) = SearchGivenName (objRecordSet.Fields("sAMAccountName"))
	            OutPut(UBound(OutPut)) = objRecordSet.Fields("sAMAccountName")
	        End If
	    End if
	    On Error GoTo 0
        Set objUser = Nothing
        objRecordSet.MoveNext
    Wend
    objConnection.Close
    GetAllUsers = OutPut
    Set objRecordSet = Nothing
    Set objCommand = Nothing
    Set objConnection = Nothing
    Set objRootDSE = Nothing
End Function

Also check out faq329-5688

Good Luck...

_____________________________________
Feed a man a fish and feed him for a day.
Teach a man to fish and feed him for a lifetime...
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top