小编Thi*_*ace的帖子

Powershell脚本不通过计划任务运行

我的域控制器上有一个小脚本,设置为通过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, …
Run Code Online (Sandbox Code Playgroud)

windows powershell scheduled-tasks

24
推荐指数
3
解决办法
12万
查看次数

标签 统计

powershell ×1

scheduled-tasks ×1

windows ×1