Solved

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

Posted on 2009-06-30
6
780 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
NAS Cloud Backup Strategies

This article explains backup scenarios when using network storage. We review the so-called “3-2-1 strategy” and summarize the methods you can use to send NAS data to the cloud

 
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

Best Practices: Disaster Recovery Testing

Besides backup, any IT division should have a disaster recovery plan. You will find a few tips below relating to the development of such a plan and to what issues one should pay special attention in the course of backup planning.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

The article will show you how you can maintain a simple logfile of all Startup and Shutdown events on Windows servers and desktops with PowerShell. The script can be easily adapted into doing more like gracefully silencing/updating your monitoring s…
Microsoft Windows Server Update Service (WSUS) is free for everyone, but it lacks of some desirable features like send an e-mail to the administrator with the status of all computers on the WSUS server. This article is based on my PowerShell script …
This video shows how to quickly and easily add an email signature for all users on Exchange 2016. The resulting signature is applied on a server level by Exchange Online. The email signature template has been downloaded from: www.mail-signatures…

777 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