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

asked on

Need help getting output generated in a powershell script into the body of an email

Hello Experts,

I have written a small Powershell script that pings a list of servers and shows me which ones are online and which ones are offline.  How do I get the data that the script returns into the body of an email?  Below is the script.

$Servers = @(Get-QADComputer -ManagedBy ccit | foreach{$_.name} | Sort-Object)
ForEach($Server In $Servers)
{$PingStatus = Gwmi Win32_PingStatus -Filter "Address = '$Server'" |
Select-Object StatusCode
If ($PingStatus.StatusCode -eq 0)
{Write-Host "$Server" is online and answering pings -Fore "Green"}
Else
{Write-Host "$Server" is OFFLINE and must be checked -Fore "Red"}}
Avatar of ndalmolin_13
ndalmolin_13
Flag of United States of America image

ASKER

Here is what I have done:
$Body = ""
$Servers = @(Get-QADComputer -ManagedBy ccit | foreach{$_.name} | Sort-Object)
ForEach($Server In $Servers){
$PingStatus = Gwmi Win32_PingStatus -Filter "Address = '$Server'" |
Select-Object StatusCode
If ($PingStatus.StatusCode -eq 0)
{$Body = "$Body$($Server) is online and answering Pings. `n"}
Else
{$Body = "$Body$($Server) is OFFLINE and must be checked. `n"}
}

This appears to give me the body of the email.  Is there a way I can make the online servers show up in green and the offline servers show up in red within the body?
I'm getting closer.  Here is my script:
add-PSSnapin quest.activeroles.admanagement -ea SilentlyContinue

$Body = ""
$Servers = @(Get-QADComputer -ManagedBy ccit | foreach{$_.name} | Sort-Object)
ForEach($Server In $Servers){
$PingStatus = Gwmi Win32_PingStatus -Filter "Address = '$Server'" |
Select-Object StatusCode
If ($PingStatus.StatusCode -eq 0)
{$Body = "$Body$($Server) is online and answering pings. `n"}
Else
{$Body = "$Body$($Server) IS OFFLINE AND NEEDS TO BE CHECKED IMMEDIATELY. `n"}
}

$Mail = New-Object System.Net.Mail.MailMessage( `
  "ServerAdmin@coconino.az.gov", `
  "ndalmolin@coconino.az.gov", `
  "Server Disk Report", `
  $Body)
 
$Mail.IsBodyHTML = $True
 
$Smtp = New-Object System.Net.Mail.SmtpClient("cas-hub01.summitlan.states")
$Smtp.Send($Mail)

Here is the body of the email that I receive:
TS01 is online and answering pings. VM02 is online and answering pings. VM03 is online and answering pings. WICHITA is online and answering pings.

Here are my questions:
How can I format the body so that each server's status is on its own line?  Example:
TS01 is online and answering pings.
VM02 is online and answering pings.
VM03 is online and answering pings.
WICHITA is online and answering pings.

Can I make online servers highlighted in green and offline servers highlighted in red?

As always, any and all help is greatly appreciated.

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

I forgot to mention, each on it's own line is taken care of by <br />, HTML line break.

Chris
Hell we may as well take it a few steps further :)

PowerShell 2 brought us "Test-Connection" which wraps up the WMI method you're using above and makes it easier to use and Send-MailMessage to simplify that half.

So, if I were doing this, I would consider something like this. PowerShell 2 all the way. In this version Offline servers will appear first in the list (in alphabetical order by name).

Chris
$Head = "<title>Server Status</title>
  <style type='text/css'>
    body       { font-family: sans-serif; font-size: 10pt; }
    table      { width: 100%; border-collapse: collapse; }
    th         { text-align: left; padding: 0px 5px 0px 5px; border-bottom: 1px solid #CCCCCC; }
    td         { text-align: left; padding: 0px 5px 0px 5px; color: black; }
    td.Online  { text-align: left; padding: 0px 5px 0px 5px; color: green; }
    td.Offline { text-align: left; padding: 0px 5px 0px 5px; color: red; font-weight: bold; }
  </style>"

$ServerStatus = Get-QADComputer -ManagedBy ccit | 
  Select-Object Name,
   @{n='Status';e={ If (Test-Connection $($_.Name) -Quiet) { "Online" } Else { "Offline" } }} |
  Sort-Object Status, Name

$HtmlServerStatus = [String]($ServerStatus | ConvertTo-Html -Head $Head)
$HtmlServerStatus = $HtmlServerStatus -Replace "<td>Online", "<td class='Online'>Online"
$HtmlServerStatus = $HtmlServerStatus -Replace "<td>Offline", "<td class='Offline'>Offline"

Send-MailMessage -To "you@yourdomain.com" -Subject "Server Status" -From "admin@yourdomain.com" `
  -Body $HtmlServerStatus -SmtpServer "SomeServer" -BodyAsHtml

Open in new window

Outstanding!!  This worked great.  It looks like I'm going to have to teach myself HTML as well as powershell.  Can you recommend any online resources for either?

For HTML and CSS it's difficult to find anywhere better than here:

http://www.w3schools.com/

You don't need much to make things pretty, and it's useful as a reference as well.

Chris