Category filter
PowerShell script to send email notification
The document explains a PowerShell script to send an email message to different users.
The script can be used to automate sending email notifications, which is particularly useful in various administrative tasks where you need to send alerts, status updates, or system information directly to an email address.
Hexnode provides various methods for executing such scripts.
- The Execute Custom Script action allows for the one-time execution of the script.
- The Deployments feature in Hexnode allows the scheduling of script execution.
While executing the script directly using the action sends the email to the specified users instantaneously, scheduling it using the deployments can help facilitate the automatic dispatch of emails at a predefined time without the need for admin’s intervention.
PowerShell script to send email message
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 |
$smtpServer = "Server" $smtpPort = 587 $smtpUser = "user email" $smtpPassword = "password" $from = "email id of sender" $to = @ (“email id 1”, “email id 2”) $subject = "subject for email" $body = "Body of the email" $securePassword = New-Object -TypeName System.Security.SecureString $smtpPassword.ToCharArray() | ForEach-Object {$securePassword.AppendChar($_)} $credential = New-Object System.Management.Automation.PSCredential($smtpUser, $securePassword) Send-MailMessage -SmtpServer $smtpServer -Port $smtpPort -Credential $credential -From $from -To $to -Subject $subject -Body $body -UseSsl |
The Send-MailMessage cmdlet is used to send the email, with all parameters passed securely, ensuring that the email is sent over an encrypted connection.
You define the necessary parameters: SMTP server details, sender and recipient information, subject, and body of the email.
$smtpServer is used to store the address of the SMTP server that will be used to send the email. It should be the address of your SMTP server (e.g., smtp.gmail.com, smtp-mail.outlook.com).
$smtpPort defines the port to use for the SMTP server. Port 587 is commonly used for sending email over TLS (secure connection).
$smtpUser: This variable stores the email address (sender’s email ID) used to authenticate to the SMTP server.
$smtpPassword: This variable stores the password used for authentication.
The plain-text password is securely converted into a SecureString and used to create a PSCredential object that is required for SMTP authentication.
With this script, the receiver will get an email received in their inbox.