Find application partitions for a server (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 finds the application partitions hosted by the specified server.

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

' ----------------------------------------------------------
' First need to find the NTDS Settings object for the server
' ----------------------------------------------------------
set objRootDSE = GetObject("LDAP://RootDSE")
strBase    =  "<LDAP://cn=Sites," & _
              objRootDSE.Get("ConfigurationNamingContext") & ">;"
strFilter  = "(&(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 object: "
   WScript.Echo strServerDN
   Wscript.Echo
end if

' ------------------------------------------------------------------
' 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"
set objRS = objConn.Execute(strBase & strFilter & strAttrs & strScope)
if objRS.RecordCount = 0 then
   WScript.Echo "Server " & strServer & _
                " does not host any application partitions"
   WScript.Quit
else
   Wscript.Echo "App partitions hosted by server " & strServer & ": "
   objRS.MoveFirst
   while not objRS.EOF
      WScript.Echo " " & objRS.Fields("nCName").Value
      objRS.MoveNext
   wend   
end if

This code has been viewed 2649 times.

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