Automation

 View Only
Expand all | Collapse all

sending report as an attachment after exporting into CSV format

  • 1.  sending report as an attachment after exporting into CSV format

    Posted Sep 29, 2020 06:13 AM

    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



  • 2.  RE: sending report as an attachment after exporting into CSV format

    Posted Sep 29, 2020 06:22 AM

    Use the Send-MailMessage cmdlet and the Attachments parameter.



  • 3.  RE: sending report as an attachment after exporting into CSV format

    Posted Sep 29, 2020 06:31 AM

    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



  • 4.  RE: sending report as an attachment after exporting into CSV format

    Posted Sep 29, 2020 06:37 AM

    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.



  • 5.  RE: sending report as an attachment after exporting into CSV format

    Posted Sep 29, 2020 06:45 AM

    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



  • 6.  RE: sending report as an attachment after exporting into CSV format

    Posted Sep 29, 2020 06:53 AM

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



  • 7.  RE: sending report as an attachment after exporting into CSV format

    Posted Sep 29, 2020 07:09 AM

    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'

    Thanks

    V



  • 8.  RE: sending report as an attachment after exporting into CSV format

    Posted Sep 29, 2020 07:17 AM

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



  • 9.  RE: sending report as an attachment after exporting into CSV format

    Posted Sep 29, 2020 07:32 AM

    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



  • 10.  RE: sending report as an attachment after exporting into CSV format

    Posted Sep 29, 2020 07:37 AM

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



  • 11.  RE: sending report as an attachment after exporting into CSV format

    Posted Sep 29, 2020 07:43 AM

    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



  • 12.  RE: sending report as an attachment after exporting into CSV format
    Best Answer

    Posted Sep 29, 2020 07:49 AM

    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'



  • 13.  RE: sending report as an attachment after exporting into CSV format

    Posted Sep 29, 2020 07:56 AM

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

    Thanks

    V