Only want it to email when it fails

Hi All,

I have the following script that runs and copies backup files from one location to another location.

Once it’s done it will email the log file across. At present it will always email after it’s completed.

What I would like to do is have it so it only emails when the copying fails.

See below for the script:

Change these values

$SourceFolder = “\NAS01\Backup”
$DestinationFolder = “\GP01\USBX”
$Logfile = “\GP01\USBX\Backuplog.log”
$EmailFrom = “
$EmailTo = “
$EmailBody = “Robocopy completed successfully for Daily. See attached log file for details”
$EmailSubject = “NAS2USB Daily Robocopy Summary”
$SMTPServer = “mailserver”
$SMTPPort = “25”

Copy Folder with Robocopy

Robocopy $SourceFolder $DestinationFolder /COPY:DAT /XA:H /XD ‘$Recycle.bin’ ‘Boot Recycler’ ‘System Volume Information’ ‘@Recycle’ /E /V /PURGE /W:10 /R:3 /LOG:"\GP01\USBX\Backuplog.log" /tee /np

Send E-mail message with log file attachment

$Message = New-Object Net.Mail.MailMessage($EmailFrom, $EmailTo, $EmailSubject, $EmailBody)
$Attachment = New-Object Net.Mail.Attachment($Logfile, ‘text/plain’)
$SMTPClient = New-Object Net.Mail.SmtpClient($SmtpServer, $SMTPPort)

Does the log file state a success message and date? You could use this in a IF statement to say if success, do nothing, if not success send e-mail.

if((Robocopy c:\doesntexist h:) -match ‘error’){‘something wrong’}