VBScript Conbin reading a list from a text file and taking each name in the list and running a query on each, then ouputing that data into word?

41 views
Skip to first unread message

Jules Wale

unread,
Oct 3, 2013, 11:43:43 AM10/3/13
to vbsc...@googlegroups.com
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
    DoUntil 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
    
    
EndSub

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)

DoUntil objTextFile.AtEndOfStream
    strNextLine = objTextFile.Readline
    StrDCsInList = Split(strNextLine , ",")
    objSelection.TypeText StrDCsInList(0) & vbCrLf
    For i = 1ToUBound(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")

Foreach objReplicationJobin 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

Reply all
Reply to author
Forward
0 new messages