Monday, July 22, 2013

Send-Email Alert with powershell

Today we had an issue with the power in one of our buildings. Only a section of the power would go off. This section did not include servers and did not have the battery backups that would alert via email if there was a power outage. I need a fast way to know when the power went out in this area while the electricians worked to find the problem over the week. Here was my solution.

Since the servers were not going down in this particular instance I could run a script on the server. It would ping the computer every so often and then if that computer was not reachable it would send an email to my account notifying me that the power was out for that section of the building or at least that computer had been turned off.

So here was the approach.
Check to see if the computer is on

Test-Connection computername

That was easy, but how do we set a condition with this to notify us only when the power is down?

If (Test-Connection -comp computername -count 1 -quiet) {
  Write-Host Online!
  }
Else {
  Write Host Offline!
  }

Good now we just need to find a way to send email through powershell. Now you can use the Send-MailMessage commandlet however that will not work if you do not have an smtp relay setup for your email. This especially will not work if you want to use a gmail account.

Here was a solution provided by another.

Function Send-EMail {
$emailSmtpServer = "mail.somesite.com"
$emailSmtpServerPort = "587"
$emailSmtpUser = "user@somesite.com"
$emailSmtpPass = "password1234"

$emailFrom = "IT Department <user@somesite.com>"
$emailTo = "user2@somesite.com"

$emailMessage = New-Object System.Net.Mail.MailMessage( $emailFrom , $emailTo )
$emailMessage.Subject = "Panel B Power Failure"
$emailMessage.IsBodyHtml = $true
$emailMessage.Body = @"
<p><strong>Power Has Failed on Panel B</strong>.</p>
<p>Power will need to be restored</p>
"@

$SMTPClient = New-Object System.Net.Mail.SmtpClient( $emailSmtpServer , $emailSmtpServerPort )
$SMTPClient.EnableSsl = $true
$SMTPClient.Credentials = New-Object System.Net.NetworkCredential( $emailSmtpUser , $emailSmtpPass );

$SMTPClient.Send( $emailMessage )
}

Clearly you will need to change the username and passwords to fit your needs
Now all we need to do is insert the function where we had the write-host
we can even create a second function labeled  Send-AltEmail if you would like to get an email if the computer is online or offline and change the subject and message accordingly.

We are almost there. The last two steps are to put it in an endless loop and then insert a delay.
we can put the script in an endless loop by using the following lines of code.

While ($True)
{
IF (Test-Connection Computername -count 1 -quiet )
{
Send-EMail
}
ELSE
{
Send-AltEmail
}

Make sure you create your email functions first in the script. Put it all together with a start-sleep at then end choosing how long of a delay you would like to have. Now you have a complete solution to know the exact time that computer goes down.

No comments:

Post a Comment