Created
September 3, 2015 19:38
-
-
Save deed02392/b5d537dc0b1b42665edb to your computer and use it in GitHub Desktop.
PowerShell cmdlet that sends an e-mail via Outlook, with address and subject as arguments and message body from pipe
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Function Global:Send-Email { | |
[cmdletbinding()] | |
Param ( | |
[Parameter(Mandatory=$True,Position=0)] | |
[String]$Address, | |
[Parameter(Mandatory=$True,Position=1)] | |
[String]$Subject, | |
[Parameter(ValueFromPipeline=$True)] | |
[String[]]$BodyLine | |
) | |
Begin { | |
$Body = "" | |
} | |
Process { | |
$Body += "$BodyLine`r`n" | |
} | |
End { | |
$Outlook = New-Object -ComObject Outlook.Application | |
$Mail = $Outlook.CreateItem(0) | |
$Mail.BodyFormat = 1 | |
$Mail.To = "$Address" | |
$Mail.Subject = "$Subject" | |
$Mail.Body = "$Body" | |
$Mail.Send() | |
[System.Runtime.Interopservices.Marshal]::ReleaseComObject($Outlook) | |
$Outlook = $null | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment