10 Replies Latest reply on Nov 5, 2019 10:14 PM by vmk2014

    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
          vExpertUser Moderators

          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
              User ModeratorsvExpertCommunity Warriors

              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

               

              ---------------------------------------------------------------------------------------------------------

               

              Was it helpful? Let us know by completing this short survey here.

               

               

              Blog: http://lucd.info | Twitter: @LucD22 | PowerCLI Reference co-author: http://tinyurl.com/hkn4glz
              • 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
                  Community WarriorsUser ModeratorsvExpert

                  I updated the code above, try like that.

                  Blog: http://lucd.info | Twitter: @LucD22 | PowerCLI Reference co-author: http://tinyurl.com/hkn4glz
                  • 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
                      vExpertCommunity WarriorsUser Moderators

                      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

                      Blog: http://lucd.info | Twitter: @LucD22 | PowerCLI Reference co-author: http://tinyurl.com/hkn4glz
                      • 8. Re: Diskconsolidation send email as HTML is blank
                        vmk2014 Hot Shot

                        LucD,

                         

                        Can you help me here to fix below error

                         

                        Send-mailmessage : The specified string is not in the form required for an e-mail address.

                        At D:\nilay\Disk-consolidation.ps1:59 char:1

                        + Send-mailmessage @sMail

                        + ~~~~~~~~~~~~~~~~~~~~~~~

                            + CategoryInfo          : InvalidType: (:) [Send-MailMessage], FormatException

                            + FullyQualifiedErrorId : FormatException,Microsoft.PowerShell.Commands.SendMailMessage

                         

                         

                        Send-mailmessage : A recipient must be specified.

                        At D:\nilay\Disk-consolidation.ps1:59 char:1

                        + Send-mailmessage @sMail

                        + ~~~~~~~~~~~~~~~~~~~~~~~

                            + CategoryInfo          : InvalidOperation: (System.Net.Mail.SmtpClient:SmtpClient) [Send-MailMessage], InvalidOpe

                           rationException

                            + FullyQualifiedErrorId : InvalidOperationException,Microsoft.PowerShell.Commands.SendMailMessage

                         

                        Thanks

                        vmk

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

                          I would need to see the value you provided on the To and From parameter.
                          It should be of the format user@domain.

                          Blog: http://lucd.info | Twitter: @LucD22 | PowerCLI Reference co-author: http://tinyurl.com/hkn4glz
                          • 10. Re: Diskconsolidation send email as HTML is blank
                            vmk2014 Hot Shot

                            Hi LucD,

                             

                            It was my mistake. It's fixed now.

                             

                            Thanks

                            vmk