Add or remove replica (Perl)
This code can be found in
Chapter 18 of Active Directory Cookbook, 2nd 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.
# This Perl code adds or removes a replica server for the
# specified application partition
# ---------------------------------------------------------------
# Adapted from VBScript code contained in the book:
# "Active Directory Cookbook" by Robbie Allen
# ISBN: 0-596-00466-4
# ---------------------------------------------------------------
# ------ SCRIPT CONFIGURATION ------
my $strAppPart = "<AppPartitionFQDN>"; # DNS name of the application partition
# Hostname of server to add as replica for app partition/
# This needs to match the common name for the DC's server $object/
my $strServer = "<DomainControllerName>"; # e.g. dc01
# Set to 1 to add server as new replica or 0 to remove
my $boolAdd = 1;
# ------ END CONFIGURATION ---------
use Win32::OLE;
$Win32::OLE::Warn = 3;
# Constants taken from ADS_PROPERTY_OPERATION_ENUM
my $ADS_PROPERTY_APPEND = 3;
my $ADS_PROPERTY_DELETE = 4;
my $objRootDSE = Win32::OLE->GetObject("LDAP://RootDSE");
# ----------------------------------------------------------
# First need to find the NTDS Settings $object for the server
# ----------------------------------------------------------
my $strBase = "<LDAP://cn=Sites," . $objRootDSE->Get("ConfigurationNamingContext") . ">;";
my $strFilter = "(&(objectclass=server)(objectcategory=server)(cn=$strServer));";
my $strAttrs = "cn,distinguishedName;";
my $strScope = "subtree";
my $objConn = Win32::OLE->CreateObject("ADODB.Connection");
$objConn->{Provider} = "ADsDSOObject";
$objConn->Open;
my $objRS = $objConn->Execute($strBase . $strFilter . $strAttrs . $strScope);
if ($objRS->RecordCount != 1 ) {
print "Did not find a match for server $strServer\n";
exit;
}
else {
$objRS->MoveLast;
$strServerDN = "cn=NTDS Settings," . $objRS->Fields("distinguishedName")->Value;
print "Found server: $strServerDN\n";
}
# ------------------------------------------------------------------
# Now need to find the crossRef $object for the application partition
# ------------------------------------------------------------------
$strBase = "<LDAP://cn=Partitions," . $objRootDSE->Get("ConfigurationNamingContext") . ">;";
$strFilter = "(&(objectclass=crossRef)(objectcategory=crossRef)" .
"(dnsRoot=" . $strAppPart . "));";
$strAttrs = "cn,distinguishedName;";
$strScope = "onelevel";
$objRS = $objConn->Execute($strBase . $strFilter . $strAttrs . $strScope);
if ($objRS->RecordCount != 1) {
print "Did not find a match for application partition $strAppPart\n";
exit;
}
else {
$objRS->MoveLast;
my $objAppPart = Win32::OLE->GetObject("LDAP://" . $objRS->Fields("distinguishedName")->Value );
print "Found app partition: ", $objRS->Fields("distinguishedName")->Value, "\n";
}
# -----------------------------------------------
# Lastly, either add or remove the replica server
# -----------------------------------------------
if ($boolAdd == TRUE) {
$objAppPart->PutEx($ADS_PROPERTY_APPEND, "msDS-NC-Replica-Locations", [$strServerDN]);
$objAppPart->SetInfo;
print "Added server to replica set\n";
}
else {
$objAppPart->PutEx($ADS_PROPERTY_DELETE, "msDS-NC-Replica-Locations", [$strServerDN]);
$objAppPart->SetInfo;
print "Removed server from replica set\n";
}
|
This code has been viewed 2373 times.
|
New from the creators of TechTasks.com:
StatSheet.com
|