Send-MailMessage -Body is blank or partial

Welcome Forums General PowerShell Q&A Send-MailMessage -Body is blank or partial

Viewing 6 reply threads
  • Author
    Posts
    • #188872
      Participant
      Topics: 1
      Replies: 3
      Points: -1
      Rank: Member

      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.

      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!

       

    • #188878
      Participant
      Topics: 1
      Replies: 1632
      Points: 3,074
      Helping Hand
      Rank: Community Hero

      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.

    • #188881
      Participant
      Topics: 1
      Replies: 3
      Points: -1
      Rank: Member

      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.

      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!

    • #188887
      Participant
      Topics: 1
      Replies: 1632
      Points: 3,074
      Helping Hand
      Rank: Community Hero

      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.

      • #189127
        Participant
        Topics: 1
        Replies: 3
        Points: -1
        Rank: Member

        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.

    • #189133
      Participant
      Topics: 1
      Replies: 1632
      Points: 3,074
      Helping Hand
      Rank: Community Hero

      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?

    • #189157
      Participant
      Topics: 8
      Replies: 1274
      Points: 1,033
      Helping Hand
      Rank: Community Hero

      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 🙂

      $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.

      • #189778
        Participant
        Topics: 1
        Replies: 3
        Points: -1
        Rank: Member

        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.

    • #189166
      Participant
      Topics: 3
      Replies: 43
      Points: 77
      Rank: Member

      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.

Viewing 6 reply threads
  • You must be logged in to reply to this topic.