VMware Cloud Community
vmk2014
Expert
Expert
Jump to solution

sending report as an attachment after exporting into CSV format

Hi All,

I want to send the report over email as an attachment after exporting the report into csv format. Any pointers or help will be much appreciated.

Note:- I'll send the data store report on a regular basis through window tasks scheduler.

Get-Datastore |

Select-Object Name,

    @{N = ”FreespaceGB”; E = { “{0:n2}” -f ($_.FreespaceGB) } },

    CapacityGB,

    @{N = ”Provisioned”; E = { “{0:n2}” -f ($_.CapacityGB – $_.FreespaceGB + ($_.extensiondata.summary.uncommitted / 1GB)) } },

    @{N = 'Used Space(%)'; E = { [math]::Round((($_.CapacityGB - $_.FreeSpaceGB) / $_.CapacityGB * 100), 1) } } |

Export-Csv -path c:\Temp\dsreport.csv -NoTypeInformation -UseCulture

Send-MailMessage -From 'User01 ' -To 'autoreports@xyz.com ', 'User03 ' -Subject 'Sending the Attachment' -Body "Forgot to send the attachment. Sending now." -Attachments .\C:\Temp\DSreport.csv -Priority High -DeliveryNotificationOption OnSuccess, OnFailure -SmtpServer 'smtp.fabrikam.com'

Thanks

V

0 Kudos
1 Solution

Accepted Solutions
LucD
Leadership
Leadership
Jump to solution

There is a blank missing before the -Priority parameter.
And the Attachments filename is incorrect.


Send-MailMessage -From 'suresh@mydomain.com ' -To 'Corp@mydomain.com ' -Subject 'DataStore Free space' -Body "Forgot to send the attachment. Sending now." -Attachments 'C:\Temp\DSreport.csv' -Priority High -DeliveryNotificationOption OnSuccess, OnFailure -SmtpServer 'smtp.ka.mydomain.com'


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

View solution in original post

0 Kudos
12 Replies
LucD
Leadership
Leadership
Jump to solution

Use the Send-MailMessage cmdlet and the Attachments parameter.


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

0 Kudos
vmk2014
Expert
Expert
Jump to solution

LucD,

I have updated the code, just want to inform that i need to send mail as attachment to shared mail box. Also, can we send mail directly as an attachment instead of saving the CSV. I need help in both the cases.

Thanks

V

0 Kudos
LucD
Leadership
Leadership
Jump to solution

I'm afraid you can't send a file attachment without creating a file.

A shared mailbox also has an email address, use that on the To parameter of the Send-MailMessage cmdlet.


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

0 Kudos
vmk2014
Expert
Expert
Jump to solution

LucD,

I tried the above code, but it throws an error. output is getting generated on path C:\temp\DSreport.

PS C:\Windows\system32> D:\abc\CI-Tested\Datastore-freespace.ps1

Send-MailMessage : The given path's format is not supported.

At D:\abc\CI-Tested\Datastore-freespace.ps1:14 char:1

+ Send-MailMessage -From 'rvk@abc.com ' -To 'Ccor ...

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

    + CategoryInfo          : NotSpecified: (:) [Send-MailMessage], NotSupportedException

    + FullyQualifiedErrorId : System.NotSupportedException,Microsoft.PowerShell.Commands.SendMailMessage

thanks

V

0 Kudos
LucD
Leadership
Leadership
Jump to solution

I would need to see the full Send-MailMessage line to have an idea


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

0 Kudos
vmk2014
Expert
Expert
Jump to solution

Thank you. LucD.

Here you go. Let me know if you still more info.

Send-MailMessage -From 'Suresh@mydomain.com ' -To 'Corp@mydomain.com ' -Subject 'DataStore Free space' -Body "Test. Sending now." -Attachments .\C:\Temp\DSreport.csv -Priority High -DeliveryNotificationOption OnSuccess, OnFailure -SmtpServer 'smtp.ka.domain.com'

pastedImage_0.png

Thanks

V

0 Kudos
LucD
Leadership
Leadership
Jump to solution

That path doesn't seem correct.
It should probably be C:\Temp\DSreport.csv


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

0 Kudos
vmk2014
Expert
Expert
Jump to solution

LucD,

You are absolutely right. The path was wrong. my apology. Now new error prompting..

PS C:\Windows\system32> D:\abc\DI-Tested\Datastore-freespace.ps1

Send-MailMessage : A positional parameter cannot be found that accepts argument 'High'.

At D:\abc\DI-Tested\Datastore-freespace.ps1:14 char:1

+ Send-MailMessage -From 'Suresh@mydomain.com ' -To 'Corp@mydomain ...

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

    + CategoryInfo          : InvalidArgument: (:) [Send-MailMessage], ParameterBindingException

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

Thanks

V

0 Kudos
LucD
Leadership
Leadership
Jump to solution

Again, without seeing the actual code I really can't determine what goes wrong


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

0 Kudos
vmk2014
Expert
Expert
Jump to solution

LucD,

please find the full code using to run the report.

Get-Datastore |

Select-Object Name,

    @{N = ”FreespaceGB”; E = { “{0:n2}” -f ($_.FreespaceGB) } },

    CapacityGB,

    @{N = ”Provisioned”; E = { “{0:n2}” -f ($_.CapacityGB – $_.FreespaceGB + ($_.extensiondata.summary.uncommitted / 1GB)) } },

    @{N = 'Used Space(%)'; E = { [math]::Round((($_.CapacityGB - $_.FreeSpaceGB) / $_.CapacityGB * 100), 1) } } |

Export-Csv -path c:\Temp\DSreport.csv -NoTypeInformation -UseCulture

Send-MailMessage -From 'suresh@mydomain.com ' -To 'Corp@mydomain.com ' -Subject 'DataStore Free space' -Body "Forgot to send the attachment. Sending now." -Attachments .C:\Temp\DSreport.csv-Priority High -DeliveryNotificationOption OnSuccess, OnFailure -SmtpServer 'smtp.ka.mydomain.com'

Even when i removed the -Priority High parameter then it says.

Send-MailMessage : Cannot find drive. A drive with the name '.C' does not exist.

Thanks

V

0 Kudos
LucD
Leadership
Leadership
Jump to solution

There is a blank missing before the -Priority parameter.
And the Attachments filename is incorrect.


Send-MailMessage -From 'suresh@mydomain.com ' -To 'Corp@mydomain.com ' -Subject 'DataStore Free space' -Body "Forgot to send the attachment. Sending now." -Attachments 'C:\Temp\DSreport.csv' -Priority High -DeliveryNotificationOption OnSuccess, OnFailure -SmtpServer 'smtp.ka.mydomain.com'


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

0 Kudos
vmk2014
Expert
Expert
Jump to solution

Thank you LucD. After posting, i also noticed it, but kudos to you.

Thanks

V

0 Kudos