Powershell: Directory Sync Script with e-mail notification (powered by Robocopy)

# Hurdles:  Robocopy ERROR 5 (0×00000005) Changing File Attributes: Access is denied

    # For the ERROR 5 (0×00000005), may be due to multiple reasons, include the below syntaxes to troubleshoot the error:
    # I found, you need to use the /FFT flag to assume FAT file times (2 second granularity). Although the target folder is NTFS/FAT, these file systems also implement file times with 2 second granularity.
    # I found, You need to turn off the attribute copying.  Robocopy uses the /COPY:DAT by default, which means to copy data, attributes and timestamp. You should turn off attribute copying by explicit setting /COPY:DT
    # This syntax should resolve the issue with Error 5 of Robocopy and the Buffalo Terastation.

# Potential:  Mature this script to NOT email anyone if everything is ok (via $SendEmail switch)
# Potential:  Mature this script to NOT email administrator unless there is a systematic problem (via $IncludeAdmin switch)
# Potential:  Is this feasible?:  Mature this script to use File Checksum Integrity Verifier (FCIV) to calculate MD5 or SHA1 hash algorithms 

$SendEmail = "True"
$SourceFolder = "\\1.1.1.1\source"
$DestinationFolder = "\\2.2.2.2\destination"
$Logfile = $PWD.path + "\Robocopy-" + (Get-Date -uFormat "%m%d%Y%H%M%S").tostring() + ".log"

$EmailFrom = "Sync_Admin@domain.com"
# Add multiple email recipients using a comma seperated list
$EmailTo = ("user@domain.com","user2@domain.com")
$EmailBody = "Robocopy - See attached log file for details"
$EmailSubject = "Directory Sync Script with e-mail notification (powered by Robocopy)"
$SMTPServer = "email.domain.com"
$SMTPPort = "587"
#$SMTPPort = "25"

$JobFile = ($PWD.path + "Exclusions.rcj")
if (Test-Path ($JobFile)){Remove-Item $JobFile}
Add-Content $JobFile "/XF"
Add-Content $JobFile "*.ini"
Add-Content $JobFile "thumbs.db"
Add-Content $JobFile "/XD"

# Load Robocopy Arguments
$Options = @("/R:10","/W:1","/SEC","/MIR","/Z","/B","/COPY:DT","/FFT","/MT:16","/MOT:60","/LOG:$Logfile","/TEE","/JOB:Exclusions.rcj")
$cmdArgs = @($SourceFolder,$DestinationFolder,$Options)

# Example command:
# robocopy "\\1.1.1.1\source" "\\2.2.2.2\destination" /FFT /TEE /S /E /COPY:DT /PURGE /MIR /B /MOT:60 /MT:16 /R:10 /W:1 /LOG:Logfile /TEE /JOB:Jobfile

# Execute Sync of drive with Robocopy
robocopy @cmdArgs

Switch ($LASTEXITCODE) {
16 {
$exit_code = "16"
$exit_reason = "***FATAL ERROR***"
#$IncludeAdmin = $False
}
8 {
$exit_code = "8"
$exit_reason = "**FAILED COPIES**"
#$IncludeAdmin = $False
}
4 {
$exit_code = "4"
$exit_reason = "*MISMATCHES*"
#$IncludeAdmin = $False
}
2 {
$exit_code = "2"
$exit_reason = "EXTRA FILES"
#$IncludeAdmin = $False
}
1 {
$exit_code = "1"
$exit_reason = "Copy Successful"
#$IncludeAdmin = $False
}
0 {
$exit_code = "0"
$exit_reason = "No Change"
#$SendEmail = $False
$IncludeAdmin = $False
}
}

$EmailSubject += " : " + $exit_reason + "|| Error Code: " + $exit_code

# Send E-mail message with log file attachment
foreach ($EmailAddress in $EmailTo)
{ 
$SMTPClient = New-Object Net.Mail.SmtpClient($SmtpServer, $SMTPPort)
$SMTPClient.EnableSsl = $true
$SMTPClient.Credentials = New-Object System.Net.NetworkCredential("user", "pword")
$message = New-Object Net.Mail.MailMessage($EmailFrom, $EmailAddress, $EmailSubject, $EmailBody)
# $message.IsBodyHtml = $true;
$Attachment = New-Object Net.Mail.Attachment($Logfile, 'text/plain')
$Message.Attachments.Add($Attachment)
$SMTPClient.Send($message)
}

if (Test-Path ($JobFile)){Remove-Item $JobFile}
if (Test-Path ($Logfile)){Remove-Item $Logfile}

No comments:

Post a Comment