Users in an OU versus users in a Group

Posted on 2006-05-24
Last Modified: 2010-04-30
From the code like this:

Set objCon = CreateObject("ADODB.Connection")
objCon.Open "Provider=ADsDSOObject;"
Set objCmd = CreateObject("ADODB.Command")
Set objCmd.ActiveConnection = objCon

objCmd.CommandText = "<LDAP://>;(&(objectCategory=Group)(cn=Group1));distinguishedname,name,member;range=0-9;subtree"

Set objRS = objCmd.Execute

Do While (Not objRS.EOF)
  WScript.Echo objRS("distinguishedname")
  WScript.Echo objRS("name")
  WScript.Echo Join(objRS("member;range=0-9"), vbCrLf)

I can now get all the users in a Group (with a range) ......... however, if I try to get all the users in an OU (with a range) (which I know the code above might change a little) I am having trouble with the objCmd.CommandText line ..... if the OU=NeedVacation, how could I modify the query to work for the OU?
Question by:249Central
    LVL 14

    Expert Comment

    The range code only applies to selecting multi-valued attributes of objects in an ADO search.  It does not apply to OUs, since users are not "attributes" of an OU the way a member list is.

    Why do you need to select a specific "range" of users from an OU?  I don't quite understand where this question is coming from, but I can probably help you with the solution once I see what it is you really need to accomplish.

    Author Comment

    The problem is that this particular OU has 2000+ users its not returning all of them.
    LVL 14

    Accepted Solution

    Ah, I've run into that before as well.  To resolve this particular problem, you need to perform a "paged search".  Luckily, most of that is performed behind the scenes by ADO, and you script needs only one modification:  before you call objCon.Execute, do this:

    objCon.Properties("Page Size") = 1000

    Even though you set the "Page Size" property to 1000, your script will obtain all of the users in the group for your RecordSet.  The paging is performed by ADO (it retrieves 1000 results at a time from the Domain Controller, as many times as are needed to get the complete list).

    Author Comment

    I was a little lost ... thanks.

    Featured Post

    What Security Threats Are You Missing?

    Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

    Join & Write a Comment

    There are many ways to remove duplicate entries in an SQL or Access database. Most make you temporarily insert an ID field, make a temp table and copy data back and forth, and/or are slow. Here is an easy way in VB6 using ADO to remove duplicate row…
    You can of course define an array to hold data that is of a particular type like an array of Strings to hold customer names or an array of Doubles to hold customer sales, but what do you do if you want to coordinate that data? This article describes…
    Get people started with the process of using Access VBA to control Excel using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Excel. Using automation, an Access application can laun…
    Show developers how to use a criteria form to limit the data that appears on an Access report. It is a common requirement that users can specify the criteria for a report at runtime. The easiest way to accomplish this is using a criteria form that a…

    733 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    21 Experts available now in Live!

    Get 1:1 Help Now