VBScript for Domain Controllers
Hi!!!
I dont know whether if this post is in the correct section, Ihavent found any section about windows 2003 or scripting.
Ok, this is my question:
I need a vbscript that can list the Domain Controllers in a domain, but only the domain that ill specify in a forest with multiple domains.
AllDCs are Windows Servers 2003.
Could you help me with this task?
Thanks
May 26th, 2008 10:44am
This script will do that, you'll need to edit the RED fields.
Const ADS_SCOPE_SUBTREE = 2
Set objConnection = CreateObject("ADODB.Connection")Set objCommand = CreateObject("ADODB.Command")objConnection.Provider = "ADsDSOObject"objConnection.Open "Active Directory Provider"Set objCOmmand.ActiveConnection = objConnectionobjCommand.CommandText = _ "Select distinguishedName from " & _ "'LDAP://cn=Configuration,DC=fabrikam,DC=com' " _ & "where objectClass='nTDSDSA'" objCommand.Properties("Page Size") = 1000objCommand.Properties("Searchscope") = ADS_SCOPE_SUBTREE
Set objRecordSet = objCommand.ExecuteobjRecordSet.MoveFirstDo Until objRecordSet.EOF Wscript.Echo "Computer Name: " & _ objRecordSet.Fields("distinguishedName").Value objRecordSet.MoveNextLoop
So if your domain is named mydomain.local it would be DC=mydomain,DC=local but if it's named ad.mydomain.local you'd have to add another DC= so the line would read DC=ad,DC=mydomain,DC=local
Free Windows Admin Tool Kit Click here and download it now
May 26th, 2008 10:58am
Joachim thank for your anwser,
My domain is subdomain.domain.net, so Ive done this script:
Const ADS_SCOPE_SUBTREE = 2
Set objConnection = CreateObject("ADODB.Connection")Set objCommand = CreateObject("ADODB.Command")objConnection.Provider = "ADsDSOObject"objConnection.Open "Active Directory Provider"Set objCOmmand.ActiveConnection = objConnectionobjCommand.CommandText = _ "Select distinguishedName from " & _ "'LDAP://CN=Configuration,DC=SubDomain,DC=Domain,DC=net' " _ & "where objectClass='nTDSDSA'" objCommand.Properties("Page Size") = 1000objCommand.Properties("Searchscope") = ADS_SCOPE_SUBTREE
Set objRecordSet = objCommand.ExecuteobjRecordSet.MoveFirstDo Until objRecordSet.EOF Wscript.Echo "Computer Name: " & _ objRecordSet.Fields("distinguishedName").Value objRecordSet.MoveNextLoop
But when I run this script It show methis error:
Error: Table does not exist.
Code: 80040E37
Source: Provider
When I write only: "'LDAP://CN=Configuration,DC=Domain,DC=net' " _ then, dont show me any error and the script show me all the DCs in the forest.
Why doesmy scripthave this behavior?
Bye!
May 26th, 2008 12:21pm
You'll need to run the script from a domain controller in that particular domain. Otherwise the script will have to enumerate all the subdomains first and then enumerate the DC's in each domain, this is of course possible but this script won't do that.
Free Windows Admin Tool Kit Click here and download it now
May 26th, 2008 2:08pm
Hello,
For your reference, if all domain controllers are in the default OU "Domain Controllers" you can simply list all computer accounts in that OU.
----------------------------------------------------Set objOU = GetObject(LDAP://ou=Domain Controllers, dc=subdomain, dc=domain, dc=net)objOU.Filter = Array(Computer)For Each objComputer in objOU Wscript.Echo objComputer.CNNext----------------------------------------------------
May 27th, 2008 2:11am
Hello,
It's correct that by default all domain controller computer objects are in the "Domain Controllers" OU. However due to a denote of a DC there can be an old computer object in this OU that isn't a active DC any longer. So the script provided that looks for
the specific nTDSDSA object is far much more accurate.
Regards,
Rikard
Free Windows Admin Tool Kit Click here and download it now
February 10th, 2012 3:36am
You can use this script for generating Domain Controllers list in Domain,
This script will automatically create a excel file at C:\DCInfo(Date).xls
****************************************************************************
' DCsInformations.vbs
' VBScript program to enumerate all Domain Controllers in the domain.
'
' ----------------------------------------------------------------------
' Copyright (c) 2012 Farook Ismail
' Version 1.0 - May 30, 2012
' Program enumerates all Domain Controllers, their DNS host name, and
' the name of the site they reside in.
'
' You have a royalty-free right to use, modify, reproduce, and
' distribute this script file in any way you find useful, provided that
' you agree that the copyright owner above has no warranty, obligations,
' or liability for such use.
Option Explicit
Dim objRootDSE, strConfig, adoConnection, adoCommand, strQuery
Dim adoRecordset, objDC, objSite, strBase, strFilter, strAttributes, j, r, objExcel, objWB, objSheet, strExportFile
' Determine configuration context from RootDSE object.
Set objRootDSE = GetObject("LDAP://RootDSE")
strConfig = objRootDSE.Get("configurationNamingContext")
' Use ADO to search Active Directory.
Set adoCommand = CreateObject("ADODB.Command")
Set adoConnection = CreateObject("ADODB.Connection")
adoConnection.Provider = "ADsDSOObject"
adoConnection.Open "Active Directory Provider"
adoCommand.ActiveConnection = adoConnection
' Search configuration container of Actice Directory.
strBase = "<LDAP://" & strConfig & ">"
' Filter on objects of class nTDSDSA.
strFilter = "(objectClass=nTDSDSA)"
' Comma delimited list of attribute values to retrieve.
strAttributes = "ADsPath"
' Construct the LDAP syntax query.
strQuery = strBase & ";" & strFilter & ";" & strAttributes & ";subtree"
adoCommand.CommandText = strQuery
adoCommand.Properties("Page Size") = 100
adoCommand.Properties("Timeout") = 30
adoCommand.Properties("Cache Results") = False
Set adoRecordset = adoCommand.Execute
' Excel File Creation Codes
SET objExcel = CREATEOBJECT("Excel.Application")
SET objWB = objExcel.Workbooks.Add
SET objSheet = objWB.Worksheets(1)
strExportFile = "C:\DCInfo"& 10000*Year(Date)+100*Month(Date)+Day(Date) &".xls"
objExcel.ActiveWorkbook.ActiveSheet.Cells(1, 1).Value = "Domain Controller's Infomation"
objExcel.ActiveWorkbook.ActiveSheet.Range("A1:D1").Merge
objExcel.ActiveWorkbook.ActiveSheet.Range("A1:D1").Borders.LineStyle = 1
objExcel.ActiveWorkbook.ActiveSheet.Range("A1:D1").Borders.Weight = 3
objSheet.Cells(1, 1).Font.Bold = True
objSheet.Cells(1, 1).Font.Size = 20
objSheet.Cells(1, 1).HorizontalAlignment = -4108
objSheet.Cells(1, 1).Interior.ColorIndex = 37
objSheet.Cells(2, 1).Value = "Domain Controller"
objSheet.Cells(2, 1).Font.Bold = TRUE
objSheet.Cells(2, 1).Interior.ColorIndex = 40
objSheet.Cells(2, 1).Borders.LineStyle = 1
objSheet.Cells(2, 1).Borders.Weight = -4138
objSheet.Cells(2, 1).HorizontalAlignment = -4108
objSheet.Cells(2, 4).Value = "Distinguished Name"
objSheet.Cells(2, 4).Font.Bold = TRUE
objSheet.Cells(2, 4).Interior.ColorIndex = 40
objSheet.Cells(2, 4).Borders.LineStyle = 1
objSheet.Cells(2, 4).Borders.Weight = -4138
objSheet.Cells(2, 4).HorizontalAlignment = -4108
objSheet.Cells(2, 2).Value = "DNS Host Name"
objSheet.Cells(2, 2).Font.Bold = TRUE
objSheet.Cells(2, 2).Interior.ColorIndex = 40
objSheet.Cells(2, 2).Borders.LineStyle = 1
objSheet.Cells(2, 2).Borders.Weight = -4138
objSheet.Cells(2, 2).HorizontalAlignment = -4108
objSheet.Cells(2, 3).Value = "Active Directory Site Name"
objSheet.Cells(2, 3).Font.Bold = TRUE
objSheet.Cells(2, 3).Interior.ColorIndex = 40
objSheet.Cells(2, 3).Borders.LineStyle = 1
objSheet.Cells(2, 3).Borders.Weight = -4138
objSheet.Cells(2, 3).HorizontalAlignment = -4108
r=2
' The parent object of each object with objectClass=nTDSDSA is a Domain
' Controller. The parent of each Domain Controller is a "Servers"
' container, and the parent of this container is the "Site" container.
Do Until adoRecordset.EOF
On Error Resume Next
If (adoRecordset.Fields("AdsPath").Value <> "") Then
r=r+1
strTempOutPut = ""
Set objDC = GetObject( _
GetObject(adoRecordset.Fields("AdsPath").Value).Parent)
Set objSite = GetObject(GetObject(objDC.Parent).Parent)
objSheet.Cells(r, 1).Value = objDC.Get("Name")
objSheet.Cells(r, 4).Value = objDC.serverReference
objSheet.Cells(r, 2).Value = objDC.DNSHostName
objSheet.Cells(r, 3).Value = objSite.name
objSheet.Cells(r, 1).Borders.LineStyle = 1
objSheet.Cells(r, 1).Borders.Weight = 2
objSheet.Cells(r, 2).Borders.LineStyle = 1
objSheet.Cells(r, 2).Borders.Weight = 2
objSheet.Cells(r, 3).Borders.LineStyle = 1
objSheet.Cells(r, 3).Borders.Weight = 2
objSheet.Cells(r, 4).Borders.LineStyle = 1
objSheet.Cells(r, 4).Borders.Weight = 2
'Wscript.Echo "Domain Controller: " & objDC.Get("Name") _
' & vbCrLf & " Distinguished Name: " & objDC.serverReference _
' & vbCrLf & " DNS Host Name: " & objDC.DNSHostName _
' & vbCrLf & " Site: " & objSite.name
End If
adoRecordset.MoveNext
Loop
objSheet.Range("A2").CopyFromRecordset(adoRecordset)
objWB.SaveAs(strExportFile)
cn.close
SET objSheet = NOTHING
SET objWB = NOTHING
objExcel.Quit()
SET objExcel = NOTHING
'Clean up.
'adoRecordset.Close
'adoConnection.Close
Wscript.Echo "Done"
**********************************************************************
Farook Ismail (M . I . F)
June 5th, 2012 9:03am


