'See http://blogcastrepository.com/blogs/mattbro/articles/478.aspx 'This breaks down the script and explains everything Option Explicit Dim objFSO, WshShell, objErrorFile, objDCList, strDCFileName, strNetLogonErrorFile Dim strCurrDir Dim arrstrDCs() Dim strNetLogonLogsFolder, strNetLogonLogsSrcLoc Dim strDate, strDay, strMonth Const ForReading = 1, ForWriting = 2, ForAppending = 8 Set objFSO = CreateObject("Scripting.FileSystemObject") Set WshShell = WScript.CreateObject("WScript.Shell") strDCFileName = "DClist.txt" strNetLogonLogsFolder = "Exports\Netlogon-logs\" strNetLogonLogsSrcLoc = "\admin$\debug\netlogon.Log" strNetLogonErrorFile = strNetLogonLogsFolder& "__Errors.log" objFSO.CreateFolder("Exports") objFSO.CreateFolder("Exports\Netlogon-logs") strCurrDir = WshShell.CurrentDirectory Set objErrorFile = objFSO.CreateTextFile(strNetLogonErrorFile, True) Call GetDate Call GetDCs Call SortDCArray Call WriteToText Call GetNetLogonLogs Call GetCurrentErrors Call SendEmailIfErrorsFound WScript.Quit '**************************************************************************************** '**************************************************************************************** '**************************************************************************************** Sub GetDate 'Formats the date so we can match it with the date format in the logs strMonth = Month(Now) strDay = Day(Now) If Len(strMonth) = 1 Then strMonth = "0" & strMonth If Len(strDay) = 1 Then strDay = "0" & strDay strDate = strMonth & "/" & strDay 'WScript.Echo strDate End Sub Sub GetDCs 'Finds all the DCs on the domain Dim objRootDSE, strConfig, strDNSDomain Dim objConnection, objCommand Dim strBase, strFilter, strAttributes, strQuery Dim objRecordSet, objDC, k ' Determine DNS domain name. Set objRootDSE = GetObject("LDAP://RootDSE") strDNSDomain = objRootDSE.Get("defaultNamingContext") ' Determine configuration context. strConfig = objRootDSE.Get("configurationNamingContext") ' Use ADO to search Active Directory for ObjectClass nTDSDSA. ' This will identify all Domain Controllers. Set objCommand = CreateObject("ADODB.Command") Set objConnection = CreateObject("ADODB.Connection") objConnection.Provider = "ADsDSOObject" objConnection.Open = "Active Directory Provider" objCommand.ActiveConnection = objConnection strBase = "" strFilter = "(objectClass=nTDSDSA)" strAttributes = "AdsPath" strQuery = strBase & ";" & strFilter & ";" & strAttributes & ";subtree" objCommand.CommandText = strQuery objCommand.Properties("Page Size") = 100 objCommand.Properties("Timeout") = 60 objCommand.Properties("Cache Results") = False Set objRecordSet = objCommand.Execute ' Enumerate parent objects of class nTDSDSA. Save Domain Controller ' DNS host names in dynamic array arrstrDCs. k = 0 Do Until objRecordSet.EOF Set objDC = _ GetObject(GetObject(objRecordSet.Fields("AdsPath")).Parent) ReDim Preserve arrstrDCs(k) arrstrDCs(k) = objDC.DNSHostName k = k + 1 'WScript.Echo objDC.DNSHostName objRecordSet.MoveNext Loop End Sub Sub SortDCArray 'Sorts the DC list alphabetically Dim i, j, temp 'WScript.Echo UBound(arrstrDCs) For i = 0 To UBound(arrstrDCs) For j = 0 To UBound(arrstrDCs) If(arrstrDCs(i) <= arrstrDCs(j)) Then temp = arrstrDCs(i) arrstrDCs(i) = arrstrDCs(j) arrstrDCs(j) = temp End If Next Next End Sub Sub WriteToText 'Writes out a list of all DCs found Dim i Set objDCList = objFSO.CreateTextFile(strDCFileName,True) For i = 0 To UBound(arrstrDCs) objDCList.WriteLine(arrstrDCs(i)) Next End Sub Sub GetNetLogonLogs 'Copies the netlogon.log files from each DC Dim i, strSourceFile, strDestFile For i = 0 To UBound(arrstrDCs) strSourceFile = "\\" & arrstrDCs(i) & strNetLogonLogsSrcLoc strDestFile = strNetLogonLogsFolder & arrstrDCs(i) & "-Netlogon.Log" 'WScript.Echo strSourceFile & vbTab & strDestFile On Error Resume Next objFSO.CopyFile strSourceFile, strDestFile, True If Err.Number <> 0 Then objErrorFile.WriteLine("ERROR COPYING NETLOGON.LOG for server: " & arrstrDCs(i)) Err.Clear End If On Error Goto 0 Next End Sub Sub GetCurrentErrors 'Parses each log file and puts todays records into the error log Dim objNetLogonLogsFolder, File, objFile Dim strLine, strCurrDateLines Set objNetLogonLogsFolder = objFSO.GetFolder(strNetLogonLogsFolder) For Each File In objNetLogonLogsFolder.Files objErrorFile.WriteLine File & VbCrLf Set objFile = objFSO.OpenTextFile(File,ForReading) Do Until objFile.AtEndOfStream strLine = objFile.ReadLine 'This makes sure we only get records from the current date If InStr(strLine,strDate) <> 0 Then 'Add the current line to the variable strCurrDateLines = strCurrDateLines & strLine & vbcrlf Else '***IMPORTANT***This might affect whether you get the correct records 'This resets the variable that stores all of the records for this server if it finds a line that 'does NOT have the correct date - This will make sure that we don't get records from previous years 'This is the only way to do it b/c no year information is stored in the logs strCurrDateLines = "" End If Loop 'Write all of the records for this server to the error log objErrorFile.Write strCurrDateLines Next End Sub Sub SendEmailIfErrorsFound Dim objMessage, strErrorFile, blnDiffsFound 'Dim strHTML Set objErrorFile = Nothing 'Close file so we can attach it 'WScript.Echo strCurrDir & "\" & strNetLogonErrorFile Set objErrorFile = objFSO.OpenTextFile(strCurrDir & "\" & strNetLogonErrorFile, ForReading) strErrorFile = objErrorFile.Readall If InStr(strErrorFile, "NO_CLIENT_SITE") <> 0 Then blnDiffsFound = True 'blnDiffsFound = True 'Use to force e-mail to send during testing If blnDiffsFound = True Then ''Build HTML for e-mail message 'strHTML = "" 'strHTML = strHTML & "" 'strHTML = strHTML & "" 'strHTML = strHTML & "Changes found - Please review ' strHTMLOutputFile & strQuote & " target=content>" & strHTMLOutputFile & "" 'strHTML = strHTML & "" 'strHTML = strHTML & "" 'Sending a text email using a remote server Set objMessage = CreateObject("CDO.Message") objMessage.Subject = "New subnet Report" objMessage.Sender = "matt_broadstock@mycompany.com" objMessage.To = "matt_broadstock@mycompany.com" 'WScript.Echo strHTML objMessage.TextBody = "Subnet not found in AD" 'objMessage.HTMLBody = strHTML 'objMessage.CreateMHTMLBody "file:" & strHTMLOutputFile 'objMessage.Bcc = "you@your.com" objMessage.AddAttachment(strCurrDir & "\" & strNetLogonErrorFile) objMessage.Configuration.Fields.Item _ ("http://schemas.microsoft.com/cdo/configuration/sendusing") = 2 'Name or IP of Remote SMTP Server objMessage.Configuration.Fields.Item _ ("http://schemas.microsoft.com/cdo/configuration/smtpserver") = "smtp.mycompany.com" 'Server port (typically 25) objMessage.Configuration.Fields.Item _ ("http://schemas.microsoft.com/cdo/configuration/smtpserverport") = 25 objMessage.Configuration.Fields.Update objMessage.Send End If End Sub