Option Explicit Dim adoCommand, adoConnection, strBase, strFilter, strAttributes Dim objRootDSE, strDNSDomain, strQuery, adoRecordset, strName, strDN ' Setup ADO objects. Set adoCommand = CreateObject("ADODB.Command") Set adoConnection = CreateObject("ADODB.Connection") adoConnection.Provider = "ADsDSOObject" adoConnection.Open "Active Directory Provider" Set adoCommand.ActiveConnection = adoConnection ' Search entire Active Directory domain. Set objRootDSE = GetObject("LDAP://RootDSE") strDNSDomain = objRootDSE.Get("defaultNamingContext") strBase = "<LDAP://" & strDNSDomain & ">" ' Filter on user objects. strFilter = "(&(objectCategory=person)(objectClass=user))" ' Comma delimited list of attribute values to retrieve. strAttributes = "distinguishedName,sAMAccountName" ' Construct the LDAP syntax query. strQuery = strBase & ";" & strFilter & ";" & strAttributes & ";subtree" adoCommand.CommandText = strQuery adoCommand.Properties("Page Size") = 200 adoCommand.Properties("Timeout") = 30 adoCommand.Properties("Cache Results") = False ' Run the query. Set adoRecordset = adoCommand.Execute ' Enumerate the resulting recordset. Do Until adoRecordset.EOF ' Retrieve values and display. strDN = adoRecordset.fields("distinguishedName").Value strName = adoRecordset.Fields("sAMAccountName").Value Wscript.Echo "Logon Name: " & strName & ", DN: " & strDN ' Move to the next record in the recordset. adoRecordset.MoveNext Loop ' Clean up. adoRecordset.Close adoConnection.Close
$adsiSearcher = [adsisearcher]'(&(objectCategory=person)(objectClass=User))' $adsiSearcher.searchroot = 'LDAP://DC=Contoso,DC=Internal' $searcherResults = $adsiSearcher.findall()
Richard Mueller edited Revision 8. Comment: Replace RGB values with color names in HTML to restore colors
Richard Mueller edited Revision 6. Comment: Removed (en-US) from title, added tag
Richard Mueller edited Revision 4. Comment: Add dsquery * command example
Richard Mueller edited Revision 3. Comment: User -Limit 0 to retrieve more than 100 users, improve ADSI filter for users (to skip contact objects)
Richard Mueller edited Revision 2. Comment: Add VBScript code to retrieve all users from AD
Craig Lussier edited Revision 1. Comment: added en-US to tags and title
Rich Prescott edited Original. Comment: Added links for modules
Awesome