Add many group members (Perl)

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 Perl 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)
# **************************************************************************

use Win32::OLE;

if ((SearchAD('LDAP://dc=mycorp,dc=com', '(&(objectCategory=group)(cn=USER_*))', 'SubTree', 'cn,ADsPath', $arrUSERGroup))) {

    # **************************************************************************
    # As above but for DRUP_ groups
    # **************************************************************************
    if ((SearchAD('LDAP://dc=mycorp,dc=com', '(&(objectCategory=group)(cn=DRUP_*))', 'SubTree', 'cn,ADsPath', $arrDRUPGroup))) {

        # ***********************************************************************
        # 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 = $#$arrUSERGroups;
        for my $intUSERGroupIndex (0 .. $arrUSERGroupsUb) {
            # ***********************************************************************
            # As above but for DRUP_ groups
            # ***********************************************************************
            $arrDRUPGroupsUb = $#$arrDRUPGroups;
            for my $intDRUPGroupIndex (0 .. $arrDRUPGroupsUb) {
                # ***********************************************************************
                # Extract the portion of the name that corresponds to all letters after
                # the "USER_" or "DRUP_" parts (i.e., five  letters)
                # ***********************************************************************
                $txtUSERGroupSuffixName = substr(arrUSERGroup($intUSERGROUPIndex, 0), -1 * (length(arrUSERGroup($intUSERGROUPIndex, 0)) - 5));
                $txtDRUPGroupSuffixName = substr(arrDRUPGroup($intDRUPGroupIndex, 0), -1 * (length(arrDRUPGroup($intDRUPGroupIndex, 0)) - 5));
                # ***********************************************************************
                # If the two extracted strings are not the same, then add the USER group
                # to the DRUP group
                # ***********************************************************************
                if (!(_compareValues($txtUSERGroupSuffixName, $txtDRUPGroupSuffixName, 2))) {
                    $objDRUPGroup = Win32::OLE->GetObject(arrDRUPGroup($intDRUPGroupIndex, 1));
                    if (!$objDRUPGroup->IsMember(arrUSERGroup($intUSERGROUPIndex, 1))) {
                        $objDRUPGroup->Add(arrUSERGroup($intUSERGROUPIndex, 1));
                        $objDRUPGroup->SetInfo();
                    }
                }
            }
        }
    }
}

sub _isNumeric {
    no warnings qw(numeric);
    return ($_[0] + 0) eq $_[0];
}

sub _compareValues {
    my($lhs, $rhs, $cval) = @_;
    my $cmpVal= (_isNumeric($lhs) || _isNumeric($rhs)) ? ($lhs <=> $rhs) : ($lhs cmp $rhs);
    # Map (-1, 0, 1) => (1, 2, 4) and do a bit-and
    return $cval & (1, 2, 4)[$cmpVal + 1];
}

This code has been viewed 1301 times.

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