Highlighted
Contributor
Contributor

send multiple report attachment

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.

0 Kudos
4 Replies
Highlighted
User Moderator
User Moderator

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


Blog: lucd.info  Twitter: @LucD22  Co-author PowerCLI Reference

0 Kudos
Highlighted
Contributor
Contributor

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

0 Kudos
Highlighted
User Moderator
User Moderator

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.


Blog: lucd.info  Twitter: @LucD22  Co-author PowerCLI Reference

0 Kudos
Highlighted
Contributor
Contributor

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

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

Thanks

0 Kudos