WORKLET NOTIFICATION :: Send an email alert when a worklet is evaluated

  • 10 March 2020
  • 1 reply
  • 175 views

While putting together a few Automox Worklets, I found I wanted a quick way to tell whether a worklet had been processed by a remote agent (vs. combing through logs 😇).


I cobbled together a hasty email snippet to drop into the Evaluation Code window (this prolly should be placed before any real evaluation code block to ensure exit codes don’t overstep this snippet).


🙈🙉🙊


Example email:


image

Evaluation code:


<# Grab some system variables to pump into the email alert (as an example). #>

$Hostname = $env:COMPUTERNAME.ToUpper()
$HostDomain = $env:USERDOMAIN.ToUpper()
$HostWindowsVersion = (Get-WmiObject -class Win32_OperatingSystem).Caption.ToUpper()
$HostWindowsBuild = (Get-WmiObject -class Win32_OperatingSystem).Version.ToUpper()

<# Build a message body. #>

$smtpHTMLBody =
@"
<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8">
<style>
table {
font-family: "Trebuchet MS", Arial, Helvetica, sans-serif;
font-size:1em;
text-align:Left;
border-collapse: collapse;
width: 50%;
}
#p01 {
color: #C95D42;
font-family: "Trebuchet MS", Arial, Helvetica, sans-serif;
font-size: 1em;
font-weight: bold;
}
#p02 {
color: #FFFFFF;
font-family: "Trebuchet MS", Arial, Helvetica, sans-serif;
font-size: 0.1em;
}
#p03 {
color: #767676;
font-family: "Trebuchet MS", Arial, Helvetica, sans-serif;
font-size: 0.5em;
}
td {
border:1px solid #98bf21;
padding-top:5px;
padding-bottom:5px;
padding-right:7px;
padding-left:7px;
}
td#td01 {
font-weight: bold;
color:#ffffff;
padding-top:5px;
padding-bottom:5px;
padding-right:7px;
padding-left:7px;
background-color:#A7C942;
}
</style>
</head>
<body>
<p id="p01">Alert Message:</p>
<table>
<tr>
<td id="td01">Host:</td>
<td>$Hostname</td>
</tr>
<tr>
<td id="td01">Domain:</td>
<td>$HostDomain</td>
</tr>
<tr>
<td id="td01">Windows Version:</td>
<td>$HostWindowsVersion</td>
</tr>
<tr>
<td id="td01">Windows Build:</td>
<td>$HostWindowsBuild</td>
</tr>
</table>
<p id="p02">hidden value used to filter message</p>
<p id="p03">Powered by Automox. :)</p>
</body>
</html>
"@

<# Declare message components. #>

$smtpFrom = "Worklet Alerts <worklet_alerts@domain.com>"
$smtpTo = "Worklet Admin <worklet_admin@domain.com>"
$smtpSubject = "Automox Alert :: " + $env:COMPUTERNAME + " :: " + (Get-Date).ToString("MM/dd/yy HH:mm")
$SMTPServer = "smtp.domain.com"
$SMTPPort = "587"
$smtpUser = "messenger@domain.com"
$smtpPass = "Sup37looo000oong&kindaC0mpLeX"
$smtpSSPass = $smtpPass | ConvertTo-SecureString -AsPlainText -Force
$smtpCreds = New-Object System.Management.Automation.PSCredential -ArgumentList $smtpUser,$smtpSSPass

<# Build and send the message - written to support multiple versions of Powershell. #>

if ($PSVersionTable.PSVersion.Major -ge 3) {
$email = @{
From = $smtpFrom
To = $smtpTo
Subject = $smtpSubject
BodyAsHtml = $True
Body = $smtpHTMLBody
#attachment = $path
smtpServer = $SMTPServer
Port = $smtpPort
UseSsl = $True
Credential = $smtpCreds
}
send-MailMessage @email
} else {
$emailMessage = New-Object System.Net.Mail.MailMessage
$emailMessage.From = $smtpFrom
$emailMessage.To.Add( $smtpTo )
$emailMessage.Subject = $smtpSubject
$emailMessage.IsBodyHtml = $true
$emailMessage.Body = $smtpHTMLBody
$SMTPClient = New-Object Net.Mail.SmtpClient($SMTPServer,$SMTPPort)
$SMTPClient.EnableSsl = $true
$SMTPClient.Credentials = New-Object System.Net.NetworkCredential($smtpUser, $smtpPass);
$SMTPClient.Send($emailMessage)
}

1 reply

Userlevel 5
Badge

@jermicide Wow, this is really impressive, and a great idea! I know a few people that would start using this immediately.


Thanks for sharing and contributing to the community!

Reply