Link to home
Start Free TrialLog in
Avatar of ndalmolin_13
ndalmolin_13Flag for United States of America

asked on

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

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
ASKER CERTIFIED SOLUTION
Avatar of Chris Dent
Chris Dent
Flag of United Kingdom of Great Britain and Northern Ireland image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of ndalmolin_13

ASKER

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

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

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 :)

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
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