由于Win 2008 R2服务器上面有不少计划任务,邮件通知若是任务运行失败。由于任务运行失败会有不一样的Event ID生成是日志里面,因此,若是有不一样的Event ID,应该再作一个计划任务。
shell
新建一个计划任务,类型以下,并设置作如下三个配置.服务器
选择Run whether user is logged on or not.app
Triggers配置以下图.ide
Action 配置以下.测试
Program script C:\Windows\System32\WindowsPowerShell\v1.0\powershell.exe3d
Add arguments (optional) -command "C:\scripts\Task_Failed_Alert.ps1"日志
Task_Failed_Alert.ps1 脚本以下。orm
#This is script is for email alert when Event ID 101 occur in Task Scheduler. $OutputFileLocation = "C:\scripts\Task_Failed_Alert.log" Start-Transcript -path $OutputFileLocation -append $ErrorMessage = wevtutil qe Microsoft-Windows-TaskScheduler/Operational "/q:*[System [(EventID=101)]]" /f:text /rd:true /c:1 #Get EventID 101 lastest details in Operational log $ComputerName = $ErrorMessage |select-string -pattern "computer" #Display specific line. $ComputerNames = $ComputerName.Line.Split(".")| Select-Object -first 1 #Split specific phrase. $ErrorSubject = $ErrorMessage |Select-string -pattern "Additional Data" #Display specific line. $ErrorSubjects = $ErrorSubject.Line.Split(".")| Select-Object -first 1 #Split specific phrase. $ErrorSub = "$Computernames" + " " + "$ErrorSubjects" $ErrorMessage = $ErrorMessage |Out-String #Formatting string for email Body. Send-MailMessage -From "ABC@ABC.com" -To "BBC@ABC.com" -Subject "$ErrorSub" -Body "$ErrorMessage" -SMTPServer SMTPSERVER Write-Host "Mail message sent on $(Get-Date -format 's')" Write-Host $ErrorMessage
能够新建一个任务来测试以上邮件通知是否成功,以权限不够的用户来运行这个任务,就能够产生Event ID 101的错误。blog
注意,不要设置默认记事本打开这个脚本,不然任务运行时就会以记事原本打开这个脚本,而不是PowerShell.exe来运行。ip