[英]Powershell - Sending email message that contains an array, need to have each item on a new line
当尝试发送包含在数组中收集的文件列表的 email 时,该数组将整个列表打印在一个字符串中。 目标是将找到的文件列表打印在每个项目的新行上。
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
iRon 提供的代码片段解决了我的问题。 在此下方是 function 我曾经使用 email 的收件人需要目录中的文件列表。 谢谢您的帮助。
$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
您可以首先将$OFS
特殊变量(输出字段分隔符)设置为<backtick>n
。 然后使用 [string] 类型加速器将数组转换为字符串,数组中的每个项目都位于新行上。
$smtpbody = "Files: $( $ofs = "`n"; [string]$rptdata ) "
如果您想要另一个分隔符,请注意在此之后转换 arrays ,因为它将保持为新行直到脚本结束。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.