Listing04_Create_UPNSuffix (Perl)

This code can be found in Chapter 7 of Managing Enterprise Active Directory Services

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 "Managing Enterprise Active Directory Services"
# ISBN: 0-672-32125-4

use strict;
use Net::LDAP;

my $server  = 'dc1';
my $user    = 'administrator@xyz.com';
my $passwd  = 'password';

my $ldap = Net::LDAP->new($server)
           or die "Could not connect to $server: $@";

# Need to get the ConfigurationNamingContext value from RootDSE
my $rootdse = $ldap->search(base   => '',
                            filter => '(objectclass=*)',
                            scope  => 'base',
                            attrs => ['configurationNamingContext']);
die "Could not get RootDSE: ",$rootdse->error if $rootdse->code;
my $base_dn = ($rootdse->entries)[0]->get_value('configurationNamingContext');
die "Could not get configurationNamingContext from RootDSE\n" unless $base_dn;

my $bind = $ldap->bind($user, password => $passwd);
die "Could not bind to server: ", $bind->error if $bind->code;

my $result = $ldap->search(base=> "cn=Partitions,$base_dn",
                           scope=> 'base',
                           filter=> '(objectclass=crossRefContainer)',
                           attrs => ['name']);
die "Search returned an error: ", $result->error if $result->code;

my $part = ($result->entries)[0];
die "Container not found" unless ref $part;

$part->add(upnsuffixes => 'test.com');
$result = $part->update($ldap);
if ($result->code) {
   print "Could not update upnsuffixes: ",$result->error,"\n";
}
else {
   print "Successfully updated upnsuffixes\n";
}

This code has been viewed 685 times.

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