Listing13_Find_FSMOs (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";
my $bind = $ldap->bind($user, password => $passwd);
die "Could not bind to $server" if $bind->code;

# Get the schemaNamingContext value
my $rootdse = $ldap->search(base   => '',
                            filter => '(objectclass=*)',
                            scope  => 'base',
                            attrs => ['rootDomainNamingContext',
                                      'defaultNamingContext']);
my $forestDN = $rootdse->entry(0)->get_value('rootDomainNamingContext');
my $domainDN = $rootdse->entry(0)->get_value('defaultNamingContext');

print "FSMOs known by $server:\n";

# PDC Emulator
my $pdcfsmo = $ldap->search(base   => $domainDN,
                            filter => "(objectclass=*)",
                            scope  => "base",
                            attrs  => ['fsmoroleowner'] );
print "PDC Emulator: ",$pdcfsmo->entry(0)->get_value('fsmoroleowner'),"\n";

# RID Master
my $ridfsmo = $ldap->search(base   => "cn=RID Manager\$,cn=system,$domainDN",
                            filter => "(objectclass=*)",
                            scope  => "base",
                            attrs  => ['fsmoroleowner'] );
print "RID Master: ",$ridfsmo->entry(0)->get_value('fsmoroleowner'),"\n";

# Schema Master
my $schemafsmo = $ldap->search(
                            base   => "cn=Schema,cn=Configuration,$forestDN",
                            filter => "(objectclass=*)",
                            scope  => "base",
                            attrs  => ['fsmoroleowner'] );
print "Schema Master: ",
       $schemafsmo->entry(0)->get_value('fsmoroleowner'),"\n";

# Infrastructure Master
my $infrafsmo = $ldap->search(base   => "cn=Infrastructure,$domainDN",
                            filter => "(objectclass=*)",
                            scope  => "base",
                            attrs  => ['fsmoroleowner'] );
print "Infrastructure Master: ",
       $infrafsmo->entry(0)->get_value('fsmoroleowner'),"\n";

# Domain Naming Master
my $dnfsmo = $ldap->search(base   => "cn=Partitions,cn=Configuration,$forestDN",
                           filter => "(objectclass=*)",
                           scope  => "base",
                           attrs  => ['fsmoroleowner'] );
print "Domain Naming Master: ",
      $dnfsmo->entry(0)->get_value('fsmoroleowner'),"\n";

$ldap->unbind;

This code has been viewed 826 times.

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