Display a list (different line)when converting objects to string and send mail

Welcome Forums General PowerShell Q&A Display a list (different line)when converting objects to string and send mail

This topic contains 1 reply, has 2 voices, and was last updated by

 
Participant
2 weeks ago.

  • Author
    Posts
  • #133308

    Participant
    Points: 12
    Rank: Member

    Hi,

    I have some objects stored in a variable and would like to use the select-object, do a format table and send them in an email.

     

    I am managing to do this by converting them to a string, however the formatting of the string/email is a one liner.

     

    How can I separate them to a different line? I want them to show , as when you do Format-Table for example.

     

    This is what I am doing (In soon expiring there are multiple thing):

    $body = $soonexpiring | Select-Object -Property samaccountname, @{label='Person Responsible';expression={$_.manager -replace '^CN=|,.*$'}} | Format-Table | Out-String

    send-MailMessage -SmtpServer $smtp -To $to -From $from -Subject $subject -Body $body -BodyAsHtml -Priority high

     

    How can this be done?

     

    Thanks in advance! 🙂

  • #133319

    Participant
    Points: 438
    Helping Hand
    Rank: Contributor

    Format-Table is meant for formatting within the Powershell prompt. Try leveraging ConvertTo-HTML as you are already using the -BodyAsHtml switch:

    $body = $soonexpiring |
    Select-Object -Property samaccountname, @{label='Person Responsible';expression={$_.manager -replace '^CN=|,.*$'}} |
    ConvertTo-Html -As Table |
    Out-String

You must be logged in to reply to this topic.