Windows 2012 R2 计划任务发送邮件

来源:互联网 发布:ps mac版破解 编辑:程序博客网 时间:2024/05/16 10:25


    这两天把域控制器升级到了2012 R2,忽然发现原本用的系统自动发邮件提示用户账户锁定的计划任务配置起来有点麻烦了。原因是微软把自动发送邮件和提示消息的功能从计划任务中去除了。



首先用wevtutil导出日志中账户锁定的信息,保存成TXT文档,稍后用命令把它作为附件发出去。
wevtutil.exe qe Security "/q:*[System [(EventID=4740)]]" /f:text /rd:true /c:1 > c:\evt\accountlocked.txt
接下去,我在脚本中心找到这个脚本,稍微修改一下就可以用powershell命令发送带附件的邮件。
https://gallery.technet.microsoft.com/scriptcenter/Send-HTML-Email-Powershell-6653235c




###############################################################################


 


###########Define Variables########


 


$fromaddress="server@yourdomain.com"


$toaddress="alert@yourdomain.com"


#$bccaddress = "Vikas.sukhija@labtest.com"


#$CCaddress = "Mahesh.Sharma@labtest.com"


$Subject="Account Lock out"


#$body = get-content .\content.htm


$attachment="C:\evt\accountlocked.txt"


$smtpserver="SMTPSERVER"


 


####################################


 


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


 


#################################################################################


在计划任务中添加第二个程序,来运行powershell脚本。先运行powershell,把脚本文件作为参数天骄在后面。
C:\Windows\System32\WindowsPowerShell\v1.0\powershell.exe
-command "& 'C:\evtlog\sendmail.ps1'"


配置截图入下:


0 0
原创粉丝点击