Retired Microsoft Blog disclaimer

This directory is a mirror of retired "Decrypt My World" MSDN blog and is provided as is. All posting authorship and copyrights belong to respective authors.
Original URL: https://blogs.msdn.microsoft.com/alejacma/2010/01/13/how-to-get-all-dcs-in-a-forest-vbscript/
Post name: How to get all DCs in a Forest (VBScript)
Original author: Alejandro Campos Magencio
Posting date: 2010-01-13T07:04:00+00:00


Hi all,


The following VBScript sample lists all DCs in a Forest:

' Create log file
'
Set objFSO = CreateObject("Scripting.FileSystemObject")
Set objLog = objFSO.CreateTextFile ("log.txt")

' Get Forest's root
'
Set objRoot = GetObject("LDAP://rootDSE")

' Get root's Configuration
'
Set objConfig = GetObject("LDAP://" & objRoot.Get("ConfigurationNamingContext"))

' Search for the Partitions container in root's Configuration
'
objConfig.Filter = Array("crossRefContainer")
For Each objPartition in objConfig
strPartition = "LDAP://" & objPartition.Get("distinguishedName")
Next

' Search in Partitions for all domains in Forest
'
Set objConnection = CreateObject("ADODB.Connection")
objConnection.Open "Provider=ADsDSOObject;"

Set objCommand = CreateObject("ADODB.Command")
objCommand.ActiveConnection = objConnection
objCommand.Properties("Page Size") = 1000

objCommand.CommandText = "<" & strPartition & ">;(&(systemFlags=3));nCName,systemFlags;subTree"
Set objRecordset = objCommand.Execute

' List all domains in Forest
'
Do While Not objRecordset.EOF
' List all DCs in one domain
'
objLog.WriteLine("=================================================")
objLog.WriteLine(objRecordset.Fields(0))
objLog.WriteLine("=================================================")
Set objDCs = GetObject("GC://OU=Domain Controllers," & objRecordset.Fields(0))
For Each objDC in objDCs
objLog.WriteLine(objDC.Get("Name"))
Next
objLog.WriteLine("-------------------------------------------------")

objRecordset.MoveNext
Loop

' We are done!
'
objLog.Close
MsgBox "The End"



I hope this helps.


Regards,



Alex (Alejandro Campos Magencio)


Share this article:

Comments:

Comments are closed.