[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now

x
?
Solved

VBS Script to scan robocopy logs.

Posted on 2009-04-26
1
Medium Priority
?
1,283 Views
Last Modified: 2013-12-01
Ok guys I need something done with this script, and simply don't have the time to deal with it at the moment.  Currently the script below will scan a robocopy log, attach it to an email, and in the email body post rather it was successful or had errors......

What I would like for it to do, is basically the same thing, but I do not want it to attach the log unless it had errors.  So if it is successful, I simply want an email saying that it was successful, no log attached; if it failed, then I want to keep the error message in the subject line, and want it to attach the log......I would also like it to change the subject line, if it was successful then the subject should state Data Copy Successful, if it failed then Data Copy Unsuccessful, etc.

Thanks!
Dim txtSMTPServer, txtTo, txtFrom, txtSubject, txtBody
Dim txtLog, strValue, iTotal, iPos, strText
 
txtSMTPServer = "server.domain.local"	          'SMTP server
txtTo = "user@domain.com"		                  'To Address
txtFrom = "administrator@domain.com"                     'From Address
txtSubject = "Data Backup Status"            'Subject line
txtLog = "c:\robo.log" 'path to the
                                       'robocopy log file
 
'Get the file contents
FileContents = GetFile(txtLog)
 
'-- Cycle thru the log file to find errors --
 
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
 
'-- --
 
If iTotal > 0 Then
   strErrors = 1
Else
   strErrors = 0
End If
 
'Output something meaningful if we do or dont find failures
If strErrors = 1 Then
   txtBody = "DETECTED A FAILURE " & iTotal & " files " &_
   "appear to have failed in this job. See attached file."
Else
   txtBody = "ROBOCOPY SUCCESSFUL no errors seem to be " &_
   "present in this robocopy log. See attached file."
End If   
 
'-- Email Send --
Const cdoSendUsingMethod = _
"http://schemas.microsoft.com/cdo/configuration/sendusing", _
cdoSendUsingPort = 2, _
cdoSMTPServer = _
"http://schemas.microsoft.com/cdo/configuration/smtpserver"
'// Create CDO connections.
Dim iMsg, iConf, Flds
Set iMsg = CreateObject("CDO.Message")
Set iConf = CreateObject("CDO.Configuration")
Set Flds = iConf.Fields
With Flds
.Item(cdoSendUsingMethod) = cdoSendUsingPort
.Item(cdoSMTPServer) = txtSMTPServer
.Update
End With
'// Set the message properties.
With iMsg
Set .Configuration = iConf
.To = txtTo
.From = txtFrom
.Subject = txtSubject
.TextBody = txtBody
End With
if txtlog <> "" then iMsg.AddAttachment txtLog
'// Send the message.
iMsg.Send ' send the message.
 
'-- 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

Open in new window

0
Comment
Question by:mikeewalton
1 Comment
 
LVL 10

Accepted Solution

by:
TakedaT earned 2000 total points
ID: 24238368
This should do it.
Dim txtSMTPServer, txtTo, txtFrom, txtSubject, txtBody
Dim txtLog, strValue, iTotal, iPos, strText
 
txtSMTPServer = "server.domain.local"	          'SMTP server
txtTo = "user@domain.com"		                  'To Address
txtFrom = "administrator@domain.com"                     'From Address
txtLog = "c:\robo.log" 'path to the
                                       'robocopy log file
 
'Get the file contents
FileContents = GetFile(txtLog)
 
'-- Cycle thru the log file to find errors --
 
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
 
'-- --
 
If iTotal > 0 Then
   strErrors = 1
Else
   strErrors = 0
End If
 
'Output something meaningful if we do or dont find failures
If strErrors = 1 Then
   txtBody = "DETECTED A FAILURE " & iTotal & " files " &_
   "appear to have failed in this job. See attached file."
Else
   txtBody = "ROBOCOPY SUCCESSFUL no errors seem to be " &_
   "present in this robocopy log. See attached file."
End If   
 
'-- Email Send --
Const cdoSendUsingMethod = _
"http://schemas.microsoft.com/cdo/configuration/sendusing", _
cdoSendUsingPort = 2, _
cdoSMTPServer = _
"http://schemas.microsoft.com/cdo/configuration/smtpserver"
'// Create CDO connections.
Dim iMsg, iConf, Flds
Set iMsg = CreateObject("CDO.Message")
Set iConf = CreateObject("CDO.Configuration")
Set Flds = iConf.Fields
With Flds
.Item(cdoSendUsingMethod) = cdoSendUsingPort
.Item(cdoSMTPServer) = txtSMTPServer
.Update
End With
'// Set the message properties.
With iMsg
Set .Configuration = iConf
.To = txtTo
.From = txtFrom
If strErrors > 0 then
	.Subject = "Data Copy Unsuccessful"
Else
	.Subject = "Data Copy Successful"
End If
.TextBody = txtBody
End With
If txtLog <> "" and strErrors > 0 then iMsg.AddAttachment txtLog
'// Send the message.
iMsg.Send ' send the message.
 
'-- 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

Open in new window

0

Featured Post

NFR key for Veeam Backup for Microsoft Office 365

Veeam is happy to provide a free NFR license (for 1 year, up to 10 users). This license allows for the non‑production use of Veeam Backup for Microsoft Office 365 in your home lab without any feature limitations.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

When you see single cell contains number and text, and you have to get any date out of it seems like cracking our heads.
Windows Server 2003 introduced persistent Volume Shadow Copies and made 2003 a must-do upgrade.  Since then, it's been a must-implement feature for all servers doing any kind of file sharing.
This tutorial will walk an individual through the process of configuring basic necessities in order to use the 2010 version of Data Protection Manager. These include storage, agents, and protection jobs. Launch Data Protection Manager from the deskt…
This tutorial will show how to configure a new Backup Exec 2012 server and move an existing database to that server with the use of the BEUtility. Install Backup Exec 2012 on the new server and apply all of the latest hotfixes and service packs. The…
Suggested Courses

834 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question