简体   繁体   English

使用Powershell,任务计划程序发送Outlook 2013电子邮件(电子邮件显示在草稿中;不发送)

[英]Sending Outlook 2013 E-Mail With Powershell, Task Scheduler (E-Mail Appears in Drafts; Doesn't Send)

I'm trying to send an email from my Outlook Account to my Outlook Account via Powershell and using Task Scheduler to run that script. 我正在尝试通过Powershell从Outlook帐户向Outlook帐户发送电子邮件,并使用Task Scheduler运行该脚本。

My Tash Scheduler is configured as such: 我的Tash Scheduler配置如下:

PowerShell -NoProfile -ExecutionPolicy Bypass -File "<script location\script name.ps1>" -Parameter 'Value'

And my PowerShell script looks like: 我的PowerShell脚本如下所示:

$Outlook = New-Object -ComObject Outlook.Application
$Mail = $Outlook.CreateItem("olMailItem")
$Mail.To = "<My Email>"
$Mail.Subject = "Subject"
$Mail.Body = "Body"
$Mail.Send() 

The e-mail gets sent successfully if I run the PS1 script myself by double clicking on it. 如果我自己双击运行PS1脚本,则电子邮件将成功发送。

If I have the scheduler do it, using my account, I can see the e-mail pop into the Drafts folder of my Outlook, the Drafts folder flickers some, and the e-mail disappears. 如果我让调度程序使用我的帐户执行此操作,则可以看到电子邮件弹出到Outlook的“草稿”文件夹中,“草稿”文件夹闪烁了一些,然后电子邮件消失了。

The only thing I found when searching that it might be related to is Object Model Guard. 我在搜索时可能发现的唯一与之有关的对象是对象模型防护。 But I don't know how to fix that. 但是我不知道该如何解决。

Any ideas on how to fix my situation? 关于如何解决我的情况有什么想法吗?

EDIT: 编辑:

I think it has to do with the setting "Run only when user is logged on" versus "Run whether user is logged on or not". 我认为这与设置“仅在用户登录时运行”相对于“无论用户是否登录运行”有关。

It works if I choose "Run only when user is logged on" but when I choose "Run whether user is logged on or not", it does the situation I mention above, even if I give it my password ahead of time. 如果我选择“仅在用户登录时运行”,但是当我选择“无论用户是否登录运行”时,它都可以解决上面提到的情况,即使我提前输入密码也可以。

FIXED! 固定!

Can't believe after all of that searching and trying, I got it to work. 经过所有的搜索和尝试后,我不敢相信,我成功了。

Having the Task Scheduler call a .BAT file that contains your PowerShell command fixed the issue, in my case: 在我的情况下,让任务计划程序调用包含PowerShell命令的.BAT文件即可解决此问题:

PowerShell -NoProfile -ExecutionPolicy Bypass -File "<script location\script name.ps1>" -Parameter 'Value'

No idea why this works. 不知道为什么这样做。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM