最近在一台server上配置了每一个周末备份数据库的定时任务,想顺手配置发送备份完成的邮件提醒我去Double Check一下备份结果。shell
悲剧地发现Send an email功能被新版的server给禁掉了。数据库
只好另辟蹊径,想到经过PowerShell脚原本发送也行,找到一个脚本:ui
############################################################################### ###########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 Powershellthis
而后,在Task Scheduler的任务里面,最后的位置添加一个Action,设置以下:spa
Program: C:\Windows\System32\WindowsPowerShell\v1.0\powershell.exe调试
Arguments: -command "& 'C:\SendEmailScript\SendMail.ps1'" rest
调试运行时发现仍是有问题,在PowerShell里面运行,发现有错误日志:日志
xxx.ps1 cannot be loaded because the execution of scripts is disabled on this system.code
原来Windows 2012 Server 默认是关闭脚本执行的,须要本身手动打开:server
1. 以管理员身份运行PowerShell。
2. 检查当前状态:输入 Get-ExecutionPolicy ,回车。 显示: Restricted 。
3. 修改状态:输入 Set-ExecutionPolicy Unrestricted 。
4. 再次输入 Get-ExecutionPolicy 检查状态应该显示 Unrestricted。
好了,至此,解决了!