VMware Cloud Community
DZ1
Hot Shot
Hot Shot

-BodyAsHtml

I am rewriting a script, and I want the output to be in HTML in a direct email message.  In my last script, I would write a function, and then in the Send-MailMessage cmdlet, I would have something like -Body (Write-Mydata | Out-String).  That would write everything that I wanted, and overall it looked nice, but I want to step it up with HTML, and the text gets off-center in certain versions of outlook when I use Out-String

I have used HTML for a snapshot report, so I would have:

$custom = '<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>'


Script data

Script data


ConvertTo-Html -Head $custom -Body etc etc

That would put everything in a table, and it works fine, but I can't (or don't know how) use it the same with this script.

I tried some different things, I would Use the Send-MailMessage, and for the body portion I tried:

-Body (Write-Mydata | OutString | ConvertTo-HTML) Does not work

-Body (Write-Mydata | OutString) -BodyAsHtml Writes as Html, but looks very ugly

Overall, I want to have certain text in different colors in the report, as well as having some things in bold, I also like the way HTML lines up everything nicely.

I'm including the script that I am working on, it's still a work in progress, so I have some things commented out.

Forgot something, The $custom variable for the HTML format, I did play around with it in Send-MailMessage, I was just trying out some different things. 

Reply
0 Kudos
6 Replies
Grzesiekk
Expert
Expert

Hi,

$Head=@"
<Title>my_title</Title>
 <Style type="text/css">
  body {
   margin-left: 20%;
   text-align: Center;
    margin-right: 20%;    padding-top: 30px;    padding-bottom: 40px;    color: #2E2E2E;    background-color: #E6E6E6 }      table.gridtable {      font-family: verdana,arial,sans-serif;      font-size:11px;      color:#333333;      border-width: 1px;      border-color: #666666;      border-collapse: collapse; }   table.gridtable th {      border-width: 1px;      padding: 8px;      border-style: solid;      border-color: #666666;      background-color: #dedede; }   table.gridtable td {      border-width: 1px;      padding: 8px;      border-style: solid;      border-color: #666666;      background-color: #ffffff; } </Style> "@

Then

$body = ($ESXiInfo | convertTo-Html -head $head ) -Replace '<table>','<table class="gridtable">'


Where the ="gridtable" is the css style name for that table, or not needed if you define body style.

then

Send-MailMessage -SmtpServer '1.1.1.1' -From "<email>" -To "<email.com>" -Subject "Test" -Body $body -BodyAsHtml

If you want to have more advanced message, use -Precontent, -Postcontent while doing:

$body = ($ESXiInfo | convertTo-Html -head $head ) -Replace '<table>','<table class="gridtable">'

so you can add here: ($ESXiInfo | convertTo-Html -head $head -precontent $pre -postcontent $post)

Where in $pre and $post you define welcome message and some footer for example. And in center you will have your $esxiinfo object in table that was converted to html table.

If you do not want to have table you can use the '-as list' switch in convertto-html cmdlet.

Regards,

Greg

Message was edited by: Grzesiekk

--- @blog https://grzegorzkulikowski.info
Reply
0 Kudos
LucD
Leadership
Leadership

The CovertTo-Html should comes first (before the Out-String cmdlet).

Did you try using the -Head and -Body parameters ?

Once your generated HTML is to your liking, the  "-Body            ($text | ConvertToHtml -Head $head -Body $body | Out-String) -BodyAsHtml" should do the trick.

The content of $head and $body are your style statements and of course important for the quality of the produced body


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

Reply
0 Kudos
Grzesiekk
Expert
Expert

Luc,

why do we have to use the out-string here ?

"-Body            ($text | ConvertToHtml -Head $head -Body $body | Out-String) -BodyAsHtml"

I admit i did not test it, so that's why i am asking now Smiley Wink will test it tomorrow but:

if i will do something like this

$a=new-object PSObject

add-member -InputObject $a -Name "aaa" -Value "bbb" -MemberType noteproperty

add-member -InputObject $a -Name "myba" -Value "ccc" -MemberType noteproperty

and then:

PS C:\Users\gregu> $a|convertto-html|gm
   TypeName: System.String

And if do :

PS C:\Users\gregu> $a|convertto-html |Out-String|gm

   TypeName: System.String

So my question is, will there be any difference if i will omit the out-string, or it's needed here for some reason ?

==

Right.... i think i see my error here:

PS C:\Users\gregu> $b=$a|ConvertTo-Html
PS C:\Users\gregu> $c=$a|ConvertTo-Html|out-string
PS C:\Users\gregu> gm -InputObject $b

TypeName: System.Object[]

PS C:\Users\gregu> gm -InputObject $c

   TypeName: System.String

So that would lead to conclusion that in fact $a|ConvertTo-Html this return array of string, and $a|ConvertTo-Html|out-string outputs only one string. So that would mean that  -Body can't tolerate arrays... (checking get-help..)

-Body <string>
        Specifies the body (content) of the e-mail message.

Right...

would be nice if you would confirm my thinking that we always in this case have to use the out-string to get only 1 string for the -body parameter.

--- @blog https://grzegorzkulikowski.info
Reply
0 Kudos
LucD
Leadership
Leadership

The reason is that the ConvertTo-Html cmdlet will produce an array of strings.

And the Body parameter wants a [string], not an array of strings.

In your example you feed a single string to the ConvertTo-Html cmdlet, so there the Out-String is not needed.

Would $a have been an array, you would have needed the Out-String to make the Body parameter accept it correctly


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

Reply
0 Kudos
Grzesiekk
Expert
Expert

Thanks so much for explanation Luc!

--- @blog https://grzegorzkulikowski.info
Reply
0 Kudos
DZ1
Hot Shot
Hot Shot

I know it's me, I can't seem to get this to work.  I have the $Head portion at the top, but I'm doing something wrong with it in my script.  I have my script attached as a text file, I nornally like to figure this out, but every time I try to implement this I fail at it.  Would you show me in the script how this should work?  Obviously it's no rush, I'm still working on the script anyway, along with my other duties.  Thanks again.

Reply
0 Kudos