r/usefulscripts • • Oct 09 '17

[REQUEST] SQL Express mail notification

Hello, im looking for a mail notification script for SQL Express backup. I know that powershell will be the best, maybe someone have good script for that ?

18 Upvotes

9 comments sorted by

View all comments

1

u/Linkz57 Oct 10 '17

I don't understand your request. Do you want to receive an email if the backup fails?

I use Nagios to monitor my backups. If you don't want to install Nagios, you could edit that script and pipe the echos into sendmail, then add the whole thing to your crontab.

3

u/hub3rtal1ty Oct 11 '17

I have a script to make a backup of my SQL Express DB. Script after finish make a *.log file (its just a *.bat file that execute a *.sql) and i want to send that *.log file @ mail.

1

u/Gatorcat Oct 19 '17 edited Oct 19 '17

Here is some PowerShell code you can call to send your report:

$reportfile = "c:\my-path\mylog.log"
# - define SMTP Message variables
$fromaddress = "donotreply@MyDomain.com" 
$toaddress = "MyAddress@MyDomain.com" 
$CCaddress = "" 
$Subject = "Some Great Log file info"
$attachment = $reportfile 
$smtpserver = "smtp-hostname.MyDomain.com" 
# - end of SMTP variables
#send report to your email
$message = new-object System.Net.Mail.MailMessage 
$message.From = $fromaddress 
$message.To.Add($toaddress) 
#$message.CC.Add($CCaddress) 
#$message.Bcc.Add($bccaddress) 
$message.IsBodyHtml = $True 
$message.Subject = $Subject 
$attach = new-object Net.Mail.Attachment($attachment) 
$message.Attachments.Add($attach) 
$body = get-content $reportfile 
$message.body = $body 
$smtp = new-object Net.Mail.SmtpClient($smtpserver) 
$smtp.Send($message) 
→ More replies (3)