Circularish Logging the Event Viewer and Robocopy

I’ve been using Robocopy for a some time now to synchronize information between folders, servers, sites, etc and recently had a client requirement to update a schedule on one job in particular and log error events to the Windows Event Viewer. Due to other requirements on these servers I could not use PowerShell to write the script so I went old school and cracked open my notepad and wrote a VBS file (Visual Basic Script) and a CMD file (Old School Batch Command) to pass the VBS variables and invoke the Robocopy application. The VBScript itself is universal and can be used to pass multiple Robocopy log files from. So you only need one script for which multiple Robocopy Batch files can make calls to for log processing.

RoboworkWhen using this VBScript it will log the success of failure of the Robocopy job by examining the log for errors. If an error is found it will rename the log file with a Date and Time Stamp then create an Error Event in your Windows Application Log (Event Viewer). The script also lets you decide how long to keep your logs (Robocopy is appending the log each time). For example if you wish to keep current and archive logs for 10 days you would pass a 10 to the VBScript from the CMD file you are making your call with. (CMD File Example Below) The Script will keep the current Robocopy Log and the Last Archived Robocopy Log for 10 days, which would give you between 10 and 20 days of logs dependant upon when you reviewed them. The error logs however are not deleted programmatically as they may require your attention. Other information required needed to pass are the name of and location you want for the log file generated by Robocopy.

RobofailYou may or may not want to log “Information Events” (the success of your Robocopy job), especially if you run the job every 30 minutes. You will only need to comment 3 lines in the VBScript. (Documented in file). Commenting out these 3 lines will not stop the Script from reporting errors to the Event Viewer or archiving the failed Robocopy Log. You can also update the Text of the Error and Success message if you wish by updating the information between the double quites (“”) the strMessage value of the Write Application Log for Success or Failure section of the VBScript. The script is fairly straight forward and can bee easily modified to suit your needs and requirements.

Here is the VBScript:

 

' WINDOWS Constants for Event Loging with WSH
const EVENTLOG_SUCCESS = 0
const EVENTLOG_ERROR = 1
const EVENTLOG_WARNING = 2
const EVENTLOG_INFORMATION = 4
const EVENTLOG_AUDIT_SUCCESS = 8
const EVENTLOG_AUDIT_FAILURE = 16

Dim txtLogPath, txtLogName, txtLog, txtLogERROR, strValue, iTotal, iPos, strText, oFSO, oFolder, oFile, intArchiveDays

txtLogPath = Wscript.Arguments(0) ' Retreive Log Path from Batch File
txtLogName = Wscript.Arguments(1) ' Retreive Log Name from Batch File
intArchiveDays = Wscript.Arguments(2) ' Retreive Days to Archive Log from Batch File
txtLog = txtLogPath & txtLogName ' This will put the log file path and name together
txtLogArchive = txtLogPath & txtLogName & "." & "OLD" ' This sets the name of the Archived log file which is cycled when a new archive period begins
txtLogERROR = txtLogPath & Year(date) & "-" & Month(date) & "-" & Day(date) & "_" & Hour(Time) & Minute(Time) & Second(Time) & "_" & txtLogName 'This will rename the log file if the ROBOCOPY Job Fails


' Looks for the Archive Log and deletes it then creates a new Archive if the current log is old enough to archive.
set oFSO = CreateObject("Scripting.FileSystemObject")
set oTS = oFSO.OpenTextFile(txtLog, 8, True)
oTS.close

set oFSO = CreateObject("Scripting.FileSystemObject")
set oTS = oFSO.OpenTextFile(txtLogArchive, 8, True)
oTS.close

set objFSO = CreateObject("Scripting.FileSystemObject")
set objFile = objFso.GetFile(txtLog)

if datediff("d", objFile.DateCreated,Date) > intArchiveDays-1 then
    set objFSO = CreateObject("Scripting.FileSystemObject")
    set objFile = objFso.GetFile(txtLogArchive)
    objFile.Delete
    set objFile = objFso.GetFile(txtLog)
    objFSO.MoveFile txtLog , txtLogArchive
end if

' Get LogFile Contents and cycles thru to find errors
FileContents = GetFile(txtLog)
strText = FileContents
iPos = 1
Do While iPos <= Len(strText)
   If InStr(iPos, UCase(strText), "0X00000") > 0 Then
       iTotal = iTotal + 1
            iPos = InStr(iPos, UCase(strText), "0X00000")_
            + Len("0X00000")
        Else
            Exit Do
        End If
Loop

' Determine if Errors exist in log file
If iTotal > 0 Then
   strErrors = 1
Else
   strErrors = 0
End If

' Write Application Log for Success or Failure
If strErrors = 1 Then

    set objFSO = CreateObject("Scripting.FileSystemObject")
    objFSO.MoveFile txtLog , txtlogERROR
    strMessage = "Robocopy Job Failed - Please check log file: " & txtLogERROR
    set objShell = CreateObject("WScript.Shell")
    objShell.LogEvent EVENTLOG_ERROR, strMessage
    Set WshShell = WScript.CreateObject("WScript.Shell")
Else
   strMessage = "Robocopy Job Completed Successfully"

' If you do not want to send Success Events to the Application Even Viewer Commend out the following 3 lines
    set objShell = CreateObject("WScript.Shell")
    objShell.LogEvent EVENTLOG_INFORMATION, strMessage
    Set WshShell = WScript.CreateObject("WScript.Shell")
End If   

' Readfile function
function GetFile(txtLog)
  If txtLog<>"" Then
    Dim FS, FileStream
    Set FS = CreateObject("Scripting.FileSystemObject")
      on error resume Next
      Set FileStream = FS.OpenTextFile(txtLog)
      GetFile = FileStream.ReadAll
  End If
End Function

Once you have created the VBS File from above (ROBOCOPY_LOGEVENT.VBS) You will need to create a simple batch file to pass the needed variables to your script for processing, in addition this batch file can also execute your actual Robocopy command and then of course you can just schedule this batch file.

Here is a Sample Batch File (CMD):

 

@ECHO OFF
set daysarchive=30
set source="C:\Source_Folder"
set dest="\\ServerName\\Backup_Folder"
set logpath="C:\Log_Folder\"
set logname="Robocopy-log-file.log"
set fullpath="%logpath:"=%%logname:"=%"
ROBOCOPY %source% %dest%  /E /NP /LOG+:%fullpath% /R:1 /W:3
wscript ROBOCOPY_LOGEVENT.VBS %logpath% %logname% %daysarchive%

I hope you find this script as useful as I did. I am working on a better way to Log to the Event Viewer, by creating a Robocopy Event Log in the Windows Event Viewer and logging everything there.

Leave a Reply

You must be logged in to post a comment.

copyright © 2008 askthemct.com