List All Users LDAP Query with Attributes

If you need to extract a list of users from the Active Directory, with customizable list of attributes, here's one for you.

'==========================================================================
' NAME: ListAllusers.vbs
'
' AUTHOR: june.castillote@gmail.com
' DATE  : 03/18/2011
'
' COMMENT: This is for extracting the list of users with their attribute values.
'        Once you run the script, it will output the result to UserList-Extended.txt in TAB-delimited format.
' USAGE  : ListAllusers.vbs
'==========================================================================

 
On Error Resume Next
Dim FileToWrite, fsoWrite
Set fsoWrite = CreateObject("Scripting.FileSystemObject")
Set FileToWrite = fsoWrite.CreateTextFile("UserList-Extended.txt")

Dim objConnection, objQuery, objRootDSE, strDomain
Dim strFilter, strQuery, objRecordSet, gt
Set objConnection = CreateObject("ADODB.Connection")
Set objQuery = CreateObject("ADODB.Command")
objConnection.Provider = "ADsDSOOBject"
objConnection.Open "Active Directory Provider"
Set objQuery.ActiveConnection = objConnection
Set objRootDSE = GetObject("LDAP://RootDSE")

'Retrieve the domain
strDomain = objRootDSE.Get("defaultNamingContext")
strBase = ""

'To filter results only to user objects
strFilter = "(&(objectCategory=user))"

'Populate the required attributes.
'You can find the list of available attributes here: http://www.kouti.com/tables/userattributes.htm

strAttributes = "distinguishedName,Name"


'Query String
strQuery = strBase & ";" & strFilter & ";" & strAttributes & ";subtree"
objQuery.CommandText = strQuery
objQuery.Properties("Page Size") = 99999
objQuery.Properties("Timeout") = 300
objQuery.Properties("Cache Results") = False

FileToWrite.WriteLine "Name" & vbTab & "DN"
Set objRecordSet = objQuery.Execute
objRecordSet.MoveFirst
Do Until objRecordSet.EOF
    FileToWrite.WriteLine objRecordSet.Fields("Name") & vbTab &objRecordSet.Fields("distinguishedName")
    Wscript.Echo  objRecordSet.Fields("Name") & vbTab &objRecordSet.Fields("distinguishedName")
    objRecordSet.MoveNext
Loop

' Clean up
objConnection.Close
FileToWrite.Close
Set objConnection = Nothing
Set objQuery = Nothing
Set objRootDSE = Nothing
Set objRecordSet = Nothing
Set fsoWrite = Nothing
Set FileToWrite = Nothing
Share:

No comments:

Post a Comment

Popular Posts

Powered by Blogger.