Filter Results (Perl)

This code can be found in Chapter 22 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;

use constant adStateOpen => 1;

my $objFileSystem;
# A FileSystemObject
my $objOutput;
# A TextStream Object
my $objConn;
# An ADO Connection object
my $objRS;
# An ADO Recordset object
my $intCount;
# An integer

# *****************************************************************************
# Create the file if it doesn't exist or truncate it if it does exist
# *****************************************************************************
$objFileSystem = Win32::OLE->new('Scripting.FileSystemObject');
$objOutput = $objFileSystem->CreateTextFile('c:\\out.txt', 1);

# *****************************************************************************
# Write out the current time and date using the VBScript 'Now' function
# *****************************************************************************
$objOutput->WriteLine('Starting...' . VBS::Now());

$objConn = Win32::OLE->new('ADODB.Connection');
$objConn->{Provider} = 'ADSDSOObject';
$objConn->Open('', 'CN=Administrator,CN=Users,dc=mycorp,dc=com', '');
if ($objConn->State == adStateOpen) {
    $objOutput->WriteLine('Authentication Successful!');
}
else {
    $objOutput->WriteLine('Authentication Failed.');
    exit (1);
}

$objRS = $objConn->Execute('<LDAP://dc=mycorp,dc=com>;(&(objectClass=user)(objectCategory=person)); ' . 'cn;SubTree');

# *****************************************************************************
# Loop through the ASCII characters letters Asc("a") to Asc("z")
# where Asc("a") = 97 and Chr(97) = "a"
# *****************************************************************************
for my $intCount (97 .. 122) {
    $objRS->{Filter} = 'cn LIKE \'' . chr($intCount) . '*\'';
    $objOutput->WriteLine(chr($intCount) . ' = ' . $objRS->RecordCount);
}

$objConn->Close();
$objRS = undef;

$objOutput->Close();

package VBS;
use strict;

use Win32::OLE::NLS qw(:TIME
		       :DATE
		       GetLocaleInfo GetUserDefaultLCID
                       LOCALE_SMONTHNAME1 LOCALE_SABBREVMONTHNAME1
                       LOCALE_SDAYNAME1 LOCALE_SABBREVDAYNAME1
		       LOCALE_IFIRSTDAYOFWEEK
		       LOCALE_SDATE LOCALE_IDATE
		       LOCALE_SGROUPING
		       );
use Win32::OLE::Variant;

use constant EPOCH       => 25569;
use constant SEC_PER_DAY => 86400;

our $lcid;
BEGIN {
    $lcid = GetUserDefaultLCID();
    Win32::OLE->Option(LCID => $lcid);
}

# Return the local time in seconds since the epoch.
sub _localtime_in_sec {
    require Time::Local;
    return 2.0 * CORE::time() - Time::Local::timelocal(gmtime);
}

# Extract specific information out of a date.
sub _extract_from_date {
    my($date,$method,$format) = @_;
    return unless $date;
    unless (UNIVERSAL::isa($date, "Win32::OLE::Variant")) {
        $date = Variant(VT_DATE, $date);
    }
    return $date->$method($format, $lcid);
}

# Returns the current date and time according to the setting of your
# computer's system date and time.
sub Now {
    return Variant(VT_DATE, EPOCH + _localtime_in_sec()/SEC_PER_DAY);
}

This code has been viewed 1523 times.

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