PowerShell and Alerting using SMTP

12/16/2022 7:12 PM
 
Every Sysop or System Admin has had the tedious task of doing something that bores us to tears everyday. Most of the time, we attempt to automate these tasks using a scripting language, such as Powershell, VBscript, and even Python. The script would catch and error and execute the lines of code below. This example is in Powershell.

Please note that I am using Google for the public SMTP server, however in your environment use your smart host which is normally your Exchange server, an IIS instance using the SMTP node. I am using port 587 on line (6), however it is also possible to use the standard SMTP port 25. 
PowerShell

# Send message Alerting action needs to be taken

$message = New-Object Net.Mail.MailMessage;
$message.From = "me@datacenter.com";
$message.To.Add($email);
$message.subject = "Sending alert for process 13";
$message.Body = "This is a critical Alert - Action Required";
# use your smtp smart node or relay

$smtp = New-Object Net.Mail.SmtpClient("smtp.google.com", 587);
$smtp = EnableSSL = $true;
$smtp.credentials = New-Object System.Net.NetworkCredential($Username, $Password);
$smtp.Send($message);

Comments

Popular Posts