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 Chriss Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Query Active Directory for a list of all users

Status
Not open for further replies.

mflancour

MIS
Joined
Apr 23, 2002
Messages
379
Location
US
Idealy I'd like to simple link a table in ms access. As far as I can tell that's not possible, so this is what I have been trying, but it say's "Table does not exist."

Const ADS_SCOPE_SUBTREE = 2

Set objConnection = CreateObject("ADODB.Connection")
Set objCommand = CreateObject("ADODB.Command")
objConnection.Provider = "ADsDSOObject"
objConnection.Open "Active Directory Provider"
Set objCommand.ActiveConnection = objConnection

objCommand.Properties("Page Size") = 1000
objCommand.Properties("Searchscope") = ADS_SCOPE_SUBTREE

objCommand.CommandText = _
"SELECT Name FROM 'LDAP://dc=mydomain,dc=COM' WHERE objectCategory='user'"
Set objRecordSet = objCommand.Execute

objRecordSet.MoveFirst

Do Until objRecordSet.EOF
MsgBox objRecordSet.Fields("Name").Value

Wscript.Echo objRecordSet.Fields("Name").Value, objRecordSet.Fields("altRecipient").Value
MsgBox objRecordSet.Fields("Name").Value
objRecordSet.MoveNext
Loop
 
I wrote a script a few months back to export a list of users to a text file:

Code:
'=======================================================================================
'
' AUTHOR: 	Ben Christian ([URL unfurl="true"]www.benchristian.com)[/URL]
'
' DATE: 	04-Jun-2006
'
' COMMENT: 	Lists all users in an Active Directory Domain
'		
'		Outputs to a text file.
'
'=======================================================================================


Const ForWriting = 2

'Path for the output file
outputfilePath = "c:\scripts\exchange\List Users\List Users.txt"

Set objFSO = CreateObject("Scripting.FileSystemObject")
Set objTextFile = objFSO.OpenTextFile(outputfilepath, ForWriting, True)

'Get default Domain
Set rootDSE = GetObject("LDAP://RootDSE")
DomainContainer = rootDSE.Get("defaultNamingContext")

'Set up LDAP Query
Set conn = CreateObject("ADODB.Connection")
conn.Provider = "ADSDSOObject"
conn.Open "ADs Provider"

ldapStr = "<LDAP://" & DomainContainer & ">;(&(objectCategory=person)(objectClass=user)(name=*));name;subtree"

Set oComm = CreateObject("ADODB.Command")
oComm.ActiveConnection = conn
oComm.CommandText = ldapStr
oComm.Properties("Sort on") = "name"
oComm.Properties("Page size") = 1500

Set rs = oComm.Execute

Do While Not rs.EOF

	objTextFile.WriteLine(rs.Fields("name").Value)
	rs.MoveNext

Loop

msgbox "Done"

 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top