Skip to content

Instantly share code, notes, and snippets.

@iqwirty
Created April 16, 2014 19:44
Show Gist options
  • Save iqwirty/10925261 to your computer and use it in GitHub Desktop.
Save iqwirty/10925261 to your computer and use it in GitHub Desktop.
Sends an email from a script with an optional attachment. Includes path of currently running script in body.
Function EmailScriptReport
{
# Sends an email using the specified to/from, subject, body, and
# and SMTP server. Allows an attachment to be included. Also includes
# in the body the source path of the running script to improve
# traceability.
Param (
[string]$To,
[string]$From,
[string]$Server,
[string]$Subject,
[string]$Body,
[string]$AttachmentFullPath,
[string]$EmailSource
)
$parentInvocation = (Get-Variable MyInvocation -Scope 1).Value
$formattedBody = "$Body`r`n`r`nSent by a script at:`r`n$($parentInvocation.ScriptName)"
Send-MailMessage -To $To `
-From $From `
-SmtpServer $Server `
-Subject $Subject `
-Body $formattedBody `
-Attachments $AttachmentFullPath
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment