Home POWERSHELL Get notified via e-mail about your server health with Powershell

Get notified via e-mail about your server health with Powershell

by Cengiz Kuskaya

Here is my home made PowerShell script template to get notified about server health. The script will execute the commands you wish and paste the results into an e-mail for delivery via an SMTP server.

#Creates and Array which stores the information to send an e-mail via the SMTP server for later use. With the empty quotation marks we add additional empty lines to the e-mail to make it more readable.
[string[]] $SMTPArray = “From: from@example.com”, “To: to@example.com”, “Subject: Hello World !”, “”, “”, “Body field of your eMail”
#Example action to execute. You can execute anything you want and add it later to your e-mail. Thus you will be notified about your server health.
$ipcon = ipconfig
#Creating a variable for later use.
$SMTPMailPath = “C:\Temp\eMail.txt”
#Creating a variable for later use.
$tempPath = “C:\Temp”
#Test-Path will check if there is a Temp directory under the C:\ drive. If not it will create the directory and afterwards it will create the eMail.txt. If the directory exists it will create the eMail.txt immediately.
Test-Path -Path $tempPath -PathType Container
if ( -Not (Test-Path $tempPath))
{
$null = New-Item -Path $tempPath -ItemType Directory
}
else
{
#do nothing
}
#Create eMail.txt file under the C:\Temp directory.
New-Item $SMTPMailPath -type file
#Add to “C:\Temp\eMail.txt” file the SMTPArray and afterwards the result of ipconfig
Add-Content $SMTPMailPath $SMTPArray, $ipcon
#Replace the “C:\inetpub\mailroot\Pickup” with your SMTP Pickup Folder Path. This line copies the created eMail.txt file to the SMTP PickUp folder for delivery.
Copy-Item $SMTPMailPath “C:\inetpub\mailroot\Pickup”
#Removes the C:\Temp\eMail.txt file.
Remove-Item $SMTPMailPath

Good luck !