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
LVL 1
ndalmolin_13Asked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Chris DentPowerShell DeveloperCommented:

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

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
ndalmolin_13Author Commented:
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
Chris DentPowerShell DeveloperCommented:

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
Has Powershell sent you back into the Stone Age?

If managing Active Directory using Windows Powershell® is making you feel like you stepped back in time, you are not alone.  For nearly 20 years, AD admins around the world have used one tool for day-to-day AD management: Hyena. Discover why.

ndalmolin_13Author Commented:
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
Chris DentPowerShell DeveloperCommented:

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
ndalmolin_13Author Commented:
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
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Powershell

From novice to tech pro — start learning today.