Link to home
Start Free TrialLog in
Avatar of georgopanos
georgopanosFlag for United States of America

asked on

Email log files from backup windows 2003

I have a windows 2003 server that is using the windows backup utility to backup to an external drive. It does not have the option to email log files. Is there a way to easily grab the newest file from the DIR that contains the files and email them nightly using a script? I do not want to use another piece of software I would prefer a simple script.
ASKER CERTIFIED SOLUTION
Avatar of Brendan M
Brendan M
Flag of Australia image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of georgopanos

ASKER

how do I run the script, do i use cscript?
just make sure you have edited the path to the logs, smtp server and the email address,

then yes use cscript, or create a task that will run after the backup finishes.

eg: cscript <filename.vbs>
So the way it is setup where it asks for a filename put nothing and it will find the newest file in the DIR? Can I use  "*.log"
I am sorry to be a pain but is there a way to merge the file into the body of the email?

The script sends with no problems....
uncomment the
'oCDO.AddAttachment BACKUP_LOG_PATH & "\" & logFilePath
to also attach the file.

this should work
Option Explicit

' ***************************************************************
' * Email NT Backup log file
' * © Andrew Taylor 2008 www.andrewtaylor.me.uk
' * Free to use providing this acknowledgement remains in place
' ***************************************************************

' Dim variables
Dim oFSO, oWshNetwork, oCDO, oFolder, oFiles, oFile, objFile
dim strFileName, strComputerName, strContents

' Create Objects
Set oFSO = CreateObject("Scripting.FileSystemObject")
Set oWshNetwork = WScript.CreateObject("WScript.Network")


' Set constants
Const BACKUP_LOG_PATH = "C:\Documents and Settings\Administrator\Local Settings\Application Data\Microsoft\Windows NT\NTBackup\data"
Const EMAIL_RECIPIENT = "you@yourdomain.com"

' Set variable defaults
strFileName = ""
strComputerName = oWshNetwork.ComputerName

' Start script
Set oFolder = oFSO.GetFolder(BACKUP_LOG_PATH)

Set oFiles = oFolder.Files

For Each oFile in oFiles
	If oFSO.GetExtensionName(oFile.Name) = "log" Then
		If DateValue(oFile.DateLastModified) = Date() Then
			strFileName = oFile.Name
			Exit For
		End If
	End If
Next

If strFileName <> "" Then

	EmailLog(strFileName)

Else

	EmailError

End If

Sub EmailLog(logFilePath)

	Set oCDO = CreateObject("CDO.Message")

	oCDO.Subject = "Backup Log - " & strComputerName
	oCDO.From = strComputerName & "@yourdomain.com"
	oCDO.To = EMAIL_RECIPIENT
	
	Set objFile = objFSO.OpenTextFile(BACKUP_LOG_PATH & "\" & logFilePath, 1)
	strContents = objFile.ReadAll
	objFile.Close

	oCDO.TextBody = "Backup log attached for " & strComputerName & vbCrLf & strContents

	'oCDO.AddAttachment BACKUP_LOG_PATH & "\" & logFilePath

	oCDO.Configuration.Fields.Item("http://schemas.microsoft.com/cdo/configuration/sendusing") = 2

	oCDO.Configuration.Fields.Item("http://schemas.microsoft.com/cdo/configuration/smtpserver") ="mail.yourdomain.com"

	oCDO.Configuration.Fields.Item("http://schemas.microsoft.com/cdo/configuration/smtpserverport") = 25 

	oCDO.Configuration.Fields.Update

	oCDO.Send

	set oCDO = Nothing


End Sub

Sub EmailError

	Set oCDO = CreateObject("CDO.Message")

	oCDO.Subject = "Backup Log Missing - " & strComputerName
	oCDO.From = strComputerName & "@yourdomain.com"
	oCDO.To = EMAIL_RECIPIENT

	oCDO.TextBody = "No backup log found on " & strComputerName & " for today"

	oCDO.Configuration.Fields.Item("http://schemas.microsoft.com/cdo/configuration/sendusing") = 2

	oCDO.Configuration.Fields.Item("http://schemas.microsoft.com/cdo/configuration/smtpserver") ="mail.yourdomain.com"

	oCDO.Configuration.Fields.Item("http://schemas.microsoft.com/cdo/configuration/smtpserverport") = 25 

	oCDO.Configuration.Fields.Update

	oCDO.Send

	set oCDO = Nothing


End Sub

set oFSO = Nothing
Set oWshNetwork = Nothing

Open in new window

Thank you for all your help so far......
small issue it is not picking up the newest file, might there be something in the code that I need to modify? I looked through the code and cannot find anything!
it will email a log file that was created on the day it was run

eg: backup finishes at 2am on the 17/11/2010
then the script needs to be run on 17/11/2010 after the back is finished

have a look at implementing this function if you want the newest file
found on: http://www.ureader.com/msg/1677440.aspx

Function GetNewestFile(ByVal sPath)

   sNewestFile = Null   ' init value

   Set oFSO = CreateObject("Scripting.FileSystemObject")
   Set oFolder = oFSO.GetFolder(sPath)
   Set oFiles = oFolder.Files

   ' enumerate the files in the folder, finding the newest file
   For Each oFile In oFiles
     On Error Resume Next
     If IsNull(sNewestFile) Then
       sNewestFile = oFile.Path
       dPrevDate = oFile.DateLastModified
     Elseif dPrevDate < oFile.DateLastModified Then
       sNewestFile = oFile.Path
     End If
     On Error Goto 0
   Next

   If IsNull(sNewestFile) Then sNewestFile = ""

   GetNewestFile = sNewestFile

End Function