VMware Cloud Community
DZ1
Hot Shot
Hot Shot

HTML embedded in Email issues

I'm rewriting a report that I send out that has some vCenter information, I want to embed html right into the email that I send, but I'm having a small problem with the output.  I took a screenshot of the email and as you will notice, right under my title (Report), there is a small square, or dot, or something, and then under the 2 sections, the row has an asterisk inside of it, and then the information.  The report that I have has everything as an object with New-Object PSObject, and right as I was writing this, I decided to try using objecst, and now I have a new problem.  Well, here is the first code, without objects

$html = @'

<style>

BODY{background-color:#999966;}

TABLE{border-width:1px; border-style:solid; border-color:black; border-collapse:collapse;}

TH{border-width:1px; padding:0px; border-style:solid; border-color:black;}

TD{border-width:1px; padding:0px; border-style:solid; border-color:black;}

</style>

'@

$MyVM = Get-VM | Select -First 5 | select name

$MyHost = Get-VMHost | select -First 5 | select name

$MyhtmlVM = $MyVM | ConvertTo-html -Fragment -PreContent "<h1>Test 1</h1>" | Out-String

$MyhtmlHost = $MyHost | ConvertTo-Html -Fragment -PreContent "<h1>Hosts</h1>" | Out-String

$All = ConvertTo-Html -Head $html -PostContent $Myhtmlvm, $MyhtmlHost -PreContent "<h2>Report</h2>"

Send-MailMessage -SmtpServer '' -From "<@i.com>" -To "<@.com>" -Subject ("Daily summary for " + (Get-Date -Format "MM-dd-yyyy")) -body  ($All | Out-String ) -BodyAsHtml



And now with objects, and the problem is getting everything to come out as a list, so far, all I get is 1 long table with data in it.  I basically want the format to be like my "NoObject" attachment.  I've tried playing with it, but I can't get the format right. Thanks for any help.

$html = @'

<style>

BODY{background-color:#999966;}

TABLE{border-width:1px; border-style:solid; border-color:black; border-collapse:collapse;}

TH{border-width:1px; padding:0px; border-style:solid; border-color:black;}

TD{border-width:1px; padding:0px; border-style:solid; border-color:black;}

</style>

'@

$MyVM = Get-VM | Select -First 5 | select -ExpandProperty name

$MyHost = Get-VMHost | select -First 5 | select -ExpandProperty name

$objVM = New-Object PSObject

$objVM | Add-Member -MemberType NoteProperty -Name "VMs" -Value ([string]::Join(", ",$MyVM)) | fl

$objHost = New-Object PSObject

$objHost | Add-Member -MemberType NoteProperty -Name "Hosts" -Value ([string]::Join(", ",$MyHost))

$MyhtmlVM = $objVM | ConvertTo-html -As List -Fragment -PreContent "<h1>VMs</h1>" | Out-String

$MyhtmlHost = $objHost | ConvertTo-Html -As List -Fragment -PreContent "<h1>Hosts</h1>" | Out-String

$All = ConvertTo-Html -Head $html -PostContent $Myhtmlvm, $MyhtmlHost -PreContent "<h2>Report</h2>"

Send-MailMessage -SmtpServer '1' -From "<@om>" -To "<r@com>" -Subject ("Daily summary for " + (Get-Date -Format "MM-dd-yyyy")) -body  ($All | Out-String ) -BodyAsHtml

The report that I have will have a good amount of info, but everything gets output as an object, so if I can get the formatting correct, modifying the rest of my code should be easy (famous last words).

Reply
0 Kudos
3 Replies
LucD
Leadership
Leadership

If you look at the generated HTML you'll notice that the asterisk comes from the entry <tr><th>*</th></tr>.

That's because the cmdlet is trying to use a header, and since none is specified it uses the asterisk.

If you explicitely specify the property, the propertyname will be used as the table header.

Try changing those 2 lines like this

$MyhtmlVM = $MyVM | ConvertTo-html -Property Name -Fragment -PreContent "<h1>Test 1</h1>" | Out-String

$MyhtmlHost = $MyHost | ConvertTo-Html -Property Name -Fragment -PreContent "<h1>Hosts</h1>" | Out-String


Blog: lucd.info  Twitter: @LucD22  Co-author PowerCLI Reference

Reply
0 Kudos
DZ1
Hot Shot
Hot Shot

A always, thanks again.  The bigger issue is actually using objects with the html output.  In my example on the second section of code, I can't seem to get the data when I use objects to come out in a list format. I've tried using format-list in different areas, but it keeps coming out in a 1 row table. It's the Objects.jpg and second section of code.  Thanks again. 

Reply
0 Kudos
DZ1
Hot Shot
Hot Shot

I can't seem to get anything to come out as a list when I send my report as html within email, it's the second example that I listed.  I have tried using format-list in various places, and nothing seems to work.  I even tried joining and then splitting the data, but that does not work.  Maybe I'm just going about it wrong.  Should I change up the way the objects are created and then sent to html?  Right now I create an object, and then join them together, and then convert it to html, and email it out.  Overall it looks okay, the background color shows up, the titles are bold, but the data all shows as one long row. 

Reply
0 Kudos