我有一个 exe 文件,每天由 Windows 2008 上的任务计划程序执行。如果该脚本无法启动,或者脚本在执行过程中失败,我希望收到电子邮件通知。
有许多让任务计划根据事件日志条目发送电子邮件的示例。但是,我只想在我的特定计划任务失败时收到通知,而不是收到 EventID 203/103/201 失败的所有任务的通知。如果没有任何定制软件,我该如何做到这一点?
创建一个运行此 PowerShell 脚本的新任务。
$ScheduledTaskName = "Taskname"
$Result = (schtasks /query /FO LIST /V /TN $ScheduledTaskName | findstr "Result")
$Result = $Result.substring(12)
$Code = $Result.trim()
If ($Code -gt 0) {
$User = "[email protected] /cdn-cgi/l/email-protection"
$Pass = ConvertTo-SecureString -String "myPassword" -AsPlainText -Force
$Cred = New-Object System.Management.Automation.PSCredential $User, $Pass
$From = "Alert Scheduled Task <task@servername>"
$To = "Admin <[email protected] /cdn-cgi/l/email-protection>"
$Subject = "Scheduled task 'Taskname' failed on SRV-001"
$Body = "Error code: $Code"
$SMTPServer = "smtp.company.com"
$SMTPPort = "25"
Send-MailMessage -From $From -to $To -Subject $Subject `
-Body $Body -SmtpServer $SMTPServer -port $SMTPPort -UseSsl `
-Credential $Cred
}
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)