I Wanted a powershell script would sync a network directory of a few files and that could be scheduled / would notify me of any failures (or of successful sync).  If the script runs and there are no changes (and hence no copying) then it doesn't send an email but in all other exitcodes it notifies of the status.  Everything that is needing to be changed (other than the <BR>) is in the <>.

 

Pretty simplistic but didn't know if anyone would be interested in the exitcodes of robocopy in a script.

 

PowerShell
Edit|Remove
# Robocopy Sync Folder and email errorcode by Jonathan Tarro 
# Date: 2018-03-19  
# Version: 1.0  
 
robocopy "<source folder>" "<destination folder>" /MIR 
 
$maildest = @('<recipient1@email.org>''<recipient1@email.org>''<recipient1@email.org>''<recipient1@email.org>'$mailsrc = "<original@email.org>" 
$smtpsrv = "<SMTP Servername>" 
  
 if ($lastexitcode -eq 0) 
 { 
    $errmsg = "No errors occurred, and no copying was done. The source and destination directory trees are completely synchronized." 
    $rcstatus = "Exitcode: $lastexitcode - $errmsg" 
    write-host $rcstatus 
 } 
 elseif ($lastexitcode -eq 1) 
 { 
    $errmsg = "Files were copied successfully (that is, new files have arrived)." 
    $rcstatus = "Exitcode: $lastexitcode - $errmsg" 
    $msgbody = "Servername: $env:computername<br>$rcstatus" 
    write-host $rcstatus 
    Send-MailMessage -To $maildest -From $mailsrc -SmtpServer $smtpsrv -Subject "File Sync Status: $env:computername" -BodyAsHtml -body $msgbody 
 } 
 elseif ($lastexitcode -eq 2) 
 
...
 

 

...