Sample PowerShell scripts

Below are two sample PowerShell scripts to be used for delivering OTP via e-mail.

PowerShell script using Send-MailMessage - we name the file as sendmail.ps1

example

Example

param

(

 [string] $toAddress,

 [string] $otp

)

$smtpServer = "<server>"

$smtpPort = "<port>"

$smtpUsername = "<username>"

$smtpPassword = "<password>"

 

$fromAddress = "esa@localhost"

$subject = "ESA OTP"

$body = "Your OTP: $otp"

 

$smtpPassword_sec = ConvertTo-SecureString $smtpPassword -AsPlainText -Force

$credential = New-Object System.Management.Automation.PSCredential ($smtpUsername, $smtpPassword_sec)

 

Send-MailMessage -SmtpServer $smtpServer -Port $smtpPort -Credential $credential -UseSsl -From $fromAddress -To $toAddress -Subject $subject -Body $body

PowerShell script using System.Net.Mail - we name the file as sendmail.ps1

example

Example

param

(

 [string] $toAddress,

 [string] $otp

)

$smtpServer = "<server>"

$smtpPort = "<port>"

$smtpUsername = "<username>"

$smtpPassword = "<password>"

 

$fromAddress = "esa@localhost"

$subject = "ESA OTP"

$body = "Your OTP: $otp"

 

$mailMessage = New-Object System.Net.Mail.MailMessage($fromAddress, $toAddress, $subject, $body)

$smtpClient = New-Object System.Net.Mail.SmtpClient($smtpServer, $smtpPort) 

$smtpClient.EnableSsl = $true 

$smtpClient.Credentials = New-Object System.Net.NetworkCredential($smtpUsername, $smtpPassword); 

$smtpClient.Send($mailMessage)

note

Note

In the sample scripts above, replace the <server>, <port>, <username> and <password> placeholders with the corresponding SMTP details.

Test and use

1.Save the script in a desired location, for example c:\work\sendmail.ps1

2.Test the script outside of ESET Secure Authentication (ESA) using Windows command line:

a.Press Windows key + R key combination.

b.Type in cmd.exe, press Enter.

c.In the command line window, execute:

powershell c:\scripts\sendmail.ps1 test@address.com 123456

while test@address.com is supposed to be replaced with a valid email address, you can read its inbox.

d.If the test is successful, proceed with the next step.

3.In the Delivery Options section of ESA, refer to the script this way:

powershell c:\scripts\sendmail.ps1 [E-mail-Addresses] [OTP]