Find application partitions for a server (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 finds the application partitions hosted by the specified server
# ---------------------------------------------------------------
# Adapted from VBScript code contained in the book:
# "Active Directory Cookbook" by Robbie Allen
# ISBN: 0-596-00466-4
# ---------------------------------------------------------------
# ------ SCRIPT CONFIGURATION ------
# 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
# ------ END CONFIGURATION ---------
use Win32::OLE;
$Win32::OLE::Warn = 3;
# ----------------------------------------------------------
# First need to find the NTDS Settings $object for the server
# ----------------------------------------------------------
my $objRootDSE = Win32::OLE->GetObject("LDAP://RootDSE");
my $strBase = "<LDAP://cn=Sites," . $objRootDSE->Get("ConfigurationNamingContext") . ">;";
my $strFilter = "(&(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 $object: $strServerDN\n";
}
# ------------------------------------------------------------------
# Find the crossRef objects that are hosted by the server
# ------------------------------------------------------------------
$strBase = "<LDAP://cn=Partitions," . $objRootDSE->Get("ConfigurationNamingContext") . ">;";
$strFilter = "(&(objectcategory=crossRef)(msDS-NC-Replica-Locations=" . $strServerDN . "));";
$strAttrs = "nCName;";
$strScope = "onelevel";
$objRS = $objConn->Execute($strBase . $strFilter . $strAttrs . $strScope);
if ($objRS->RecordCount == 0) {
print "Server $strServer does not host any application partitions\n";
exit;
}
else {
print "App partitions hosted by server $strServer:\n";
$objRS->MoveFirst;
while (not $objRS->EOF) {
print " ", $objRS->Fields("nCName")->Value,"\n";
$objRS->MoveNext;
}
}
|
This code has been viewed 2095 times.
|
New from the creators of TechTasks.com:
StatSheet.com
|