Thi*_*ace 24 windows powershell scheduled-tasks
我的域控制器上有一个小脚本,设置为通过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 = "fromemail@domain.com"
$MailMessage.To.add("toemail.domain.com")
$MailMessage.IsBodyHtml = 1
$MailMessage.Subject = $MailSubject
$MailMessage.Body = $MailBody
$SmtpClient.Send($MailMessage)
Run Code Online (Sandbox Code Playgroud)
计划任务设置如下:
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
Run Code Online (Sandbox Code Playgroud)
我也尝试过以下方法:
Trigger: On event - Log: Security, Event ID: 4740
Action: Start Program - C:\path\event4740.ps1
Run Code Online (Sandbox Code Playgroud)
根据任务历史记录:任务已启动,已启动操作,已创建任务流程,已完成操作,任务已完成.我在网站上查看了一些具有相同"问题"的各种链接,但它们似乎都有某种我没有的变量.我也尝试了一些提到的解决方案,认为它们可能有些相关,但唉,什么都没有用.我甚至尝试删除我的计划任务并按照此处的说明重置它:http://blogs.technet.com/b/heyscriptingguy/archive/2012/08/11/weekend-scripter-use-the-windows-task-scheduler -to-运行一个窗口,PowerShell的script.aspx
有没有人遇到过这种类型的错误或知道如何绕过这个问题?
故障排除:
我决定通过计划任务尝试调用.bat文件.我创建了一个简单的文件,它将当前日期/时间回显到受监视的文件夹.手动运行文件并通过4740事件触发的任务实现了预期的结果.更改.bat文件,而不是调用.ps1文件.当由4740事件触发时,现在.bat将不再运行.
Col*_*350 30
将您的行动更改为:
powershell -noprofile -executionpolicy bypass -file C:\path\event4740.ps1
在Windows 2008服务器R2上:在"常规"选项卡下的"任务计划程序"中 - 确保"运行方式"用户设置为具有执行脚本所需权限的帐户.
此外,我相信您已选中"仅在用户登录时运行"选项已选中.将其更改为"运行用户是否登录".不选中Do Not Store密码选项,您可能需要标记"Run with Highest Privileges"选项.
小智 7
注意:请确保您选择创建基本任务操作而不是创建任务操作。
我找到了以下解决方案:
1)
powershell.exe为此以管理员身份运行
powershell.exe图标 2) 在任务计划程序窗口的操作窗格下添加以下脚本作为新命令
%SystemRoot%\syswow64\WindowsPowerShell\v1.0\powershell.exe -NoLogo -NonInteractive -ExecutionPolicy Bypass -noexit -File "C:\ps1\BackUp.ps1"
Run Code Online (Sandbox Code Playgroud)
小智 6
尽管您可能已经找到解决问题的方法,但我仍将发布此注释以使其他人受益。我遇到了类似的问题。我基本上使用了另一个域帐户进行测试和比较。选中“运行是否登录用户”,任务运行良好。
请记住并确保以下几点:
检查此链接,希望您或其他人可以从此信息中受益:https : //technet.microsoft.com/en-us/library/cc722152.aspx
| 归档时间: |
|
| 查看次数: |
121069 次 |
| 最近记录: |