Exchange 2007 | windows backup utility & email reporting vb script

For regular backups of our Exchange 2007 email database server and to clear the all important log files we use the standard Backup Utility which is bundled with Windows 2003 Server. It does its job well except, unless you log on to the machine, you can’t tell if a backup failed or if it was sucessful – as there is no native reporting.

The options you have are either to use a commercial third party backup application which includes reporting or create yourself a scheduled VB script to email you the backup logs. The later is exactly what I did.

Below is my VB script that is run every morning from the exchange server. It sends an email to the IT distribution group with the latest backup log file in the body and as a text file attachment (the reason for the text file is a convenient way of storing as each text file is timestamped in the script).

Option Explicit

‘ ***********************************************************
‘ * Red Chilli exchange backup email reporting
‘ * by Simon Page
‘ ***********************************************************

‘ Dim variables
Dim oFSO, oWshNetwork, oCDO, oFolder, oFiles, oFile
Dim filesys, filesys2, objFile, objFile2, objTextStream, copyBody
Dim strFileName, strComputerName

‘ Create Objects
Set oFSO = CreateObject(“Scripting.FileSystemObject”)
Set oWshNetwork = WScript.CreateObject(“WScript.Network”)
set filesys = CreateObject (“Scripting.FileSystemObject”)
set filesys2 = CreateObject (“Scripting.FileSystemObject”)

‘ Copy file
set objFile = filesys.GetFile(“c:\WINDOWS\Tasks\SchedLgu.Txt”)
objFile.Copy(“c:\Logs\SchedLgU-” & Replace(CStr(Date()), “/”, “-“) & “.Txt”)

‘ Open copied file for reading
set objFile2 = filesys2.GetFile(“c:\Logs\SchedLgU-” & Replace(CStr(Date()), “/”, “-“) & “.Txt”)
set objTextStream = objFile2.OpenAsTextStream(1,True)

copyBody = objTextStream.ReadAll

‘ Set constants
Const BACKUP_LOG_PATH = “C:\Logs”
Const EMAIL_RECIPIENT = “email@domain.com”

‘ Set variable defaults
strFileName = “SchedLgU-” & Replace(CStr(Date()), “/”, “-“) & “.Txt”
strComputerName = oWshNetwork.ComputerName

‘If strFileName <> “” Then

EmailLog(strFileName)

‘End If

Sub EmailLog(logFilePath)

Set oCDO = CreateObject(“CDO.Message”)

oCDO.Subject = “EXCHANGE BACKUP FULL LOG – ” & strComputerName & ” – ” & CStr(Date())
oCDO.From = “email@domain.com”
oCDO.To = EMAIL_RECIPIENT

oCDO.TextBody = copyBody

oCDO.AddAttachment BACKUP_LOG_PATH & “\” & logFilePath

oCDO.Configuration.Fields.Item _
(“http://schemas.microsoft.com/cdo/configuration/smtpauthenticate“) = _
1

oCDO.Configuration.Fields.Item _
(“http://schemas.microsoft.com/cdo/configuration/sendusername“) = _
“USERNAME”

oCDO.Configuration.Fields.Item _
(“http://schemas.microsoft.com/cdo/configuration/sendpassword“) = _
“PASSWORD”

oCDO.Configuration.Fields.Item _
(“http://schemas.microsoft.com/cdo/configuration/sendusing“) = 1

oCDO.Configuration.Fields.Item _
(“http://schemas.microsoft.com/cdo/configuration/smtpserver“) = _
“SMTP SERVER”

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

oCDO.Configuration.Fields.Update

oCDO.Send

set oCDO = Nothing

End Sub

objTextStream.Close
set filesys = Nothing
set filesys2 = Nothing
Set oFSO = Nothing
Set oWshNetwork = Nothing

Just copy and save the above script as “filename.vbs” in an application like notepad and simply schedule this vbs file to run sometime relevant to the backups you are running.

This script can be used on any server or workstation where you have the backup utility running scheduled tasks and you don’t want to log on to the server to check the the backups all complete without issue.