7 Replies Latest reply on Aug 13, 2019 11:46 PM by LucD

    Diskconsolidation send email as HTML is blank

    VijayKumarMyadam Enthusiast

      Hello !

       

      am trying to gathers list of vms in a vc and send an email as HTML, i see script is executing but when it is sending report is blank.

      help me in correcting this.

       

      # Get date in UK format day/month/year

      $date = Get-Date -Format dd/MM/yy

       

      $Header = @"

      <style>

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

      TH {border-width: 1px;padding: 3px;border-style: solid;border-color: black;background-color: #6495ED;}

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

      </style>

      "@

       

      Connect-VIServer -Server vc -User -u admin -p admin

      $Report = Get-VM | Where-Object {$_.Extensiondata.Runtime.ConsolidationNeeded} | Select Name, PowerState, UsedSpaceGB | ConvertTo-HTML -Head $Header

      # Send email message

      Send-mailmessage -to "xyz@abc.com" -from "zyx@abc.com" -subject "Disk consolidation needed" -body "Please find below vms requried disk consolidation , take necessary action." -BodyAsHtml -SmtpServer "10.1.2.3"

      =============================

       

      in email am receiving without data

       

        • 1. Re: Diskconsolidation send email as HTML is blank
          RvdNieuwendijk Virtuoso
          User ModeratorsvExpert

          You should use $Report as the value of the Send-Mailmessage Body parameter:

          Send-mailmessage -to "xyz@abc.com" -from "zyx@abc.com" -subject "Disk consolidation needed" -body $Report -BodyAsHtml -SmtpServer "10.1.2.3"

          • 2. Re: Diskconsolidation send email as HTML is blank
            ganapa2000 Hot Shot

            try like this,

             

            # Get date in UK format day/month/year

            $date = Get-Date -Format dd/MM/yy

             

            $Header = @"

            <style>

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

            TH {border-width: 1px;padding: 3px;border-style: solid;border-color: black;background-color: #6495ED;}

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

            </style>

            "@

             

            Connect-VIServer -Server navc -User -u admin -p admin

            $body += "Please find below vms requried disk consolidation , take necessary action. <br>"

            $body += Get-VM | Where-Object {$_.Extensiondata.Runtime.ConsolidationNeeded} | Select Name, PowerState, UsedSpaceGB | ConvertTo-HTML -Head $Header

             

            # Send email message

            Send-mailmessage -to "xyz@abc.com" -from "zyx@abc.com" -subject "Disk consolidation needed" -body $body -BodyAsHtml -SmtpServer "10.1.2.3"

            • 3. Re: Diskconsolidation send email as HTML is blank
              LucD Guru
              Community WarriorsUser ModeratorsvExpert

              The Body parameter of the Send-MailMessage cmdlet, expects a single string, not an array of strings.

              You need to have an Out-String after the ConvertTo-Html.

               

              Something like this.

              I use splatting for the Send-MailMessage cmdlet, this to not have a long line.

               

               

              # Get date in UK format day/month/year

               

              $date = Get-Date -Format dd/MM/yy

              $Header = @"

              <style>

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

              TH {border-width: 1px;padding: 3px;border-style: solid;border-color: black;background-color: #6495ED;}

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

              </style>

              "@


              Connect-VIServer -Server vc -User -u admin -p admin

              $Report = Get-VM |

              Where-Object { $_.Extensiondata.Runtime.ConsolidationNeeded } |

              Select Name, PowerState, UsedSpaceGB |

              ConvertTo-HTML -Head $Header | Out-String

              # Send email message


              $sMail = @{

                 To = "xyz@abc.com"

                 From = "xyz@abc.com"

                 Subject = "Disk consolidation needed"

                 Body = $report

                 BodyAsHtml = $true

                 SmtpServer = "10.1.2.3"

              }


              Send-mailmessage @sMail

              • 4. Re: Diskconsolidation send email as HTML is blank
                VijayKumarMyadam Enthusiast

                Lucd, i received output like this

                 

                • 5. Re: Diskconsolidation send email as HTML is blank
                  LucD Guru
                  User ModeratorsCommunity WarriorsvExpert

                  I updated the code above, try like that.

                  • 6. Re: Diskconsolidation send email as HTML is blank
                    VijayKumarMyadam Enthusiast

                    in output if no vms are applicable email body should contain no vms consolidation is required, this is missing in the script, how do we can achieve that by adding else

                    • 7. Re: Diskconsolidation send email as HTML is blank
                      LucD Guru
                      User ModeratorsvExpertCommunity Warriors

                      Try like this

                       

                      # Get date in UK format day/month/year

                      $date = Get-Date -Format dd/MM/yy

                      $Header = @"

                      <style>

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

                      TH {border-width: 1px;padding: 3px;border-style: solid;border-color: black;background-color: #6495ED;}

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

                      </style>

                      "@


                      #Connect-VIServer -Server vc -User -u admin -p admin

                      $report = Get-VM |

                         Where-Object { $_.Extensiondata.Runtime.ConsolidationNeeded } |

                        Select Name, PowerState, UsedSpaceGB

                      if($report -ne $null){

                         $report = $report | ConvertTo-HTML -Head $Header | Out-String

                      }

                      else{

                         $report = 'No VMs disk consolidation is required'

                      }


                      # Send email message

                      $sMail = @{

                         To = "xyz@abc.com"

                         From = "xyz@abc.com"

                         Subject = "Disk consolidation needed"

                         Body = $report

                         BodyAsHtml = $true

                         SmtpServer = "10.1.2.3"

                      }


                      Send-mailmessage @sMail