Add or remove replica (VBScript)

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 VBScript code from this site.

' This VBScript code adds or removes a replica server for the
' specified application partition

' ---------------------------------------------------------------
' From the book "Active Directory Cookbook" by Robbie Allen
' ISBN: 0-596-00466-4
' ---------------------------------------------------------------

' ------ SCRIPT CONFIGURATION ------
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.
strServer  = "<DomainControllerName>"  ' e.g. dc01

' Set to True to add server as new replica or False to remove
boolAdd    = True  
' ------ END CONFIGURATION ---------

' Constants taken from ADS_PROPERTY_OPERATION_ENUM
const ADS_PROPERTY_APPEND = 3
const ADS_PROPERTY_DELETE = 4

set objRootDSE = GetObject("LDAP://RootDSE")

' ----------------------------------------------------------
' First need to find the NTDS Settings object for the server
' ----------------------------------------------------------
strBase    = "<LDAP://cn=Sites," & _        
             objRootDSE.Get("ConfigurationNamingContext") & ">;"
strFilter  = "(&(objectclass=server)(objectcategory=server)(cn=" & _
             strServer & "));" 
strAttrs   = "cn,distinguishedName;"
strScope   = "subtree"
set objConn = CreateObject("ADODB.Connection")
objConn.Provider = "ADsDSOObject"
objConn.Open "Active Directory Provider"
set objRS = objConn.Execute(strBase & strFilter & strAttrs & strScope)
if objRS.RecordCount <> 1 then
   WScript.Echo "Did not find a match for server " & strServer
   WScript.Quit
else
   objRS.MoveLast
   strServerDN = "cn=NTDS Settings," & _
                 objRS.Fields("distinguishedName").Value
   Wscript.Echo "Found server: "
   WScript.Echo strServerDN
   Wscript.Echo
end if

' ------------------------------------------------------------------
' 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"
set objRS = objConn.Execute(strBase & strFilter & strAttrs & strScope)
if objRS.RecordCount <> 1 then
   WScript.Echo "Did not find a match for application partition " & _
                strAppPart
   WScript.Quit
else
   objRS.MoveLast
   set objAppPart = GetObject("LDAP://" &  objRS.Fields("distinguishedName").Value )
   Wscript.Echo "Found app partition: "
   WScript.Echo objRS.Fields("distinguishedName").Value
   WScript.Echo
end if

' -----------------------------------------------
' Lastly, either add or remove the replica server
' -----------------------------------------------
if boolAdd = TRUE then
   objAppPart.PutEx ADS_PROPERTY_APPEND, "msDS-NC-Replica-Locations", _
                    Array(strServerDN)
   objAppPart.SetInfo
   WScript.Echo "Added server to replica set"
else
   objAppPart.PutEx ADS_PROPERTY_DELETE, "msDS-NC-Replica-Locations", _
                    Array(strServerDN)
   objAppPart.SetInfo
   WScript.Echo "Removed server from replica set"
end if

This code has been viewed 3272 times.

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