Listing05_List_DCs (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
domain = "xyz.com"  'domain to retrieve DCs from
user   = "administrator@xyz.com"
passwd = "password"

' Need to get domain's default naming context
' (i.e. the domains DN)
Dim rootDSE
Set rootDSE = GetObject("LDAP://" & domain & "/RootDSE")
aDsPath =  "LDAP://" & domain & "/" & _
              rootDSE.Get("defaultNamingContext")

' Open the domain object with the supplied credentials
Dim ldapObj
Set ldapObj = GetObject("LDAP:")
Dim domainObj
Set domainObj = ldapObj.OpenDsObject(aDsPath,user,passwd,1)
' Retrieve the NTDS DNs that master for this domain
Dim masteredBy
masteredBy = domainObj.GetEx("masteredBy")

' Iterate through each NTDS object, get its parent
' and print out the dNSHostName attribute
Dim ntdsDN
for each ntdsDN in masteredBy
   Dim ntdsObj
   ntdsDN = "LDAP://" & strDomain & "/" & ntdsDN
   Set ntdsObj = ldapObj.OpenDsObject(ntdsDN,user,passwd,1)
   Dim serverObj
   Set serverObj = ldapObj.OpenDsObject(ntdsObj.Parent,user,passwd,1)
   Wscript.echo serverObj.Get("dNSHostName")
next

This code has been viewed 1188 times.

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