Send-MailMessage -Body is blank or partial

Hi There,

I’m really new with PowerShell, and I’m trying to do what seems like a very simple task but for the life of me cannot wrap my head around the behavior.

All I am trying to do is connect to one of our VCenter servers, query the datastore information and send the output to us via email.

  • $datastore = get-datastore
  • Send-MailMessage -To mailbox@ourdomain.com -From vcenter@server.local -Subject Test -BodyAsHTML -Body "$datastore" -SmtpServer exchangeserver@server.local -Credential DOMAIN\UserID
I get the email but it only lists the one column (datastore names) and puts only that in the body of the email. If I just type $datastore by itself, I get all the data including the free space/available space which is what I want. All of my Google searches keeps hinting around something to do with HTML vs. plain-text as they relate to tables etc... but adding or removing the "-BodyAsHTML" does not impact this.

Can anyone see what I’m missing from my 2 lines here? Any help would be appreciated.

 

Thanks!

 

You specify the parameter -BodyAsHtml. Does the cmdlet get-datastore outputs html formatted data? If not you should convert the output of the cmdlet to valid html.

[quote quote=188878]You specify the parameter -BodyAsHtml. Does the cmdlet get-datastore outputs html formatted data? If not you should convert the output of the cmdlet to valid html.

[/quote]

Hi! Thanks for the suggestion. I’m not 100% certain on the proper syntax of that commandlet or really more so where to insert it. I seemed to have received the same output in the email using the covertto-html command or not.

To answer your question - It doesn’t look like the output of get-datastore is in HTML at all, so I might just be going down a bunny trail thinking it had something to do with HTML or not. Thanks again for the suggestion!

I think you have two options. Either you omit the parameter -BodyAsHtml of Send-MailMessage and send the data as plain text or you figure out how to use the cmdlet ConvertTo-Html. You should always read the complete help including the examples for every cmdlet you’re about to use to learn how to use it.

Thanks for the suggestion. I’ll go figure out how to use the cmdlet. That should help.

You might have not noticed that - you can omit the parameter -BodyAsHtml and send the data as plain text. Do you really need it formatted as html?

You are returning an object, so it would simpler to use ConvertTo-HTML to convert it into a table. Passing credentials is only required if the SMTP server is not setup as anonymous, but if it does require credentials it need to be a credential object. Lastly, a kitten is killed every time you don’t use splatting for long commands in Powershell :slight_smile:

$datastore = Get-DataStore
$datastoreHtml = $datastore | ConvertTo-Html


$secpasswd = ConvertTo-SecureString "PlainTextPassword" -AsPlainText -Force
$creds = New-Object System.Management.Automation.PSCredential ("DOMAIN\UserID", $secpasswd)

$sendParams = @{
    To         = 'mailbox@ourdomain.com'
    From       = 'vcenter@server.local'
    Subject    = 'Test'
    BodyAsHTML = $true
    Body       = ($datastoreHtml | Out-String)
    SmtpServer = 'exchangeserver@server.local'
    Credential = $creds
}

Send-MailMessage @sendParams

Also, there is a free e-book in the Free Resources link on the left, which can assist you in understanding ConvertTo-HTML.

I have found that when I do not use splatting as Rob suggests, I get odd results with the Send-MailMessage command in PowerShell. Just something to look at if you do not choose to follow the recommendation and experience inconsistencies.

Thank you Sir! I really appreciate your help with this. This worked perfectly. Now that I have this, I can tweak it to use it in other ways. I appreciate you giving me what I need along with resource suggestion. Much more helpful than the old Linux-Forum “GO RTFM” response I’ve been getting.