Modify user - LDAP (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

use Win32::OLE;

use strict;

my ($objConn, $objComm, $objRS, $objUser);
my ($strBase, $strFilter, $strAttrs, $strScope);
# **********************************************************************
# Set the ADO search criteria
# **********************************************************************
$strBase = '<LDAP://dc=mycorp,dc=com>;';
$strFilter = '(&(objectclass=user)(objectcategory=person)(department=Sales));';
$strAttrs = 'ADsPath;';
$strScope = 'Subtree';

$objConn = Win32::OLE->new('ADODB.Connection');
$objConn->{Provider} = 'ADsDSOObject';
$objConn->Open();
# **********************************************************************
# Need to enable Paging in case there are more than 1000 objects returned
# **********************************************************************
$objComm = Win32::OLE->new('ADODB.Command');
$objComm->{ActiveConnection} = $objConn;
$objComm->{CommandText} = $strBase . $strFilter . $strAttrs . $strScope;
$objComm->SetProperty("Properties", 'Page Size', 1000);
$objRS = $objComm->Execute();
while (!$objRS->EOF) {
    $objUser = Win32::OLE->GetObject($objRS->Fields->Item('ADsPath')->Value);
    $objUser->{LoginScript} = 'login-sales.vbs';
    $objUser->SetInfo();
    if ((0 + Win32::OLE::LastError()) != 0) {
        print $objUser->Name . ' error occurred', "\n";
        Win32::OLE::LastError(0);
    }
    else {
        print $objUser->Name . ' modified', "\n";
    }
    $objRS->MoveNext();
}

This code has been viewed 1613 times.

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