发送多行身体和多个附件

时间:2014-12-15 15:34:54

标签: powershell

我有两个不同的文件夹包含两个文本文件,目标是使用PowerShell在电子邮件中发送两个txt文件。 第一个文件存储在日志中,另一个存储在Error中。我找到了一个有用的PowerShell脚本,但我不知道如何在同一个电子邮件中添加两个附件,并且每个附件都有两行。以下是脚本,任何帮助表示赞赏:

# Date values to find related log file for the day and hour or the run
$y = Get-Date -format yyyy
$m = Get-Date -format MM
$d = Get-Date -format dd
$h = Get-Date -format 05
# Modify the log path
$LogPath       = "C:\Program Files\Logs\"
$LogFile       = $LogPath + "Log_" + $y + $m  + $d + "_" + $h + "*.txt" 
$LogFileName   = $LogFile | ? {Test-Path $LogFile} | Get-ChildItem | Where-Object { $_ -is [System.IO.FileInfo] }

$LogPath1       = "C:\Program Files\ERROR\"
$LogFile1       = $LogPath1 + "Log_" + $y + $m  + $d + "_" + $h + "*.txt" 
$LogFileName1   = $LogFile1 | ? {Test-Path $LogFile} | Get-ChildItem | Where-Object { $_ -is [System.IO.FileInfo] }

$MessageBodyNA = "Email from scheduler. No log file found  " + $LogFile
$MessageBodyA  = "Email from scheduler. File" + $LogFileName + " found and attached"

$MessageBodyNA1 = "Email from scheduler. No log file found " + $LogFile1
$MessageBodyA1  = "Email from scheduler. File(s) " + $LogFileName1 + " found and attached"

$FromAddress   = "test@gmail.com"

$ToAddress     = "test1@gmail.com"
$Subject       = "test"

$SMTPserver    = "SMTPServerName.com"


if ( $LogFileName | Where-Object { $_ -is [System.IO.FileInfo] })
{
  send-mailmessage -from $FromAddress -to $ToAddress -subject $Subject -body  $MessageBodyA  -smtpServer $SMTPserver -Attachments $LogFileName
}
else
{
  send-mailmessage -from $FromAddress -to $ToAddress -subject $Subject -body  $MessageBodyNA  -smtpServer $SMTPserver
}

if ( $LogFileName1 | Where-Object { $_ -is [System.IO.FileInfo] })
{
  send-mailmessage -from $FromAddress -to $ToAddress -subject $Subject -body  $MessageBodyA1  -smtpServer $SMTPserver -Attachments $LogFileName1
}
else
{
  send-mailmessage -from $FromAddress -to $ToAddress -subject $Subject -body  $MessageBodyNA1  -smtpServer $SMTPserver
}

2 个答案:

答案 0 :(得分:1)

如有疑问,请阅读documentation-Attachment参数接受字符串数组,因此您可以附加多个文件,如下所示:

Send-MailMessage -From $FromAddress -To $ToAddress -Subject $Subject `
  -Body $MessageBodyA -SmtpServer $SMTPserver `
  -Attachments $LogFileName, $logFileName1

使用多行字符串可以轻松创建多线体:

$MessageBody = @"
This is line 1.

This is line 2.
"@

或通过连接多行:

$line1 = "This is line 1."
$line2 = "This is line 2."

$MessageBody = "$line1`n`n$line2"

答案 1 :(得分:-1)

这显示了如何在邮件正文中附加多个文件和多行(请注意,我没有对此进行测试)

 function sendMail{

    #SMTP server name and other mail parameters
    $smtpServer = "smtp.bethanie.com.au"
    $OFS = "`r`n`r`n"
    $a = get-date
    $SourceDir = "C:\Source\"
    $DestDir = "C:\Dest\"
    $files = get-childitem $SourceDir | where name -like "*.txt"
    $attachments = @()
    $msgFrom = "abc@abc.com"
    $msgTo = "abc@abc.com"
    $msgsubject = "Automated Task(" + $a + " )"
    $msgBody = "This is an automated mail" + $OFS + "Check the attached file"


    # Attach files of specific types 
    foreach($file in $files){
        $filename = [system.io.path]::GetFileName($file.FullName)
        $attachments += $file.fullname
    }

    Send-MailMessage -to $msgTo -From $msgFrom -SmtpServer $smtpServer -Subject $msgsubject -BodyAsHtml $msgBody -Attachments $attachments

}
#Calling function
sendMail