Link to home
Create AccountLog in
VB Script

VB Script

--

Questions

--

Followers

Top Experts

Avatar of detox1978
detox1978🇬🇧

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

Zero AI Policy

We believe in human intelligence. Our moderation policy strictly prohibits the use of LLM content in our Q&A threads.


Avatar of kulboykulboy

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 = “yourgmailadress@gmail.com

$EmailTo = “destination@somedomain.com”

$Subject = “The subject of your email”

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

$SMTPServer = “smtp.gmail.com”

$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.


source: http://www.howtogeek.com/120011/stupid-geek-tricks-how-to-send-email-from-the-command-line-in-windows-without-extra-software/

ASKER CERTIFIED SOLUTION
Avatar of oBdAoBdA

Link to home
membership
Log in or create a free account to see answer.
Signing up is free and takes 30 seconds. No credit card required.
Create Account

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.

Reward 1Reward 2Reward 3Reward 4Reward 5Reward 6

EARN REWARDS FOR ASKING, ANSWERING, AND MORE.

Earn free swag for participating on the platform.


Avatar of detox1978detox1978🇬🇧

ASKER

Sorry for the slow response.

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

User generated image

OK, that means you're still on Powershell version 2.
Are you planning to update to v3 (http://www.microsoft.com/en-us/download/details.aspx?id=34595) or v4 (http://www.microsoft.com/en-us/download/details.aspx?id=40855) 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.

Avatar of detox1978detox1978🇬🇧

ASKER

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.

Free T-shirt

Get a FREE t-shirt when you ask your first question.

We believe in human intelligence. Our moderation policy strictly prohibits the use of LLM content in our Q&A threads.


Avatar of detox1978detox1978🇬🇧

ASKER

Script worked a charm.  many thanks.

EE justifying the fee yet again :-)

Hi All,
would the above script to send email will work for my Robocopy script below.

rem THE SCRIPT AND THE LOG FILES FOLDERS MUST BE LOCATED ON A LOCAL DRIVE

rem The system date must be set to the mm-dd-yyyy format in the Regional Options for this
rem script to work. Also it's better to use  dash (-) instead of slash (/) as the separator.

set MyDate=%DATE:~0,2%-%DATE:~3,2%-%DATE:~6,4%
set MyTime=%Time:~0,2%-%Time:~3,2%-%Time:~6,2%
set LogFile="F:\Backupfolder\folder1_folder2_%MyDate%_%MyTime%.log"

RoboCopy.exe \\source server\folder1\folder2 \\destination server\folder1\folder2 *.* /S /E /R:0 /XX /FP /NP >>%LogFile%

Any suggestion or assistance  will be a big help.
Many Thanks
VB Script

VB Script

--

Questions

--

Followers

Top Experts

VBScript (Visual Basic Scripting Edition) is an interpreted scripting language developed by Microsoft that is modeled on Visual Basic, but with some important differences. VBScript is commonly used for automating administrative and other tasks in Windows operating systems (by means of the Windows Script Host) and for server-side scripting in ASP web applications. It is also used for client-side scripting in Internet Explorer, specifically in intranet web applications.