Email on completed Scheduled Task

Sleestack90
Sleestack90 used Ask the Experts™
on
I would like to know how to have a scheduled task email when it's done, and I would also like to recieve the log file in the email, or a link to it.

What is going to be the best way to accomplish this?
Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
You could create a batch file that ran the task that you wanted, and then at the end add a line to send an email... However you'll need a command line mail utility to do this like...

http://www.beyondlogic.org/solutions/cmdlinemail/cmdlinemail.htm

or

http://www.blat.net/
How about this:
http://forums.techarena.in/server-scripting/972774.htm

Looks like it might be easier to do it this way:
http://www.experts-exchange.com/Web_Development/Web_Languages-Standards/XML/Q_23855362.html

And if you go with powershell, you can use this to send email:
# email notification with log attached
$smtpServer = "smtp.xxxx.edu"
$msg = new-object Net.Mail.MailMessage
$att = new-object Net.Mail.Attachment($logFile)
$smtp = new-object Net.Mail.SmtpClient($smtpServer)

Set-Variable -name sendEmailFrom        -Value "xdsd@xxxx.edu"
Set-Variable -name sendEmailTo          -Value "afsdf@xxxx.edu"  
Set-Variable -name sendEmailToToo          -Value "metoo@xxxx.edu"   # couldn't get 2 emails on same line working

$msg.From = $sendEmailFrom
if (! $testFlag) {  # do not sent to Connie if test
    $msg.To.Add($sendEmailTo)
}
$msg.To.Add($sendEmailToToo)
$msg.Subject = $BuildNum + " build is done."

$msg.Body = "The log file is attached and the build is in: " +  $ImageDestDir
$msg.Attachments.Add($att)

$smtp.Send($msg)
Ben Personick (Previously QCubed)Lead SaaS Infrastructure Engineer

Commented:
If you are open to writing your script inn VBS, or simply calling a small vbs snippet you could do this through telnet.  Then you woudl need no third party tools or downloads.
Starting with Angular 5

Learn the essential features and functions of the popular JavaScript framework for building mobile, desktop and web applications.

Author

Commented:
I haven't decided how I am going to do this as of yet. I am looking for the simplest method to accomplish this being that I don't have alot of scripting exp.
The simplest will be determined by your exact situation and abilities.  Do you know how to write the bat file?  How to setup a schedule task?  How to install and use a command line email client?  Here's another person on here that was looking for the same type thing:

http://www.experts-exchange.com/OS/Microsoft_Operating_Systems/Server/Q_23086895.html

Author

Commented:
Ok, this is the script I am testing tonight:

@echo off

REM Mapping network drives
net use v: /d /y
net use y: /d /y

REM Assigning the network drives to the NAS devices
net use v: \\NAS1\users
net use y: \\NAS3\users


set LogFile1Users="G:\FILE SERVER Backup Logs\NAS1userbackuplog1.log"

set LogFile3Users="G:\FILE SERVER Backup Logs\NAS3userbackuplog1.log"


REM Copies Users Shared Folder to Network Attached Storage device NAS1 and writes the log file on the G: drive
xcopy "d:\Users Shared Folders" V:\ /s/e/c/h/d/y /Log >>"%LogFile1Users%" 2>&1
blat.exe "%LogFile1Users%" -server EMAIL SERVER -f FILE SERVERUserBackup@domain.com -t admin@domain.com -s "FILE SERVER User Backup to NAS1 finished with errorlevel %Errorlevel%"



REM Copies Users Shared Folder to Network Attached Storage device NAS3 and writes the log file on the G: drive
xcopy "d:\Users Shared Folders" Y:\ /s/e/c/h/d/y /Log >>"%LogFile3Users%" 2>&1
blat.exe "%LogFile3Users%" -server EMAIL SERVER -f FILE SERVERUserBackup@domain.com -t admin@domain.com -s "FILE SERVER User Backup to NAS3 finished with errorlevel %Errorlevel%"


REM Backup Complete!






The BLAT installation seems way to simple. Just drop it in the SYSTEM32 directory? Who am I to look an easy solution in the mouth. That is what I asked for, I will keep you posted with the success or failure of this attempt.
Blat is the easiest!  Just remember, your always appending to the log file.  You may want to change the name each time, or remove/rename the file.

Author

Commented:
An example of what you mean please?
In your xcopy command, you have ">>" which appends.  If the first one is just ">" it will start a new file.  So you would change this:
xcopy "d:\Users Shared Folders" V:\ /s/e/c/h/d/y /Log >>"%LogFile1Users%" 2>&1

to:

xcopy "d:\Users Shared Folders" V:\ /s/e/c/h/d/y /Log >"%LogFile1Users%" 2>&1

Or you would move the file to a new name.  

Author

Commented:
gotcha, thanks.
Good call developedtester.

Basically, every time you run this script you will append to the previous log "G:\FILE SERVER Backup Logs\NAS1userbackuplog1.log". If you're emailing the log eventually it will be huge since it's never being cleared or rotated.

Solutions for this would/could be
1) Clear the log
     set LogFile1Users="G:\FILE SERVER Backup Logs\NAS1userbackuplog1.log"
     set LogFile3Users="G:\FILE SERVER Backup Logs\NAS3userbackuplog1.log"
     echo "%date%" > %LogFile1Users%
     echo "%date%" > %LogFile3Users%

2)  Make a new log
     set LogFile1Users="G:\FILE SERVER Backup Logs\NAS1userbackuplog_%date%.log"
     set LogFile3Users="G:\FILE SERVER Backup Logs\NAS3userbackuplog_%date%.log"
Good call yourself.  Good ideas....the "date" line to start the file, and putting the date in the name of the file.  I guess he could use %time%, but you can't have ":" in filenames.  Wait, doesn't dave give "\" which isn't allowed in filenames either.   Ideas?

Author

Commented:
Looks like it has to many parameters when it ran. I am going to do some testing and see what isn't working.

Author

Commented:
This appears to run correctly after I delete the following from my batch file:

The lines:

set LogFile1Users="G:\FILE SERVER Backup Logs\NAS1userbackuplog1.log"

set LogFile3Users="G:\FILE SERVER Backup Logs\NAS3userbackuplog1.log"



And the lines:

/Log >>"%LogFile1Users%" 2>&1

/Log >>"%LogFile3Users%" 2>&1



I don't have any feedback about the email portion of this as of yet because the job is still running.
You need the "set" command for the log names.  You can just delete the "/Log" from the lines and it should work.

Author

Commented:
This is what I have been testing:

@echo off

REM Mapping network drives
net use v: /d /y
net use y: /d /y

REM Assigning the network drives to the NAS devices
net use v: \\NAS1\users
net use y: \\NAS3\users


set LogFile1Users="G:\FILE SERVER Backup Logs\NAS1userbackuplog1.log"

set LogFile3Users="G:\FILE SERVER Backup Logs\NAS3userbackuplog1.log"


REM Copies Users Shared Folder to Network Attached Storage device NAS1 and writes the log file on the G: drive
xcopy "d:\Users Shared Folders" V:\ /s/e/c/h/d/y
blat.exe "%LogFile1Users%" -server EMAIL SERVER -f FILE SERVERUserBackup@domain.com -t admin@domain.com -s "FILE SERVER User Backup to NAS1 finished with errorlevel %Errorlevel%"



REM Copies Users Shared Folder to Network Attached Storage device NAS3 and writes the log file on the G: drive
xcopy "d:\Users Shared Folders" Y:\ /s/e/c/h/d/y
blat.exe "%LogFile3Users%" -server EMAIL SERVER -f FILE SERVERUserBackup@domain.com -t admin@domain.com -s "FILE SERVER User Backup to NAS3 finished with errorlevel %Errorlevel%"


REM Backup Complete!




It's doing the backup, but it's not creating the log files and I am not getting an email. Ideas?
You still need to redirect the xcopy output (std err and std out) to the log:

xcopy "d:\Users Shared Folders" V:\ /s/e/c/h/d/y >>"%LogFile1Users%" 2>&1

Without this, the log will be blank.
Ben Personick (Previously QCubed)Lead SaaS Infrastructure Engineer

Commented:
@SleeStack  you cannot email from batch unless you use a 3rd party utility, since I specialize in batch, I didn't write you anything because I figured you'd rather use something like VBScript which will do the emailing for you.  if you have a 3rd party utility in mind which will perform the mailing action tell me which one and I will take a crack at this.

Author

Commented:
@ocubed: Actually I am giving Blat a shot, that seems simplest really.

@developedtester: I removed the Log File line because the batch would not run with that line in the file. So I am guessing there is something wrong with it, but I am unable to put my finger on it.
Try echoing the %LogFile1Users% to see if that is set right.  And if it's set right, make sure you can access that file/folder from the command line:

echo "test" >> %LogFile1Users%

I don't think you need the quotes, and actually that may be messing things up.

Author

Commented:
removing the ' " ' worked. We now have a log file. I am going to enable that peice of the batch for the rest of the backups. Now we just need to get it mailing correctly.

Author

Commented:
it looks like this is emailing me just fine for one of the logs. I will continue to test to ensure all of them are working and then I will award points if everything is ok.

Author

Commented:
I don't think I would like for the file to contain the log, only the error level.

blat.exe %LogFile1Users% -server EMAIL SERVER -f FILE SERVERUserBackup@domain.com -t admin@domain.com -s "FILE SERVER User Backup to NAS1 finished with errorlevel %Errorlevel%"

I tired removing the %LogFile1Users%, but it didn't end up sending the email with just the %Errorlevel%

Any ideas?
According to the syntax @ http://www.blat.net/syntax/syntax.html

"if your message body is on the command line, use a hyphen (-) as your first argument, and -body followed by your message if your message will come from the console/keyboard, use the hyphen as your first argument, but do not use -body option."

I think the following should work...

blat.exe - -body "Backup completed with Errorlevel %Errorlevel%" -server EMAIL SERVER -f FILE SERVERUserBackup@domain.com -t admin@domain.com -s "FILE SERVER User Backup to NAS3 finished with errorlevel %Errorlevel%"

Author

Commented:
@Tech Stig: currently testing, thank you.

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial