Add many group members (VBScript)

This code can be found in Chapter 23 of Active Directory, 3rd Edition

Purchase XP Cookbook or Networking Recipes for only $25 plus shipping! While supplies last.

Find out how to download all of the VBScript code from this site.

' From the book "Active Directory, Third Edition" 
' ISBN: 0-596-10173-2

'**************************************************************************
'Search the entire AD for all groups starting USER_ and return the cn
'and AdsPath variables in the following structure
'
'  arrUSERGroup(index,0) = cn attributes
'  arrUSERGroup(index,1) = ADsPath attribute
'
'where index goes from 0 to (the maximum number of results returned -1)
'**************************************************************************
If (SearchAD("LDAP://dc=mycorp,dc=com", _
       "(&(objectCategory=group)(cn=USER_*))", _
           "SubTree", "cn,ADsPath", arrUSERGroup)) Then

  '**************************************************************************
  'As above but for DRUP_ groups
  '**************************************************************************
  If (SearchAD("LDAP://dc=mycorp,dc=com", _
      "(&(objectCategory=group)(cn=DRUP_*))", _
              "SubTree", "cn,ADsPath", arrDRUPGroup)) Then

    '***********************************************************************
    'Set up an index to allow us to iterate through the USER_ groups. The
    'Ubound function here counts the maximum number of rows returned
    '***********************************************************************
    arrUSERGroupsUb = Ubound(arrUSERGroups)
    For intUSERGroupIndex = 0 To arrUSERGroupsUb
      '***********************************************************************
      'As above but for DRUP_ groups
      '***********************************************************************
      arrDRUPGroupsUb = Ubound(arrDRUPGroups)
      For intDRUPGroupIndex = 0 To arrDRUPGroupsUb
        '***********************************************************************
        'Extract the portion of the name that corresponds to all letters after
        'the "USER_" or "DRUP_" parts (i.e., five  letters)
        '***********************************************************************
        txtUSERGroupSuffixName = Right(arrUSERGroup(intUSERGroupIndex,0), _
          Len(arrUSERGroup(intUSERGroupIndex,0))-5)
        txtDRUPGroupSuffixName = Right(arrDRUPGroup(intDRUPGroupIndex,0), _
          Len(arrDRUPGroup(intDRUPGroupIndex,0))-5)
        '***********************************************************************
        'If the two extracted strings are not the same, then add the USER group
        'to the DRUP group
        '***********************************************************************
        If Not txtUSERGroupSuffixName = txtDRUPGroupSuffixName Then
          Set objDRUPGroup = GetObject(arrDRUPGroup(intDRUPGroupIndex,1))
          If NOT objDRUPGroup.IsMember(arrUSERGroup(intUSERGROUPIndex,1)) Then
           objDRUPGroup.Add(arrUSERGroup(intUSERGroupIndex,1))
           objDRUPGroup.SetInfo
          End If
        End If
      Next
    Next
  End If
End If

This code has been viewed 2831 times.

New from the creators of TechTasks.com: StatSheet.com