Automation

 View Only
  • 1.  send multiple report attachment

    Posted Oct 22, 2020 04:05 PM

    Hi all,

    Need help to modify sending email for report using .net method that can send multiple attachment

    this is the original script : PowerCLI-1/CPReport-v2.1-Community-Edition.ps1 at master · voletri/PowerCLI-1 · GitHub

    ################################# PER CLUSTER RESILIENCE REPORT ##########################

    # HEADER

    $HTMLBody += CreateHeader ("CLUSTER RESILIENCE REPORT FOR <font color=" + $ColorArray[$ColorArrayIndex] + "><b> CLUSTER " + $ClusterTemp.Name + "</b></font>")

    # INTRO TEXT

    $HTMLBody += "<b>" + [String]$VMHostsInCluster.Count + " ESXi Hosts with a total of " + (GetTotalCPUCyclesInGhz ($VMHostsInCluster)) + " GHz on " +

    (GetTotalNumberofCPUs ($VMHostsInCluster)) + " CPUs and " + (GetTotalMemoryInGB ($VMHostsInCluster)) + " GB of RAM </b><br>"

    $HTMLBody += "<b>" + [String]$DatastoresInCluster.Count + " Datastores with a total of " + (GetTotalDatastoreDiskSpaceinGB ($DatastoresInCluster)) + `

    " GB of disk space</b><br><br>"

    #TABLE

    $HTMLBody += CreateClusterResilienceTable ($ClusterTemp)

    # CHART

    if ($global:ArrayOfNames.count -gt 0){ # If HA Admission Control is set to a %, create a pie chart

    Create-Chart -ChartType Pie -ChartTitle "Cluster Resilience Report" -FileName ("Cluster_" + ($ClusterTemp.Name -replace " ", "-") + `

    "_Resilience_Report") -ChartWidth 850 -ChartHeight 750 -NameArray $global:ArrayOfNames -ValueArray $global:ArrayOfValues

    $HTMLBody += "<IMG SRC=Cluster_" + ($ClusterTemp.Name -replace " ", "-") + "_Resilience_Report.png>"

    $Attachments += "Cluster_" + ($ClusterTemp.Name -replace " ", "-") + "_Resilience_Report.png"

    }

    # CLEANUP

    ReinitializeArrays

    #-----------------------------------------------------------------------------------------------

    # Change the color of the Header for the next Cluster

    $ColorArrayIndex++

    }

    }

    ########################### SEND REPORT BY E-MAIL #################################

    $HTMLBody += "</BODY>" # Close HTML Body

    $HTMLPage = $HTMLHeader + $HTMLBody + $HTMLFooter

    $HTMLPage | Out-File ((Get-Location).Path + "\report.html") # Export locally to html file

    Write-Host "Report has exported to HTML file " + ((Get-Location).Path + "\report.html")

    Send-Mailmessage -From $FromAddress -To $ToAddress -Subject $Subject -Attachments $Attachments -BodyAsHTML -Body $HTMLPage -Priority Normal -SmtpServer $SMTPServer -UseSSL -Credential (Get-Credential)

    Write-Host "Report has been sent by E-mail to " $ToAddress " from " $FromAddress

    Exit

    What i already try is change to :

    ########################### SEND REPORT BY E-MAIL #################################

    $HTMLBody += "</BODY>" # Close HTML Body

    $HTMLPage = $HTMLHeader + $HTMLBody + $HTMLFooter

    $HTMLPage | Out-File ((Get-Location).Path + "\report.html") # Export locally to html file

    Write-Host "Report has exported to HTML file " + ((Get-Location).Path + "\report.html")

    $message = New-Object System.Net.Mail.MailMessage $FromAddress, $ToAddress

    $att = new-object Net.Mail.Attachment($Attachments)

    $message.Subject = $Subject

    $message.IsBodyHTML = $true

    $message.Body = $HTMLPage

    $smtp = New-Object Net.Mail.SmtpClient($SMTPServer)

    $message.Attachments.Add($att)

    $smtp.Send($message)

    Write-Host "Report has been sent by E-mail to " $ToAddress " from " $FromAddress

    It can send the email but not the attachments and have error

    Exception calling "Add" with "1" argument(s): "Value cannot be null.

    Parameter name: item"

    At D:\Drive_D\My Documents\VMware\Script\CPReport\CPReport-v2.1.ps1:920 char:1

    + $message.Attachments.Add($att)

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

        + CategoryInfo          : NotSpecified: (:) [], MethodInvocationException

        + FullyQualifiedErrorId : ArgumentNullException

    Thanks for all the help.



  • 2.  RE: send multiple report attachment

    Posted Oct 22, 2020 04:20 PM

    Why would you use that .NET method while you have the Send-MailMessage cmdlet?



  • 3.  RE: send multiple report attachment

    Posted Oct 23, 2020 08:44 AM

    If i used send-mailmessage it show the credential pop up, i don`t want that because i want to make task schedule from this script, and also i don`t want to store my password because the password will change every month. or may be another way that i can send this report automatically?

    Or maybe powershell can used current user? I running this task schedule using my user domain.

    In my other script i used .net method, i don`t need credential for send the email and i can running is using windows task scheduler



  • 4.  RE: send multiple report attachment

    Posted Oct 23, 2020 09:47 AM

    If your scheduled task runs under an account that has permissions to send emails and your SMTP server is not configured to always prompt for credentials, the Send-MailMessage shouldn't prompt.

    And I definitely do not understand why Send-MailMessage would prompt while the .NET method doesn't.
    They are basically using the same methods.



  • 5.  RE: send multiple report attachment

    Posted Oct 24, 2020 04:43 AM

    This part i think i need to check first with the mail guy.

    "SMTP server is not configured to always prompt for credentials,"

    Thanks