簡體   English   中英

Powershell - 發送包含數組的 email 消息,需要將每個項目放在新行上

[英]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.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM