(ldap_client, base_dn, search_filter, attributes, output_object=None, page_size=1000)
return output_array
Get all the Active Directory results from LDAP using a paging approach.
       By default Active Directory will return 1,000 results per query before it errors out.
Get all the Active Directory results from LDAP using a paging approach.
       By default Active Directory will return 1,000 results per query before it errors out.
Get all the Active Directory results from LDAP using a paging approach . By default Active Directory will return 1 000 results per query before it errors out .
def query_ldap_with_paging(ldap_client, base_dn, search_filter, attributes, output_object=None, page_size=1000):
    """Get all the Active Directory results from LDAP using a paging approach.
       By default Active Directory will return 1,000 results per query before it errors out."""

    # Method Variables
    cookie=''
    more_pages = True
    output_array = deque()

    # Paging for AD LDAP Queries
    ldap_control = ldap.controls.SimplePagedResultsControl(True, size=page_size, cookie='')

    while more_pages:
        # Query the LDAP Server
        msgid = ldap_client.search_ext(base_dn, ldap.SCOPE_SUBTREE, search_filter, attributes, serverctrls=[ldap_control])
        result_type, result_data, message_id, server_controls = ldap_client.result3(msgid)

        # Append Page to Results
        for element in result_data:
            if (output_object is None) and (element[0] is not None):
                output_array.append(element[1])
            elif (output_object is not None) and (element[0] is not None):
                output_array.append(output_object(element[1]))

       # Get the page control and get the cookie from the control.
        page_controls = [c for c in server_controls if c.controlType == ldap.controls.SimplePagedResultsControl.controlType]

        if page_controls:
            cookie = page_controls[0].cookie

        # If there is no cookie then all the pages have been retrieved.
        if not cookie:
            more_pages = False
        else:
            ldap_control.cookie = cookie

    return output_array
def query_ldap_with_paging ( ldap_client , base_dn , search_filter , attributes , output_object = None , page_size = 1000 ) : # Method Variables cookie = '' more_pages = True output_array = deque ( ) # Paging for AD LDAP Queries ldap_control = ldap . controls . SimplePagedResultsControl ( True , size = page_size , cookie = '' ) while more_pages : # Query the LDAP Server msgid = ldap_client . search_ext ( base_dn , ldap . SCOPE_SUBTREE , search_filter , attributes , serverctrls = [ ldap_control ] ) result_type , result_data , message_id , server_controls = ldap_client . result3 ( msgid ) # Append Page to Results for element in result_data : if ( output_object is None ) and ( element [ 0 ] is not None ) : output_array . append ( element [ 1 ] ) elif ( output_object is not None ) and ( element [ 0 ] is not None ) : output_array . append ( output_object ( element [ 1 ] ) ) # Get the page control and get the cookie from the control. page_controls = [ c for c in server_controls if c . controlType == ldap . controls . SimplePagedResultsControl . controlType ] if page_controls : cookie = page_controls [ 0 ] . cookie # If there is no cookie then all the pages have been retrieved. if not cookie : more_pages = False else : ldap_control . cookie = cookie return output_array