send multiple report attachment

This post was originally published on this site

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_DMy DocumentsVMwareScriptCPReportCPReport-v2.1.ps1:920 char:1

+ $message.Attachments.Add($att)

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

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

    + FullyQualifiedErrorId : ArgumentNullException

 

Thanks for all the help.

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.