由於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