WriteCache (Perl)

This code can be found in Chapter 21 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 constant vbCrLf => "\r\n";
use constant vbTab => "\x09";

use strict;

# **********************************************************************
# Declare the constants and variables
# **********************************************************************
use constant ADSTYPE_CASE_IGNORE_STRING => 3;
use constant ADS_PROPERTY_UPDATE => 2;

my $objPropValue;
# An ADSI PropertyValue object
my $objPropEntry;
# An ADSI PropertyEntry object
my $objUser;
# The user whose property list you want to investigate
my $strText;
# A text string that displays results in one message box
my $intPropCount;
# The number of properties
my $intIndex;
# The index used while looping through the property list

$objUser = Win32::OLE->GetObject('LDAP://cn=AlistairGLN,ou=Sales,dc=mycorp,dc=com');
$objUser->GetInfo();

# **********************************************************************
# Section A: Calculate the property count, and enumerate each 
# property's name and datatype
# **********************************************************************
$intPropCount = $objUser->PropertyCount;
print 'There are ' . $intPropCount . ' values in the property cache before adding the new one.', "\n";

$strText = '';
for my $intIndex (0 .. ($intPropCount - 1)) {
    $strText = $strText . $objUser->Item($intIndex)->Name . vbTab . $objUser->Item($intIndex)->ADsType . vbCrLf;
}
print "$strText\n";

# **********************************************************************
# Section B: Create a property entry, and write it to the cache
# **********************************************************************
$objPropValue = Win32::OLE->new('PropertyValue');
$objPropValue->{ADsType} = ADSTYPE_CASE_IGNORE_STRING;
$objPropValue->{CaseExactString} = '0123-456-7890';

$objPropEntry = Win32::OLE->new('PropertyEntry');
$objPropEntry->{Name} = 'pager';
$objPropEntry->{Values} = [$objPropValue];
$objPropEntry->{ADsType} = ADSTYPE_CASE_IGNORE_STRING;
$objPropEntry->{ControlCode} = ADS_PROPERTY_UPDATE;

$objUser->PutPropertyItem($objPropEntry);

# **********************************************************************
# Section C: Write out the cache to Active Directory and read the new
# cache explicitly back in from the object
# **********************************************************************
$objUser->SetInfo();
$objUser->GetInfo();

# **********************************************************************
# Section D: Recalculate the property count, and re-enumerate each
# property's name and datatype to see the changes
# **********************************************************************
$intPropCount = $objUser->PropertyCount;

print 'There are ' . $intPropCount . ' values in the property cache after adding the new one.', "\n";

$strText = '';
for my $intIndex (0 .. ($intPropCount - 1)) {
    $strText = $strText . $objUser->Item($intIndex)->Name . vbTab . $objUser->Item($intIndex)->ADsType . vbCrLf;
}
print "$strText\n";

This code has been viewed 747 times.

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