Last active
October 26, 2022 04:14
-
-
Save laymanstake/5f2e97f844ffee4a8578f16387c85557 to your computer and use it in GitHub Desktop.
PowerShell Script, for sending Email alerts
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
# Author : Nitish Kumar | |
# Email Alert Function | |
# version 1.0 | 19/10/2022 | |
# Function to send email | |
function EmailAlert () { | |
[CmdletBinding()] | |
param( | |
[parameter(mandatory=$true)]$RecipientAddressTo, | |
[parameter(mandatory=$true)]$SenderAddress, | |
[parameter(mandatory=$true)]$SMTPServer, | |
[parameter(mandatory=$true)]$Subject, | |
[parameter(mandatory=$true)]$Body, | |
[parameter(mandatory=$false)]$SMTPServerPort="25", | |
[parameter(mandatory=$false)]$RecipientAddressCc, | |
[parameter(mandatory=$false)][switch]$SMTPServerSSL=$false | |
) | |
if ($RecipientAddressCc){ | |
try{ | |
$email = @{ | |
From = $SenderAddress | |
To = $RecipientAddressTo | |
Cc = $RecipientAddressCc | |
Subject = $Subject | |
Body = $Body | |
SmtpServer = $SMTPServer | |
Port = $SMTPServerPort | |
UseSsl = $SMTPServerSSL | |
} | |
Send-MailMessage @email | |
} | |
Catch{ | |
Throw $_.exception.message | |
} | |
} | |
else { | |
try{ | |
$email = @{ | |
From = $SenderAddress | |
To = $RecipientAddressTo | |
Subject = $Subject | |
Body = $Body | |
SmtpServer = $SMTPServer | |
Port = $SMTPServerPort | |
UseSsl = $SMTPServerSSL | |
} | |
Send-MailMessage @email | |
} | |
Catch{ | |
Throw $_.exception.message | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment