Listing06_Enable_GC (VBScript)

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

'# From the book "Managing Enterprise Active Directory Services"
'# ISBN: 0-672-32125-4

ON ERROR RESUME NEXT
Dim adsPath, user, passwd, server
server = "dc1"
user   = "administrator@xyz.com"
passwd = "password"

' Get configuration naming context
Dim rootDSE
Set rootDSE = GetObject("LDAP://" & server & "/RootDSE")
adsPath =  "<LDAP://" & server & "/" & _
              rootDSE.Get("configurationNamingContext") & ">;"

' Find the server object for this DC
Dim criteria, properties, scope
strCriteria = "(&(objectClass=server)(objectCategory=server)" _
              & "(cn=" & server & "));"
properties = "distinguishedName;"
scope = "SubTree"

Dim objConnect, objCommand, objRecordSet
Set objConnect = CreateObject("ADODB.Connection")
objConnect.Provider = "ADsDSOObject"
objConnect.Open "Active Directory Provider", user, passwd
Set objCommand = CreateObject("ADODB.Command")
Set objCommand.ActiveConnection = objConnect
'Set the query string
objCommand.CommandText  = adsPath & criteria _
                          & properties & scope
'Execute the query
Set objRecordSet = objCommand.Execute
Dim numObjs
numObjs = objRecordSet.RecordCount
objRecordSet.MoveFirst
If numObjs = 1 then
   Dim serverDN, nTDSSettingsDN, ntdsObj, ldapObj
   serverDN = objRecordSet.Fields(0).Value
   nTDSSettingsDN = "LDAP://" & server  _ 
                        & "/cn=NTDS Settings," & serverDN
   ' Open the NTDS Settings object with the supplied credentials
   Set ldapObj = GetObject("LDAP:")
   Set ntdsObj = ldapObj.OpenDsObject(nTDSSettingsDN, _
                                      user,           _
                                      passwd,1)
   ntdsObj.Put "options",1
   ntdsObj.SetInfo
   If Err.Number then
      Wscript.Echo "Error occurred when setting options attribute: " _
              & Err.Description
      Wscript.Quit
   Else 
      Wscript.Echo "Enabled GC for server " & strServer
   End If
Else
    Wscript.Echo "Search did not return expected results: " _
                 & numObjs & " matching entries found"
End If

This code has been viewed 986 times.

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