Convertto-HTML outputting pre/post content showing up as System.String[] instead of actual content

9.9k views Asked by At

I am trying to use Powershell to output a table with some pre/post content and then email it, but the pre/post content is showing up in the email as "System.String[]." The rest of the content seems fine, and if I output the HTML string to the console, everything looks fine.

function Send-SMTPmail($to, $from, $subject, $smtpserver, $body) {
    $mailer = new-object Net.Mail.SMTPclient($smtpserver)
    $msg = new-object Net.Mail.MailMessage($from,$to,$subject,$body)
    $msg.IsBodyHTML = $true
    $mailer.send($msg)
}

$Content = get-process | Select ProcessName,Id
$headerString = "<table><caption> Foo. </caption>"
$footerString = "</table>"
$MyReport = $Content | ConvertTo-Html -fragment -precontent $headerString -postcontent $footerString

send-SMTPmail "my Email" "from email" "My Report Title" "My SMTP SERVER" $MyReport

Shows up in my email as:

System.String[]
ProcessName Id
...             ...
System.String[]

Doing an out-file and then an invoke-item has the same results as sending the email...

2

There are 2 answers

2
Keith Hill On BEST ANSWER

ConvertTo-Html returns a list of objects - some are strings and some are string arrays e.g.:

407# $headerString = "<table><caption> Foo. </caption>"
408# $footerString = "</table>"
409# $content = Get-Date | select Day, Month, Year
410# $MyReport = $Content | ConvertTo-Html -Fragment -PreContent $headerString `
                                           -PostContent $footerString
411# $MyReport | Foreach {$_.GetType().Name}
String[]
String
String
String
String
String
String
String
String
String
String[]

So $MyReport contains an array of both strings and string arrays. When you pass this array to the MailMessage constructor, which expects type string, PowerShell attempts to coerce that to a string. The result is:

412# "$MyReport"
System.String[] <table> <colgroup> <col/> <col/> <col/> </colgroup> <tr><th>Day
</th><th>Month</th><th>Year</th></tr> <tr><td>9</td><td>2</td><td>2011
</td></tr> </table> System.String[]

The easy solution is to run the output of ConverTo-Html through Out-String which will cause $MyReport to be a single string:

413# $MyReport = $Content | ConvertTo-Html -Fragment -PreContent $headerString `
                                           -PostContent $footerString |
                            Out-String
414# $MyReport | Foreach {$_.GetType().Name}
String
1
beefarino On

convertto-html returns a list of strings, not a string. So I think $myreport ends up being an object array; e.g., try this:

$Content = get-process | Select ProcessName,Id
$headerString = "<table><caption> Foo. </caption>"
$footerString = "</table>"
$MyReport = $Content | ConvertTo-Html -fragment -precontent $headerString -postcontent $footerString
get-member -input $MyReport

Instead force $myreport to be a string before passing it to send-SMTPMail:

$MyReport = ( $Content | ConvertTo-Html -fragment -precontent $headerString -postcontent $footerString ) -join "`n";