我的域控制器上有一个小脚本,设置为通过 SMTP 向我发送有关最新安全事件 4740 的电子邮件。
该脚本在手动执行时将按预期运行;但是,当设置为通过计划任务运行时,尽管它显示已执行,但什么也没有发生(没有电子邮件)。
脚本如下:
If (-NOT ([Security.Principal.WindowsPrincipal][Security.Principal.WindowsIdentity]::GetCurrent()).IsInRole([Security.Principal.WindowsBuiltInRole] "Administrator"))
{
$arguments = "& '" + $myinvocation.mycommand.definition + "'"
Start-Process powershell -Verb runAs -ArgumentList $arguments
Break
}
$Event = Get-EventLog -LogName Security -InstanceId 4740 -Newest 5
$MailBody= $Event.Message + "`r`n`t" + $Event.TimeGenerated
$MailSubject= "Security Event 4740 - Detected"
$SmtpClient = New-Object system.net.mail.smtpClient
$SmtpClient.host = "smtp.domain.com"
$MailMessage = New-Object system.net.mail.mailmessage
$MailMessage.from = "[email protected] /cdn-cgi/l/email-protection"
$MailMessage.To.add("toemail.domain.com")
$MailMessage.IsBodyHtml = 1
$MailMessage.Subject = $MailSubject
$MailMessage.Body = $MailBody
$SmtpClient.Send($MailMessage)
计划任务设置如下:
RunsAs:LOCAL SYSTEM
Trigger: On event - Log: Security, Event ID: 4740
Action: Start Program - C:\Windows\System32\WindowsPowerShell\v1.0\powershell.exe
Argument: -executionpolicy bypass c:\path\event4740.ps1
我还尝试过以下方法:
Trigger: On event - Log: Security, Event ID: 4740
Action: Start Program - C:\path\event4740.ps1
根据任务历史记录:任务已开始、操作已开始、创建的任务流程、操作已完成、任务已完成。我浏览了网站上一些具有相同“问题”的各种链接,但它们似乎都有某种我没有的变量。我还尝试了一些提到的解决方案,认为它们可能有些相关,但可惜没有任何效果。我什至尝试删除我的计划任务并重置它,如下所述:http://blogs.technet.com/b/heyscriptingguy/archive/2012/08/11/weekend-scripter-use-the-windows-task-scheduler-to-run-a-windows-powershell-script.aspx http://blogs.technet.com/b/heyscriptingguy/archive/2012/08/11/weekend-scripter-use-the-windows-task-scheduler-to-run-a-windows-powershell-script.aspx
有人曾经遇到过此类错误或知道如何绕过此问题吗?
故障排除:
我决定尝试通过计划任务调用 .bat 文件。我创建了一个简单的文件,它将当前日期/时间回显到受监视的文件夹。通过 4740 事件触发的任务手动运行该文件取得了预期的结果。更改 .bat 文件以改为手动调用 .ps1 文件。当被 4740 事件触发时,.bat 现在将不再运行。