CreateItem(0)在计划任务中失败

时间:2018-07-10 07:55:35

标签: powershell scheduled-tasks

我有PowerShell脚本可以发送电子邮件。当我直接运行它时,效果很好。

通过任务计划程序进行计划时失败。下面是代码

# Get Outlook Object
if (Get-Process Outlook) {
    $outlook = [System.Runtime.InteropServices.Marshal]::GetActiveObject('Outlook.Application')
} else {
    $outlook = New-Object -ComObject Outlook.Application
}

$Mail = $Outlook.CreateItem(0)
$Mail.To = "x@y.com"
$Mail.Subject = "Test Test TEst"
$Mail.Body = "Test Test TEst"
$Mail.Send()

$Outlook.CreateItem(0)是失败的那个。 错误:

The term 'Outlook.CreateItem' is not recognized as the name of a cmdlet,
function, script file, or operable program. Check the spelling of the name,
or if a path was included, verify that the path is correct and try again.
At C:\Temp\SendMail.PS1:9 char:31
+ $Mail = Outlook.CreateItem <<<< (0)
    + CategoryInfo          : ObjectNotFound: (Outlook.CreateItem:String) [], CommandNotFoundException
    + FullyQualifiedErrorId : CommandNotFoundException"

0 个答案:

没有答案