none
PowerShell Reading HTML using Get-Content RRS feed

  • Question

  • Hi,

    I need to email the content of an HTML file using Send-MailMessage. 

    $MsgBody = Get-Content X:\myfile.html

    Send-MailMessage -To "me@mydomain.com" -from "report@mydomain.com" -Subject "TPM Reports" -body $MsgBody -SmtpServer mysmtp

    When I try to execute the script I get
    Send-MailMessage : Cannot convert 'System.Object[]' to the type 'System.String' required by parameter 'Body'. Specified method is not supported.

        + CategoryInfo          : InvalidArgument: (:) [Send-MailMessage], ParameterBindingException

        + FullyQualifiedErrorId : CannotConvertArgument,Microsoft.PowerShell.Commands.SendMailMessage


    If I add –RAW to the Get-Content cmdlet I end up with an HTML code inside the email body.

    Is there a way to craft the body of the message using the rendered HTML rather than sending the actual HTML code inside the message body?



    Thank you


    • Edited by net_tech Monday, February 8, 2016 6:15 PM
    Monday, February 8, 2016 6:11 PM

Answers

  • Hi,

    You need to pipe the message body into Out-String and also use the -BodyAsHtml switch of Send-MailMessage.


    • Marked as answer by net_tech Monday, February 8, 2016 6:40 PM
    Monday, February 8, 2016 6:26 PM

All replies

  • Hi,

    You need to pipe the message body into Out-String and also use the -BodyAsHtml switch of Send-MailMessage.


    • Marked as answer by net_tech Monday, February 8, 2016 6:40 PM
    Monday, February 8, 2016 6:26 PM
  • seemed to work just by adding -BodyAsHtml switch. 

    Do I still need to use Out-String? 

    Monday, February 8, 2016 6:36 PM
  • seemed to work just by adding -BodyAsHtml switch. 

    Do I still need to use Out-String? 

    Perhaps not, if you're still using the -Raw switch. I always do personally, but I never send HTML email via PowerShell. Otherwise you won't have a string and you'll need to pipe through Out-String.

    Monday, February 8, 2016 6:37 PM