Posts

Showing posts with the label outlook

Use PowerShell to send mail via Outlook along with SQL result csv as attachment

Image
Introduction In this post we will create a simple utility to perform these tasks:  Execute a SQL script stored as .sql file in a local directory. Save the result set as .csv. Use outlook application configured on the local machine to send the .csv as an attachment to email recipients.  We will be using PowerShell script. Lets prepare the .sql script. For this utility purpose we will make use of a very simple query. Below is the code for the .ps1 which will perform the tasks we mentioned above. $global:toMailList         = "toEmail@domain.com" ; $global:ccMailList          = "cc Email1@domain.com ; ccEmail2@domain.com " ; $global:MailSubject       = "PS outlook Test Email" ; $global:MailBody           = "Sent using Powershell via Outlook Client" ; $global:SQLScriptPath = "C:\...\SQLInputFile.sql" ; $global:Attachment       = "C:\...\TestOut...