title: Robocopy script /w Powershell + Mail report for failure
body: <p>I'm studying for Network\System Admin certification (MCSE is in it) and among other things we started to delve into powershell scripting.</p>
  
  <p>At any rate my instructor gave me a challenge to do as follows:
  I need to create a PS script which will run Robocopy tool in order to mirror a folder from point A to Point B and that script should run as a schedule task (/w Task Scheduler).
  If robocopy have encountered any errors the script should send a mail via the Company's exchange mail server to the Admin in charge of the process.</p>
  
  <p>I'm not Scripting savvy of any sort so I had to sit and learn the syntax, command etc before I could start working.</p>
  
  <p>Here is what I got so far:</p>
  
  <pre><code># PowerShell Robocopy Script V.1 | Liron Ben-David 
  
  # Variables
  $Source ="C:\Source\"
  $Destination = "C:\Destination\"
  $ExitCode = "Null"
  
  #Copying Process
  Robocopy $Source $destination *.* /mir /r:100 /w:10
  
  # Return Code Section
  if ($LASTEXITCODE -eq 0) {$ExitCode = "Succeeded, Code: $LASTEXITCODE" }
  elseif (($LASTEXITCODE -gt 1) -and ($LASTEXITCODE -lt 17)) {$ExitCode = "Failure, Code: $LASTEXITCODE" }
  else {$ExitCode = "No Return Code"}
  
  Write-Host $ExitCode
  
  ## Failure Report
  if ($ExitCode -gt 1) {Send-MailMessage -to "Admin (Admin@Domain.com)" -From "Machine-Robocopy1 (M1RC@domain.com)" -Subject $ExitCode -SmtpServer mail.domain.com}
  else {exit}
  </code></pre>
  
  <p>basically if there is any kind of error in the process the script supposed to send an email with the script error code as the "Subject" of the message.</p>
  
  <p>First of all:
  Does this script contain any embarassing mistakes I did and which I should avoid in the future?</p>
  
  <p>Secondarily:
  I never used the CLI to send email messages, using the Send-Mail CMDLET I should have used a credentials switch but according to what I've read\watched it seems it will pop up a window for input.
  Considering I'm setting this script for a Domain Environment wouldn't the SSO feature will kick in when I try to send the message?</p>
  
  <p>EDIT:
  Also noticed I mixed the error codes so I need to re-calibrate the error code lines.</p>
  
  <p>EDIT 2: Version 2 bsaed on your help,</p>
  
  <pre><code># PowerShell Robocopy Script V.2 | Liron Ben-David 
  
  # Variables
  $Source = "C:\Source\"
  $Destination = "C:\Destination\"
  $ExitCode = "Null"
  $ReturnCodeValue = "Void"
  $Log = "C:\RoboLogs\RobocopyLog.txt"
  
  #Copying Process
  Robocopy $Source $destination *.* /mir /r:100 /w:10 /UniLog:C:\RoboLogs\RobocopyLog.txt /v
  
  # Return Code Section
  if (($LASTEXITCODE -eq 0) -or ($LASTEXITCODE -eq 1) -or ($LASTEXITCODE -eq 3) -or ($LASTEXITCODE -eq 5) -or ($LASTEXITCODE -eq 6) -or ($LASTEXITCODE -eq 7)) {$ExitCode = "Succeeded, Code: $LASTEXITCODE" }
  elseif (($LASTEXITCODE -eq 2) -or ($LASTEXITCODE -eq 4)) {$ExitCode = "Error, Code: $LASTEXITCODE" }
  elseif ($LASTEXITCODE -gt 7) {$ExitCode = "Failure, Code: $LASTEXITCODE" }
  else {$ExitCode = "No Return Code"}
  
  $ReturnCodeValue = $LASTEXITCODE
  
  # Debug Section
  #Write-Host "Did the script completed succesfuly:" $?
  #Write-Host "Script Exit Code:" $ExitCode
  #Start-Sleep -s 4
  
  
  ## Failure Report
  if (($ReturnCodeValue -gt 7) -or ($ReturnCodeValue -eq 2) -or ($ReturnCodeValue -eq 4)) {Send-MailMessage -to "Admin (Admin@Domain.com)" -From "Machine-Robocopy1 (M1RC@domain.com)" -Subject $ExitCode -SmtpServer mail.domain.com –Attachments $Log -UseSsl}
  else {exit}
  </code></pre>