Robocopy Check E-mail
Robocopy is a great command for backing up data between drives or across the network on a scheduled basis.
Generally I create a batch file with commands like the following to mirror data over night when no one (ok, nearly no one) is accessing the content.
E: CD\ robocopy "E:\SharedTeacher$" "\\AAA-MS-XX\Backup$\Teachers$" /MIR /R:1 /W:1 /NP > "E:\Logs\Teachers to MSXX.log" robocopy "E:\SharedAdmin$" "\\AAA-MS-XX\Backup$\Admin$" /MIR /R:1 /W:1 /NP > "E:\Logs\Admin to MSXX.log"
As you can see, I output the results to a file, so I can review. Problem is, the more Robocopy commands that are used, across different servers, the more files I have to check through where most of the time the process would have been successful... Wouldn't it be much simpler if a brief summary was e-mailed to us...
If you have an SMTP relay setup, excellent you're most of the way there already, if not, this is how I set one up on Windows 2012 R2 (and can be done on previous versions back to at least 2003, though instructions may vary).
Install the Server feature 'SMTP Relay' and accept all the dependencies.
Open Internet Information Services (IIS) 6.0 Manager, right click on [sMTP Virtual Server] and select Properties.
Click on the Advanced tab.
Ensure you have a port 25 setup for incoming mail.
Note: You may need to add a Windows Firewall rule to allow traffic to the server on that port.
On the Access tab, click Authentication.
Tick Anonymous access and OK.
Click the Connection button and list the Server IP Address you wish to send email via this server. Click OK.
Click the Relay button and list the Server IP Address you wish to send email via this server. Click OK.
Make sure you have sensible limits on the Messages tab.
Click the Delivery tab, followed by the Outbound Security button.
Select Basic authentication and type an e-mail address you wish the information to be sent from, as well as the password. Click OK.
In Advanced, type the FQDN of your local server. For smart host, type your smtp address. In this example we are sending out to our Office 365 account, so the smart host is: SMTP.office365.com
Now that you have an SMTP relay up and running, we will want to create a script to scan the robocopy log files and email if there is an issue... here's one I made earlier...
$Computername = (Get-Childitem env:computername).Value $Log = "" $DetectedFailure = 0 # Mail relay settings (The local server configured to send an e-mail). $SMTPSERVER = "ms00.internal.school.county.sch.uk" $MAILFROM = "[email protected]" $MAILTO = "[email protected]" $MESSAGE = "" function funcResultCheck () { $DirError = 9 $FileError = 9 $ByteError = 9 $Data = Get-Content($Log) ForEach ($Line in $Data) { # Dirs - Manual Error Testing Comment # 0 1Total 2Copied 3Skipped 4Mismatch 5FAILED 6Extras #$Line = " Dirs : 1 0 1 0 0 0" If ($Line.Contains("Dirs :")) { $Line = $Line -replace "Dirs :","" $Line = $Line -replace " ",";" Do { $Line = $Line -replace ";;",";" }While ($Line.Contains(";;")) $DirValues = $Line -split ";" $DirError = 9 If ($DirValues.Count -eq 7) { $DirError = [int]$DirValues[5] If (!($?)) {$DirError = 9} } } # Files - Manual Error Testing Comment # 0 1Total 2Copied 3Skipped 4Mismatch 5FAILED 6Extras #$Line = " Files : 1 0 0 0 1 0" If ($Line.Contains("Files :")) { $Line = $Line -replace "Files :","" $Line = $Line -replace " ",";" Do { $Line = $Line -replace ";;",";" }While ($Line.Contains(";;")) $FileValues = $Line -split ";" $FileError = 9 If ($FileValues.Count -eq 7) { $FileError = [int]$FileValues[5] If (!($?)) {$FileError = 9} } } # Bytes - Manual Error Testing Comment # 0 1Total 2Copied 3Skipped 4Mismatch 5FAILED 6Extras #$Line = " Bytes : 15.0 k 0 0 0 15.0 k 0" If ($Line.Contains("Bytes :")) { $Line = $Line -replace "Bytes :","" $Line = $Line -replace "[a-z]","" $Line = $Line.TrimEnd() $Line = $Line -replace " ",";" Do { $Line = $Line -replace ";;",";" }While ($Line.Contains(";;")) $ByteValues = $Line -split ";" $ByteError = 9 If ($ByteValues.Count -eq 7) { $ByteError = [int]$ByteValues[5] If (!($?)) {$ByteError = 9} } } } $TotalError = $DirError + $FileError + $ByteError If ($TotalError -eq 0) { Write-Host "Successful! :D" $MESSAGE = $MESSAGE + $Log + " - Successful !`n" } Else { Write-Host "Unsuccessful! D:" $MESSAGE = $MESSAGE + $Log + " - FAILED! D:!!!`n" $DetectedFailure = 1 } } $Log = "E:\Logs\Teachers to MSXX.log" . funcResultCheck $Log = "E:\Logs\Admin to MSXX.log" . funcResultCheck #Set subject message based on value of text in ResultsEmail if($DetectedFailure -eq 1){$MAILSUBJECT = "$Computername Backup FAILURE"}else {$MAILSUBJECT = "$Computername Backup Success"} $MESSAGE = "This is a backup message from " + $Computername + ".`n`n$Computername Backup has been Processed. Results as follows:`n`n" + $MESSAGE + "`nServer Automated Message" Send-MailMessage -To "$MAILTO" -From "$MAILFROM" -Subject "$MAILSUBJECT" -Body "$MESSAGE" -SmtpServer "$SMTPSERVER"
Remember to replace the value set for:
- $SMTPSERVER with your FQDN or IP Address of your SMTP relay server.
- $MAILFROM with the email account you will be sending the message from (same as the one used on the relay server).
$MAILTO with the e-mail address of who you want the report sent to.
$Log with the name of the log file you wish to check.
After specifing the log file to check, always make sure there is a new line with
. funcResultCheck
This will tell the script to check the file to see if it failed to copy any file.
You can run the script manually to test the result.
When you are happy that everything has been setup, you can go back to your batch file running robocopy and at the end type this line (modifying for path an script name of course).
powershell.exe -noprofile -executionpolicy bypass -file "E:\Tasks\RoboCheck.ps1"












0 Comments
Recommended Comments
There are no comments to display.
Create an account or sign in to comment
You need to be a member in order to leave a comment
Create an account
Sign up for a new account in our community. It's easy!
Register a new accountSign in
Already have an account? Sign in here.
Sign In Now