首页 > 解决方案 > Powershell - 发送包含数组的电子邮件,需要将每个项目放在新行上

问题描述

当尝试发送包含在数组中收集的文件列表的电子邮件时,该数组将整个列表打印在一个字符串中。目标是将找到的文件列表打印在每个项目的新行上。

function SendReport
{
    $rptDataPath = "C:\Maintenance\Logs\"
    [array]$rptData = Get-ChildItem -Path "$rptDataPath" | select Name | Out-String
    $smtpBody = "Below are the following files located in $rptDataPath <br><br> $rptFileList"
    Send-MailMessage -SmtpServer $smtpServer -To $smtpRecipient -From $smtpSender -Subject $smtpSubject -Body $smtpBody -BodyAsHtml
}
SendReport

标签: htmlarrayspowershell

解决方案


iRon 提供的代码片段解决了我的问题。下面是我用来向需要目录中文件列表的收件人发送电子邮件的功能。谢谢您的帮助。

$smtpServer = "<Mail Server>"
$smtpRecipient = "Recipient Address"
$smtpSubject = "PowerShell Reporting HTML Example"
$smtpSender = "From or Spoofing Address"
function SendReport
{
    $rptDataPath = "<Directory Path>"
    $rptData = (Get-ChildItem -Path "$rptDataPath").name -join '<br>'
    $smtpBody = "Below are the following files located in $rptDataPath <br> $rptData"
    Send-MailMessage -SmtpServer $smtpServer -To $smtpRecipient -From $smtpSender -Subject $smtpSubject -Body $smtpBody -BodyAsHtml
}
SendReport

推荐阅读