2

当我将以下脚本一个一个地写入powershell命令行时,它成功发送了电子邮件,但是当我运行脚本时,它返回了一堆错误。我猜在语法上需要更改某些内容才能将其作为脚本运行?有任何想法吗?

Start-Process Outlook

$o = New-Object -com Outlook.Application


$mail = $o.CreateItem(0)

#2 = high importance email header
$mail.importance = 2

$mail.subject = “Auto Build Test“

$mail.body = “This is a test“

#for multiple email, use semi-colon ; to separate
$mail.To = “myemail@company.com"
$mail.Send()

# $o.Quit()
4

2 回答 2

1
Param(
[parameter(Mandatory=$true)]
[alias("e")]
[string]$RecipientEmailAddress
)

if($RecipientEmailAddress -notmatch  "\b[A-Za-z0-9._%+-]+@BLAHH.com")
{
    Write-Output "the email address for the receipient of log reports is not a valid       email address, hence will not send the report via email. They can still be accessed at "   |Out-String ;
}else
{
    $returnVal= New-Object PSObject ;
    $returnVal |Add-Member -Name is_Success -MemberType NoteProperty -Value $null;
    $returnVal |Add-Member -Name Explanation -MemberType NoteProperty -Value $null;
    try{
            $Attachments =Get-ChildItem -Path "C:\FOLDERWHEREYOURAATACHMENTS ARESTORED";
            if($Attachments.count -eq 0)
            {
                 $returnVal.Explanation="Error sending log report email to the user: $RecipientEmailAddress. Please check if the C:\FOLDERWHEREYOURAATACHMENTS is accessible and there are indeed log files present";
            #Write-Output "Error sending log report email to the user: $RecipientEmailAddress" |Out-String ;
            #Write-Output "Please check if the C:\FOLDERWHEREYOURAATACHMENTS is accessible and there are indeed log files present "|Out-String;
            $returnVal.is_Success= $false;
            return $returnVal;
        }
        $TestedAttachmentsList = new-Object System.Collections.ArrayList;
        for($i=0;$i -lt $Attachments.count;$i++)
        {
           $TestedAttachmentsList.add($Attachments[$i].FullName);
        }
        Send-MailMessage -From "<FROM@BLAHH.COM>" -To "<$RecipientEmailAddress>" -SmtpServer "mail.BLAHH.com" -Attachments $TestedAttachmentsList -Subject "BLAHH SUBJECT" -Body "BLAHH BLAHH";
        $returnVal.is_Success=$true;  
        $returnVal.Explanation="An email has been sent to the $RecipientEmailAddress containing the log of the setup and configuration." 
        return $returnVal ; 
}Catch [System.Exception]
   {
        #Write-Output "Error sending log report email to the user: $RecipientEmailAddress" |Out-String ;
        #Write-Output "Please check communication between your host machine and mail.BLAHH.com on port 25 is possible"|Out-String;
        $returnVal.is_Success= $false;
        $returnVal.Explanation="Error sending log report email to the user: $RecipientEmailAddress Please check communication between your host machine and mail.BLAHH.com on port 25 is possible";
        return $returnVal ; 
}
}
于 2013-08-09T23:40:42.333 回答
0

命令行和脚本文件之间的语法不会改变。改变的是命令执行的速度。如果您正在输入它们,那么每个命令之间会有很多延迟。但是,如果它们从脚本运行,它们会更快地呈现给 Outlook。

解决此问题的一种简单方法是Sleep 1在失败的命令之前添加(或类似的)。如果没有看到你的错误输出,我猜你想在 CreateItem 之后睡觉,也许在 Send 之前睡觉。但是,如果您仔细查看错误消息,您会发现它们确定了脚本的哪一行失败。将 Sleep 放在失败的第一行之前。重试脚本。如果一条新线路出现故障,那么在它之前也放置一个延迟。如果第一行仍然失败,您可以尝试Sleep 2. 您还可以缩短睡眠时间。1/2 秒:Sleep -milliseconds 500.

如果添加 Sleeps 可以解决问题 - 换句话说,问题是同步问题,您可以使用的 Outlook 对象模型中的某些内容可能不会像使用 Sleeps 那样骇人听闻。


我无法在我的 Outlook 2010 安装中重现这一点。但是,我确实查找了从 PS 发送电子邮件的替代方法(如下)。也许这种方法会奏效。

$i=$o.Session.folders.item(2).folders.item("Outbox").items.add(0)
$i.to="me@whereiwork.com"
$i.Subject="a wittle testy"
$i.Body="some body"
$i.send()
于 2013-07-12T00:48:59.750 回答