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.
Microsoft Server AppsMicrosoft Applications

Avatar of undefined
Last Comment
Brendan M

8/22/2022 - Mon
ASKER CERTIFIED SOLUTION
Brendan M

Log in or sign up to see answer
Become an EE member today7-DAY FREE TRIAL
Members can start a 7-Day Free trial then enjoy unlimited access to the platform
Sign up - Free for 7 days
or
Learn why we charge membership fees
We get it - no one likes a content blocker. Take one extra minute and find out why we block content.
See how we're fighting big data
Not exactly the question you had in mind?
Sign up for an EE membership and get your own personalized solution. With an EE membership, you can ask unlimited troubleshooting, research, or opinion questions.
ask a question
ASKER
georgopanos

how do I run the script, do i use cscript?
Brendan M

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>
ASKER
georgopanos

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"
This is the best money I have ever spent. I cannot not tell you how many times these folks have saved my bacon. I learn so much from the contributors.
rwheeler23
ASKER
georgopanos

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....
Brendan M

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

ASKER
georgopanos

Thank you for all your help so far......
Get an unlimited membership to EE for less than $4 a week.
Unlimited question asking, solutions, articles and more.
ASKER
georgopanos

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!
Brendan M

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