Robocopy script with summary email

Hi All,

One of our tape drives in a remote site has died, so I need to copy their data to our head office every night.

Here is the snippet I used to get the data to our central server.

robocopy "C:\department" "\\backup\etl\Dept" /COPYALL /E
robocopy "C:\Users$" "\\backup\etl\Users" /COPYALL /E

Open in new window

Could someone help me with the summary email?

Many thanks
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

It’s pretty easy to send an e-mail from PowerShell, all you need to do is copy the template provided and change some of the details.

$EmailFrom = “

$EmailTo = “”

$Subject = “The subject of your email”

$Body = “What do you want your email to say”

$SMTPServer = “”

$SMTPClient = New-Object Net.Mail.SmtpClient($SmtpServer, 587)

$SMTPClient.EnableSsl = $true

$SMTPClient.Credentials = New-Object System.Net.NetworkCredential(“usr”, “pass”);

$SMTPClient.Send($EmailFrom, $EmailTo, $Subject, $Body)

You will need to change the following:

$EmailFrom = Your GMail address.
$EmailTo = The recipient’s email address.
$Subject = What you want the subject of the mail to say.
$Body = What you want the main part of the mail to say.
usr = You will need to replace this with your GMail username.
pass = You will need to replace this with your GMail password.

Muhammad BurhanManager I.T.Commented:
Here's a Powershell script. I changed the robocopy options to skip over errors (otherwise a single open file will bring the script to a grinding halt, because robocopy will by default retry 1 million times and wait 30 seconds between retries.
It sends a summary in the body and the log file as attachment.
$RobocopyLog = "C:\Temp\robocopy.log"

$smtpServer = ""
$smtpFrom = "" 
$smtpTo = ""
$smtpSubject = "Backup results"
$smtpHtml = $True

$Copy = [ordered]@{
	"C:\department" = "\\backup\etl\Dept"
	"C:\Users$" = "\\backup\etl\Users"

If (Test-Path -Path $RobocopyLog) {
	Remove-Item -Path $RobocopyLog -Force
$smtpBody = ""
$TaskCount = 0
ForEach ($Source In $Copy.Keys) {
	$TaskCount += 1
	& robocopy.exe $Source $Copy[$Source] /copyall /E /r:0 /np /nfl /ndl /tee /Log+:$RobocopyLog
	If ($LASTEXITCODE -lt 4) {
		$TaskStatus = "OK"
	} Else {
		$TaskStatus = "ERROR"
	$smtpBody += "Copy task $($TaskCount): '$($Source)' --> '$($Copy[$Source])'`r`n"
	$smtpBody += "$($TaskStatus): errorlevel $($LASTEXITCODE)`r`n"
	$smtpBody += "`r`n"

If ($smtpHtml) {
	$HTMLHeader = "<HTML><HEAD><TITLE>$($smtpSubject)</TITLE></HEAD><BODY><FONT FACE=`"Courier New`">"
	$HTMLTrailer = "</FONT></BODY></HTML>"
	$smtpBody = $HTMLHeader + $smtpBody.Replace(" ", "&nbsp").Replace("`r`n", "<br />") + $HTMLTrailer
Send-MailMessage -From $smtpFrom -To $smtpTo -Subject $smtpSubject -Body $smtpBody -SmtpServer $smtpServer -Attachments $RobocopyLog -BodyAsHtml:$smtpHtml

Open in new window

If you want to schedule it, you can call it like this:
powershell.exe -ExecutionPolicy Bypass -Command "& 'C:\Temp\whatever.ps1'"

Open in new window

Edit: fixed issue with HTML view.

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
HTML5 and CSS3 Fundamentals

Build a website from the ground up by first learning the fundamentals of HTML5 and CSS3, the two popular programming languages used to present content online. HTML deals with fonts, colors, graphics, and hyperlinks, while CSS describes how HTML elements are to be displayed.

Mohammed KhawajaManager - Infrastructure:  Information TechnologyCommented:
First of all, you need to get a log from Robocopy, change your Robocopy command to below:

robocopy "C:\department" "\\backup\etl\Dept" /COPYALL /E /NS /NC /NFL /NDL /NP /Log:c:\logs\logfile.txt
robocopy "C:\Users$" "\\backup\etl\Users" /COPYALL /E /NS /NC /NFL /NDL /NP /Log+:c:\logs\logfile.txt

You could then use the PowerShell script to email the file.

I also note that you have not added any retry attempts counts and interval in the command.  If this is not set in registry then Robocopy will retry items it doesn't have security to or locked files for 1,000,000 times with a wait time of 30 seconds between each retry.  Consider adding /R:0 /W:0 to skip locked and/or file it couldn't access due to permissions.
detox1978Author Commented:
Sorry for the slow response.

oBdA, I get the following error message when I run your script.

PowerShell error
OK, that means you're still on Powershell version 2.
Are you planning to update to v3 ( or v4 ( at some point in the near future anyway (important: check the notes under System Requirements)? Powershell 2 is from 2009, so not really up to date.
Powershell 3 has some nice features ("get-help -ShowWindow", the bug is fixed that ForEach () enters the loop even if the expression is $Null, ordered hash tables, ...) and lots of new modules/cmdlets.
If you can't update at this point, say so, and I'll fix it so that it works with v2.
detox1978Author Commented:
Thanks for the link.  I've installed PowerShell 4.0.  The server needs a reboot before it can be picked up, so I will report back tomorrow.
detox1978Author Commented:
Script worked a charm.  many thanks.

EE justifying the fee yet again :-)
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
VB Script

From novice to tech pro — start learning today.