Jenkins无法使用带有Outlook的PowerShell脚本发送电子邮件

时间:2017-05-15 09:46:09

标签: powershell batch-file jenkins

我有一个PowerShell脚本文件,它向预定用户发送一些特定的电子邮件,它使用Outlook并等待Outlook在同一台计算机上运行(在Windows平台上)。

在Jenkins中,我尝试使用此powershell脚本文件和批处理文件(嵌入其中的PowerShell文件)发送此电子邮件但未成功。

起初我遇到了错误The remote procedure call failed,但现在它已经不见了,但它只是不发送电子邮件;使用cmd或PowerShell终端,它可以完美运行。 我的代码如下:

# Filters and zips the files before sending by email
Function PreProcessAttachments
{
[cmdletBinding()]
param(
    [Parameter(Mandatory=$true)] [string]$integrationOutputFilePath,
    [Parameter(Mandatory=$true)] [string]$updateLogFilePath)

$attachments = @()
if (Test-Path $integrationOutputFilePath)
{
    $attachments += $integrationOutputFilePath
}
else
{
    Write-Verbose ("Attachment file $integrationOutputFilePath " + 
        "does not exist. Ignoring..");
}

if (Test-Path $updateLogFilePath)
{
    $attachments += $updateLogFilePath
}
else
{
    Write-Verbose ("Attachment file $integrationOutputFilePath " + 
        "does not exist. Ignoring..");
}

return $attachments
}

Function ReportError
{
[cmdletBinding()]
param(
    [Parameter(Mandatory=$true)] [string]$stream,
    [Parameter(Mandatory=$true)] [string]$integrationOutputFilePath,
    [Parameter(Mandatory=$true)] [string]$updateLogFilePath)


$attachments = PreProcessAttachments $integrationOutputFilePath
                    $updateLogFilePath;

$subject = "[Integration Internal Error] at $stream"

$body = "An internal error has occurred during integration.`n";
$body += "`nIntegration Log is attached to this e-mail.`n"

$recipients = "tolga@mycompany.com"

write-output "Sending integration internal error email to: $recipients"
try
{
    SendEmailUsingOutLook $subject $recipients $body $attachments | Out-Null                                
} 
catch
{
    Write-Warning "Couldn't send integration internal error emails $_"
}
}

# send e-mail using OutLook
# note: Attachments should be an array:
Function SendEmailUsingOutLook
{
[cmdletBinding()]
Param(
    [Parameter(Mandatory=$true)] [string]$subject,
    [Parameter(Mandatory=$true)] [string]$to,
    [Parameter(Mandatory=$true)] [string]$body,
    $attachments) 

$Outlook = New-Object -ComObject Outlook.Application   
$Mail = $Outlook.CreateItem(0)
$Mail.To = $to
$Mail.Subject = $subject
$Mail.Body = $body

if ($attachments)
{
    foreach ($file in $attachments)
    {
        $Mail.Attachments.Add($file)    
    }
}

$Mail.Send()
}

Function StartPocess
{
$confFile = "path\to\myconf.xml"
LoadConfs $confFile

ReportError -stream $stream
        -integrationOutputFilePath $integrationOutputFilePath
        -updateLogFilePath $updateLogFilePath
}

# Start the script:
StartPocess;

1 个答案:

答案 0 :(得分:0)

好的问题是我的Jenkins是作为服务运行的(尽管它使用我的凭据)并且我认为服务在powershell中有comobjects的问题。

  1. 我从命令行usig jenkins.exe stop
  2. 停止了Jenkins
  3. 使用{JENKINS_HOME}\jre\bin\java -jar jenkins.war
  4. 启动它

    这也向我展示了一个空的Jenkins界面,但我已将旧的作业和插件目录复制到{USERNAME}\.jenkins下的新安装文件夹并在同一路径中重新启动。该服务未运行,Jenkins正在使用命令行。