Only want it to email when it fails

This topic contains 2 replies, has 3 voices, and was last updated by Profile photo of Dan Potter Dan Potter 4 months, 1 week ago.

Viewing 3 posts - 1 through 3 (of 3 total)
  • Author
    Posts
  • #39310
    Profile photo of Vann Ten
    Vann Ten
    Participant

    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 = "[email protected]"
    $EmailTo = "[email protected]"
    $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' [email protected]' /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')
    $Message.Attachments.Add($Attachment)
    $SMTPClient = New-Object Net.Mail.SmtpClient($SmtpServer, $SMTPPort)
    $SMTPClient.Send($Message)

    #39314
    Profile photo of Graham Beer
    Graham Beer
    Participant

    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.

    #39317
    Profile photo of Dan Potter
    Dan Potter
    Participant

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

Viewing 3 posts - 1 through 3 (of 3 total)

You must be logged in to reply to this topic.