Solved

How do I send an email alert of offline servers using powershell

Posted on 2009-06-30
6
777 Views
Last Modified: 2012-06-27
Hello Powershell Experts,

I would like to configure a powershell script to email me a list of servers that are not offline each morning.  I can get the list of servers offline by performing a ping test using the following powershell code:

$OS = "Windows*server*"
$Servers = Get-QADComputer -OSName $OS | foreach{$_.name} | Sort-Object
Foreach ($item in $Servers) {
$PingResult = Get-WmiObject -Query "Select * From win32_PingStatus Where address='$item'"
      If ($PingResult.statuscode -ne 0) {
      Write-Host "$item is not pingable and the server is off-line."
      }
}

I can send myself an email using the following code:
$mail = New-Object System.Net.Mail.MailMessage("ndalmolin@test.com","admin@test.com","offline servers","body")
$smtp = New-Object System.Net.Mail.SmtpClient("mail1.summitlan.states", "25");
$smtp.Send($mail);

How do I get the results of the ping test into the body of the email notice.  I want the email to look like:

To: nick@test.com
From: admin@test.com
Subject: Offline Servers

Server01 is not pingable the server is offline
Server02 is not pingable the server is offline
Server03 is not pingable the server is offline

I think I'm close, but I'm definately having troubles.

As always, any help is greatly appreciated.

Regards
Nick
0
Comment
Question by:ndalmolin_13
  • 3
  • 3
6 Comments
 
LVL 70

Accepted Solution

by:
Chris Dent earned 500 total points
ID: 24749751

Hey Nick,

This should do it.

Chris
$OS = "Windows*server*"
 

Get-QADComputer -OSName $OS | %{

  $PingResult = Get-WmiObject -Query "Select * From win32_PingStatus Where address='$($_.Name)'"

  If ($PingResult.statuscode -ne 0) {

    $Body = "$Body$($_.Name) is not pingable and the server is off-line.`n"

  }

}
 

$mail = New-Object System.Net.Mail.MailMessage("ndalmolin@test.com","admin@test.com","offline servers", $Body)

$smtp = New-Object System.Net.Mail.SmtpClient("mail1.summitlan.states", "25");

$smtp.Send($mail);

Open in new window

0
 
LVL 1

Author Comment

by:ndalmolin_13
ID: 24753962
Hi Chris,

Thanks for the prompt response.  This looks like it is working.  I have a couple of questions if you don't mind my asking.
1.  On line 3 of your code, what does the % followed by the open bracket translate too?
2.  Could you explain the $body$($_.name) in line 6 of your code.

Thanks,
Nick
0
 
LVL 70

Expert Comment

by:Chris Dent
ID: 24754619

1. % is an Alias for ForEach-Object. It allows you to loop through an object (such as the values returned from Get-QADComputer) without having to name an item.

2. For the most part String Concatenation. We're concatenating the previous values for $Body with the new line, containing the current server.

$( .. ) within the string (" ... ") has PowerShell execute the expression within before adding it to the string. Take of the $ immediately preceding it and you'll end up with "The current values in Body($_.name)" as a string rather than it giving you the value for $_.name (if you see what I mean).

It's probably best to have a play around to see how it's putting itself together. This is a bit of a debugging version.

Chris
$OS = "Windows*server*"
 

$Body = ""

Get-QADComputer -OSName $OS | %{

  Write-Host "Current host is $($_.Name)"
 

  $PingResult = Get-WmiObject -Query "Select * From win32_PingStatus Where address='$($_.Name)'"

  If ($PingResult.statuscode -ne 0) {

    # Write the current value stored in $Body

    Write-Host "Current value in Body:"

    Write-Host $Body
 

    # The `n at the end of this line is a carriage return / line break

    $Body = "$Body$($_.Name) is not pingable and the server is off-line.`n"

  }

}

Open in new window

0
How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

 
LVL 1

Author Comment

by:ndalmolin_13
ID: 24754910
Hello Chris,

I can't thank you enough for the time you have taken in helping me not only with this powershell questions, but the others you have answered before.  I have been playing around with the code to see what does what and changeing what various elements of it will have on the results.

 I have run the code you posted and I get the following:
(WISCONSIN) is not pingable and the server is off-line.
(ARKANSAS) is not pingable and the server is off-line.
(AS-TEST1) is not pingable and the server is off-line.
(AUGUSTA) is not pingable and the server is off-line.
(BUP3) is not pingable and the server is off-line.
(CADTRAINING) is not pingable and the server is off-line.
(CC-TEST2K3) is not pingable and the server is off-line.
(CONNECTICUT) is not pingable and the server is off-line.
(DC04) is not pingable and the server is off-line.
(DC1) is not pingable and the server is off-line.
(DC2) is not pingable and the server is off-line.
(EDMSTEST01) is not pingable and the server is off-line.
(FIFS01) is not pingable and the server is off-line.
(FPSPW) is not pingable and the server is off-line.
(FRANKFORT) is not pingable and the server is off-line.
(GIS-DB01) is not pingable and the server is off-line.
(GIS-MAP2) is not pingable and the server is off-line.
(IS-DEV1) is not pingable and the server is off-line.
(IT-FPQDB71C) is not pingable and the server is off-line.
(KACHINA) is not pingable and the server is off-line.
(KANSAS) is not pingable and the server is off-line.
(NEWJERSEY) is not pingable and the server is off-line.
(NEWMEXICO1) is not pingable and the server is off-line.
(OREGON) is not pingable and the server is off-line.
(OWA) is not pingable and the server is off-line.
(SITGREAVES) is not pingable and the server is off-line.
(VIRTUALSQL1) is not pingable and the server is off-line.
(VIRTUALSQL2) is not pingable and the server is off-line.
(WASHINGTON) is not pingable and the server is off-line.
(WISCONSIN) is not pingable and the server is off-line.
'ARKANSAS' is not pingable and the server is off-line.
'AUGUSTA' is not pingable and the server is off-line.
'BUP3' is not pingable and the server is off-line.
'CADTRAINING' is not pingable and the server is off-line.
'CC-TEST2K3' is not pingable and the server is off-line.
'CONNECTICUT' is not pingable and the server is off-line.
'DC04' is not pingable and the server is off-line.
'DC1' is not pingable and the server is off-line.
'DC2' is not pingable and the server is off-line.
'EDMSTEST01' is not pingable and the server is off-line.
'FIFS01' is not pingable and the server is off-line.
'FPSPW' is not pingable and the server is off-line.
'FRANKFORT' is not pingable and the server is off-line.
'GIS-DB01' is not pingable and the server is off-line.
'GIS-MAP2' is not pingable and the server is off-line.
'IS-DEV1' is not pingable and the server is off-line.
'IT-FPQDB71C' is not pingable and the server is off-line.
'KACHINA' is not pingable and the server is off-line.
'KANSAS' is not pingable and the server is off-line.
'NEWJERSEY' is not pingable and the server is off-line.
'NEWMEXICO1' is not pingable and the server is off-line.
'OREGON' is not pingable and the server is off-line.
'OWA' is not pingable and the server is off-line.
'SITGREAVES' is not pingable and the server is off-line.
'VIRTUALSQL1' is not pingable and the server is off-line.
'VIRTUALSQL2' is not pingable and the server is off-line.
'WASHINGTON' is not pingable and the server is off-line.
'WISCONSIN' is not pingable and the server is off-line.
(ARKANSAS) is not pingable and the server is off-line.
(AUGUSTA) is not pingable and the server is off-line.
(BUP3) is not pingable and the server is off-line.
(CADTRAINING) is not pingable and the server is off-line.
(CC-TEST2K3) is not pingable and the server is off-line.
(CONNECTICUT) is not pingable and the server is off-line.
(DC04) is not pingable and the server is off-line.
(DC1) is not pingable and the server is off-line.
(DC2) is not pingable and the server is off-line.
(EDMSTEST01) is not pingable and the server is off-line.
(FIFS01) is not pingable and the server is off-line.
(FPSPW) is not pingable and the server is off-line.
(FRANKFORT) is not pingable and the server is off-line.
(GIS-DB01) is not pingable and the server is off-line.
(GIS-MAP2) is not pingable and the server is off-line.
(IS-DEV1) is not pingable and the server is off-line.
(IT-FPQDB71C) is not pingable and the server is off-line.
(KACHINA) is not pingable and the server is off-line.
(KANSAS) is not pingable and the server is off-line.
(NEWJERSEY) is not pingable and the server is off-line.
(NEWMEXICO1) is not pingable and the server is off-line.
(OREGON) is not pingable and the server is off-line.
(OWA) is not pingable and the server is off-line.
(SITGREAVES) is not pingable and the server is off-line.
(VIRTUALSQL1) is not pingable and the server is off-line.
(VIRTUALSQL2) is not pingable and the server is off-line.
(WASHINGTON) is not pingable and the server is off-line.
(WISCONSIN) is not pingable and the server is off-line.
(SUMMITLAN\DC1$.Name) is not pingable and the server is off-line.
(SUMMITLAN\DC2$.Name) is not pingable and the server is off-line.
(SUMMITLAN\IS-DEV1$.Name) is not pingable and the server is off-line.
(SUMMITLAN\CADTRAINING$.Name) is not pingable and the server is off-line.
(SUMMITLAN\CC-TEST2K3$.Name) is not pingable and the server is off-line.
(SUMMITLAN\CONNECTICUT$.Name) is not pingable and the server is off-line.
(SUMMITLAN\WISCONSIN$.Name) is not pingable and the server is off-line.
(SUMMITLAN\NEWMEXICO1$.Name) is not pingable and the server is off-line.
(SUMMITLAN\SITGREAVES$.Name) is not pingable and the server is off-line.
(SUMMITLAN\NEWJERSEY$.Name) is not pingable and the server is off-line.
(SUMMITLAN\GIS-MAP2$.Name) is not pingable and the server is off-line.
(SUMMITLAN\OREGON$.Name) is not pingable and the server is off-line.
(SUMMITLAN\ARKANSAS$.Name) is not pingable and the server is off-line.
(SUMMITLAN\WASHINGTON$.Name) is not pingable and the server is off-line.
(SUMMITLAN\KACHINA$.Name) is not pingable and the server is off-line.
(SUMMITLAN\OWA$.Name) is not pingable and the server is off-line.
(SUMMITLAN\AUGUSTA$.Name) is not pingable and the server is off-line.
(SUMMITLAN\EDMSTEST01$.Name) is not pingable and the server is off-line.
(SUMMITLAN\IT-FPQDB71C$.Name) is not pingable and the server is off-line.
(SUMMITLAN\KANSAS$.Name) is not pingable and the server is off-line.
(SUMMITLAN\BUP3$.Name) is not pingable and the server is off-line.
(SUMMITLAN\FRANKFORT$.Name) is not pingable and the server is off-line.
(SUMMITLAN\FPSPW$.Name) is not pingable and the server is off-line.
(SUMMITLAN\VIRTUALSQL1$.Name) is not pingable and the server is off-line.
(SUMMITLAN\VIRTUALSQL2$.Name) is not pingable and the server is off-line.
(SUMMITLAN\DC04$.Name) is not pingable and the server is off-line.
(SUMMITLAN\GIS-DB01$.Name) is not pingable and the server is off-line.
(SUMMITLAN\FIFS01$.Name) is not pingable and the server is off-line.
DC1 is not pingable and the server is off-line.
DC2 is not pingable and the server is off-line.
IS-DEV1 is not pingable and the server is off-line.
CADTRAINING is not pingable and the server is off-line.
CC-TEST2K3 is not pingable and the server is off-line.
CONNECTICUT is not pingable and the server is off-line.
WISCONSIN is not pingable and the server is off-line.
NEWMEXICO1 is not pingable and the server is off-line.
SITGREAVES is not pingable and the server is off-line.
NEWJERSEY is not pingable and the server is off-line.
GIS-MAP2 is not pingable and the server is off-line.
OREGON is not pingable and the server is off-line.
ARKANSAS is not pingable and the server is off-line.
WASHINGTON is not pingable and the server is off-line.
KACHINA is not pingable and the server is off-line.
OWA is not pingable and the server is off-line.
AUGUSTA is not pingable and the server is off-line.
EDMSTEST01 is not pingable and the server is off-line.
IT-FPQDB71C is not pingable and the server is off-line.
KANSAS is not pingable and the server is off-line.
BUP3 is not pingable and the server is off-line.
FRANKFORT is not pingable and the server is off-line.
FPSPW is not pingable and the server is off-line.
VIRTUALSQL1 is not pingable and the server is off-line.
VIRTUALSQL2 is not pingable and the server is off-line.
DC04 is not pingable and the server is off-line.
GIS-DB01 is not pingable and the server is off-line.
FIFS01 is not pingable and the server is off-line.
(SUMMITLAN\DC1$.Name) is not pingable and the server is off-line.
(SUMMITLAN\DC2$.Name) is not pingable and the server is off-line.
(SUMMITLAN\IS-DEV1$.Name) is not pingable and the server is off-line.
(SUMMITLAN\CADTRAINING$.Name) is not pingable and the server is off-line.
(SUMMITLAN\CC-TEST2K3$.Name) is not pingable and the server is off-line.
(SUMMITLAN\CONNECTICUT$.Name) is not pingable and the server is off-line.
(SUMMITLAN\WISCONSIN$.Name) is not pingable and the server is off-line.
(SUMMITLAN\NEWMEXICO1$.Name) is not pingable and the server is off-line.
(SUMMITLAN\SITGREAVES$.Name) is not pingable and the server is off-line.
(SUMMITLAN\NEWJERSEY$.Name) is not pingable and the server is off-line.
(SUMMITLAN\GIS-MAP2$.Name) is not pingable and the server is off-line.
(SUMMITLAN\OREGON$.Name) is not pingable and the server is off-line.
(SUMMITLAN\ARKANSAS$.Name) is not pingable and the server is off-line.
(SUMMITLAN\WASHINGTON$.Name) is not pingable and the server is off-line.
(SUMMITLAN\KACHINA$.Name) is not pingable and the server is off-line.
(SUMMITLAN\OWA$.Name) is not pingable and the server is off-line.
(SUMMITLAN\AUGUSTA$.Name) is not pingable and the server is off-line.
(SUMMITLAN\EDMSTEST01$.Name) is not pingable and the server is off-line.
(SUMMITLAN\IT-FPQDB71C$.Name) is not pingable and the server is off-line.
(SUMMITLAN\KANSAS$.Name) is not pingable and the server is off-line.
(SUMMITLAN\BUP3$.Name) is not pingable and the server is off-line.
(SUMMITLAN\FRANKFORT$.Name) is not pingable and the server is off-line.
(SUMMITLAN\FPSPW$.Name) is not pingable and the server is off-line.
(SUMMITLAN\VIRTUALSQL1$.Name) is not pingable and the server is off-line.
(SUMMITLAN\VIRTUALSQL2$.Name) is not pingable and the server is off-line.
(SUMMITLAN\DC04$.Name) is not pingable and the server is off-line.
(SUMMITLAN\GIS-DB01$.Name) is not pingable and the server is off-line.
(SUMMITLAN\FIFS01$.Name) is not pingable and the server is off-line.
ARKANSAS is not pingable and the server is off-line.
AUGUSTA is not pingable and the server is off-line.
BUP3 is not pingable and the server is off-line.
CADTRAINING is not pingable and the server is off-line.
CC-TEST2K3 is not pingable and the server is off-line.
CONNECTICUT is not pingable and the server is off-line.
DC04 is not pingable and the server is off-line.
DC1 is not pingable and the server is off-line.
DC2 is not pingable and the server is off-line.
EDMSTEST01 is not pingable and the server is off-line.
FIFS01 is not pingable and the server is off-line.
FPSPW is not pingable and the server is off-line.
FRANKFORT is not pingable and the server is off-line.
GIS-DB01 is not pingable and the server is off-line.
GIS-MAP2 is not pingable and the server is off-line.
IS-DEV1 is not pingable and the server is off-line.
IT-FPQDB71C is not pingable and the server is off-line.
KACHINA is not pingable and the server is off-line.
KANSAS is not pingable and the server is off-line.
NEWJERSEY is not pingable and the server is off-line.
NEWMEXICO1 is not pingable and the server is off-line.
OREGON is not pingable and the server is off-line.
OWA is not pingable and the server is off-line.
SITGREAVES is not pingable and the server is off-line.
VIRTUALSQL1 is not pingable and the server is off-line.
VIRTUALSQL2 is not pingable and the server is off-line.
WASHINGTON is not pingable and the server is off-line.
WISCONSIN is not pingable and the server is off-line.
DC1 is not pingable and the server is off-line.
DC2 is not pingable and the server is off-line.
IS-DEV1 is not pingable and the server is off-line.
CADTRAINING is not pingable and the server is off-line.
CC-TEST2K3 is not pingable and the server is off-line.
CONNECTICUT is not pingable and the server is off-line.
WISCONSIN is not pingable and the server is off-line.
NEWMEXICO1 is not pingable and the server is off-line.
SITGREAVES is not pingable and the server is off-line.
NEWJERSEY is not pingable and the server is off-line.
GIS-MAP2 is not pingable and the server is off-line.
OREGON is not pingable and the server is off-line.
ARKANSAS is not pingable and the server is off-line.
WASHINGTON is not pingable and the server is off-line.
KACHINA is not pingable and the server is off-line.
OWA is not pingable and the server is off-line.
AUGUSTA is not pingable and the server is off-line.
EDMSTEST01 is not pingable and the server is off-line.
IT-FPQDB71C is not pingable and the server is off-line.
KANSAS is not pingable and the server is off-line.
BUP3 is not pingable and the server is off-line.
FRANKFORT is not pingable and the server is off-line.
FPSPW is not pingable and the server is off-line.
VIRTUALSQL1 is not pingable and the server is off-line.
VIRTUALSQL2 is not pingable and the server is off-line.
DC04 is not pingable and the server is off-line.
GIS-DB01 is not pingable and the server is off-line.
FIFS01 is not pingable and the server is off-line.

How would you recommend cleaning this up a bit?  As you can see, there are about 30 servers that do not reply to pings and are offline, but the script lists these servers 3 different ways.  

Thanks again for any help you provide :)
0
 
LVL 70

Expert Comment

by:Chris Dent
ID: 24754931

I suspect you have a remembered "Body". Perhaps add:

$Body = ""

Right at the beginning to ensure it's empty before re-running the script :)

Chris
0
 
LVL 1

Author Closing Comment

by:ndalmolin_13
ID: 31598524
Chris,
I put the $body"" at the begining of the script and it worked like a charm.  Thanks again for all of your help.  When my powershell skills evolve beyond the pathetic newbie stage, I will remember all of the help that you gave me and hopefully I can pay it forward and help someone else.

Thanks,
Nick
0

Featured Post

Highfive + Dolby Voice = No More Audio Complaints!

Poor audio quality is one of the top reasons people don’t use video conferencing. Get the crispest, clearest audio powered by Dolby Voice in every meeting. Highfive and Dolby Voice deliver the best video conferencing and audio experience for every meeting and every room.

Join & Write a Comment

Why would I want to create a function for tracking messages? I am glad you asked. As with most monotonous/routine tasks, human error tends to creep in after doing the same task over and over again. By creating a function, you load the function once…
Synchronize a new Active Directory domain with an existing Office 365 tenant
Internet Business Fax to Email Made Easy - With eFax Corporate (http://www.enterprise.efax.com), you'll receive a dedicated online fax number, which is used the same way as a typical analog fax number. You'll receive secure faxes in your email, fr…
Here's a very brief overview of the methods PRTG Network Monitor (https://www.paessler.com/prtg) offers for monitoring bandwidth, to help you decide which methods you´d like to investigate in more detail.  The methods are covered in more detail in o…

746 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

14 Experts available now in Live!

Get 1:1 Help Now