Hi I am trying to write a script and am hoping that someone will be able to help me.
This is what I would like the script to do:
1- List all domain controllers in the domain and export the names to a text file. Then count the number of Domain Controller. See code bellow:
'List Domain Controllers in the Forest
Sub SUBDCsInForest()
ReportProgress "List Domain Controllers in the Forest"
'WScript.Echo StrDCsInForest
Const ForAppending = 8
Set objFSO = CreateObject("Scripting.FileSystemObject")
Set objTextFile = objFSO.OpenTextFile(StrDCsInForestFile, ForAppending, True)
Const ADS_SCOPE_SUBTREE = 2
Set objRootDSE = GetObject("LDAP://RootDSE")
strConfigurationNC = objRootDSE.Get("configurationNamingContext")
Set objConnection = CreateObject("ADODB.Connection")
Set objCommand = CreateObject("ADODB.Command")
objConnection.Provider = "ADsDSOObject"
objConnection.Open "Active Directory Provider"
Set objCommand.ActiveConnection = objConnection
objCommand.Properties("Page Size") = 1000
objCommand.Properties("Searchscope") = ADS_SCOPE_SUBTREE
objCommand.CommandText = "SELECT ADsPath FROM 'LDAP://" & strConfigurationNC & "' WHERE objectClass='nTDSDSA'"
Set objRecordSet = objCommand.Execute
objRecordSet.MoveFirst
Do Until objRecordSet.EOF
Set objParent = GetObject(GetObject(objRecordset.Fields("ADsPath")).Parent)
'WScript.Echo objParent.CN
objTextFile.WriteLine(objParent.CN)
objRecordSet.MoveNext
Loop
objTextFile.Close
'Count the Number of Domain Controllers in the Forest
Dim objFSO, strTextFile, strData, arrLines, LineCount
Const ForReading = 1
'name of the text file
'strTextFile = "sample.txt"
'Create a File System Object
Set objFSO = CreateObject("Scripting.FileSystemObject")
'Open the text file - strData now contains the whole file
strData = objFSO.OpenTextFile(StrDCsInForestFile,ForReading).ReadAll
'Split by lines, put into an array
arrLines = Split(strData,vbCrLf)
'Use UBound to count the lines
StrNumberofDCs = UBound(arrLines) '+ 1
objTextFile.Close
'WScript.echo StrNumberofDCs
'Cleanup
'Set objFSO = Nothing
End Sub
This section is working.
2- Read the text file and take the name of each Domain Controller and query each of the DC's and
List Active Directory Database Replication Partners into a word document.
So far I have been able the reading text and output it into word.
' Read StrDCsInForestFile Text File into Word
Const ForReading = 1
Set objFSO = CreateObject("Scripting.FileSystemObject")
Set objTextFile = objFSO.OpenTextFile (StrDCsInForestFile, ForReading)
Do Until objTextFile.AtEndOfStream
strNextLine = objTextFile.Readline
StrDCsInList = Split(strNextLine , ",")
objSelection.TypeText StrDCsInList(0) & vbCrLf
For i = 1 To UBound(StrDCsInList)
objSelection.TypeText StrDCsInList(i) & vbCrLf
Next
Loop
objTextFile.Close
But I do not know how to read the name and run a query on each and output that into word.
Here is the query I would like get working.
Version:0.9
StartHTML:00000107
EndHTML:00003696
EndFragment:00003656
EndFragment:00000000
' List Active Directory Database Replication Partners
strComputer = "omldc01"
Set objWMIService = GetObject("winmgmts:" _
& "{impersonationLevel=impersonate}!\\" & _
strComputer & "\root\MicrosoftActiveDirectory")
Set colReplicationOperations = objWMIService.ExecQuery _
("Select * from MSAD_ReplNeighbor")
For each objReplicationJob in colReplicationOperations
Wscript.Echo "Domain: " & objReplicationJob.Domain
Wscript.Echo "Naming context DN: " & objReplicationJob.NamingContextDN
Wscript.Echo "Source DSA DN: " & objReplicationJob.SourceDsaDN
Wscript.Echo "Last synch result: " & objReplicationJob.LastSyncResult
Wscript.Echo "Number of consecutive synchronization failures: " & _
objReplicationJob.NumConsecutiveSyncFailures
Next
,
Thank you.
Jules