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.

  1. The Execute Custom Script action allows for the one-time execution of the script.
  2. 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.

Disclaimer:

The sample scripts provided below are adapted from third-party open-source sites.

PowerShell script to send email message

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.
Script to send email messages on Windows devices

Notes:

  • It is recommended to manually validate the script execution on a system before executing the action in bulk.
  • Hexnode will not be responsible for any damage/loss to the system on the behavior of the script.

  • Sample Script Repository