最近在一台server上配置了每个周末备份数据库的定时任务,想顺手配置发送备份完成的邮件提醒我去Double Check一下备份结果。

悲剧地发现Send an email功能被新版的server给禁掉了。

只好另辟蹊径,想到通过PowerShell脚本来发送也行,找到一个脚本:

############################################################################### 
 
###########Define Variables######## 
 
$fromaddress = "donotreply@labtest.com" 
$toaddress = "Aishwarya.Rawat@labtest.com" 
$bccaddress = "Vikas.sukhija@labtest.com" 
$CCaddress = "Mahesh.Sharma@labtest.com" 
$Subject = "ACtion Required" 
$body = get-content .\content.htm 
$attachment = "C:\sendemail\test.txt" 
$smtpserver = "smtp.labtest.com" 
 
#################################### 
 
$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) 
$message.body = $body 
$smtp = new-object Net.Mail.SmtpClient($smtpserver) 
$smtp.Send($message) 
 
#################################################################################

原文引自:Send HTML Email and attachment Powershell

然后,在Task Scheduler的任务里面,最后的位置添加一个Action,设置如下:

Program:  C:\Windows\System32\WindowsPowerShell\v1.0\powershell.exe

Arguments: -command "& 'C:\SendEmailScript\SendMail.ps1'" 

 

调试运行时发现还是有问题,在PowerShell里面运行,发现有错误日志:

xxx.ps1 cannot be loaded because the execution of scripts is disabled on this system.

 

原来Windows 2012 Server 默认是关闭脚本执行的,需要自己手动打开:

1. 以管理员身份运行PowerShell。

2. 检查当前状态:输入 Get-ExecutionPolicy ,回车。 显示: Restricted

3. 修改状态:输入 Set-ExecutionPolicy Unrestricted

4. 再次输入 Get-ExecutionPolicy 检查状态应该显示 Unrestricted

 

好了,至此,解决了!

 

posted on 2015-04-02 14:45  SamZhang  阅读(5540)  评论(0编辑  收藏  举报