Home > Enterprise >  Powershell Script - Application Services Status Report email - need to highlight in red if service i
Powershell Script - Application Services Status Report email - need to highlight in red if service i

Time:02-24

I have a script that imports a csv file of six different servers to check the status of services for a particular application and emails a report each morning with service status (i.e. Running or Stopped). Here is the script. How would I highlight in red, the word "Stopped", for status in the email?

$CSVData = import-csv "C:\Scripts\Service.csv"
$Servernames = $CSVData.Servername | sort -unique
$output = @()

#$Cred = Get-Credential

foreach ($Server in $Servernames) {

$Services = $CSVData | Where {$_.Servername -eq $Server} | Select service
foreach ($Service in $Services) {
    write-host $Server
    $Serv = $Service.Service
    #$ServStat = Get-WMIObject -computer $server -credential $cred -Query "Select * From Win32_Service WHERE Name Like '$Serv'" | select displayname, state
    $ServStat = Get-WMIObject -Computer $Server -Query "Select * From Win32_Service WHERE DisplayName Like '$Serv'" | select displayname, state
    $Info = New-object -Typename PSObject
    $Info | Add-Member -MemberType NoteProperty -Name "Hostname" -value $Server
    $Info | Add-Member -MemberType NoteProperty -Name "ServiceName" -value $ServStat.displayname
    $Info | Add-Member -MemberType NoteProperty -Name "ServiceStatus" -value $ServStat.state
    $Output  =$Info
}
}

# $Output | export-csv "C:\Scripts\Result.csv" -notype

$style = "<style>BODY{font-family: Arial; font-size: 10pt;}"
$style = $style   "TABLE{border: 1px solid black; border-collapse: collapse;}"
$style = $style   "TH{border: 1px solid black; background: #dddddd; padding: 5px; }"
$style = $style   "TD{border: 1px solid black; padding: 5px; }"
$style = $style   "</style>"


$mail = @{
    from       = "Application [email protected]"
    to         = "[email protected]"
    subject    = "Application Services Status Report - $(get-date -format MM-dd-yyyy)"
    body       = $Output | ConvertTo-Html -Head $style | Out-String
    bodyashtml = $true
    smtpserver = "[smtp server name]"
    port       = 25
}
Send-MailMessage @mail

CodePudding user response:

I have no idea if this will work, but it looks like you are doing HTML in the email, and I'm guessing the word "Stopped" is in the body. So, what happens if you replace

body       = $Output | ConvertTo-Html -Head $style | Out-String

with

body       = $Output | ConvertTo-Html -Head $style -replace 'Stopped', '<p style="color:#FF0000";>Stopped</p>' | Out-String

?

Sorry, if this doesn't work, just my wild shot in the dark.

EDIT:

I'm not an HTML person, so I did a little more checking and maybe span style instead of p style is what we want.

body       = $Output | ConvertTo-Html -Head $style -replace 'Stopped', '<span style="color:red">Stopped</span>' | Out-String

EDIT#2

Just ran an experiment and this seemed to fix the PowerShell side of the problem.

body       = ($Output | ConvertTo-Html -Head $style) -replace 'Stopped', '<span style="color:red">Stopped</span>' | Out-String

CodePudding user response:

Here is another way of doing it using a Regex.Replace with script block to add colors to the ServiceStatus column. I have hardcoded the CSV since we don't know how it actually looks.

The current code will generate a test.html file on your current location, this should be changed for your send mail logic.

$output = @'
Hostname,ServiceName,ServiceStatus
Server01,SomeService,Stopped
Server02,SomeService,Started
Server03,SomeService,Started
Server04,SomeService,Stopped
Server05,SomeService,Stopped
'@ | ConvertFrom-Csv

$style = @'
<style>
BODY {
  font-family: Arial;
  font-size: 10pt;
}
TABLE {
  border: 1px solid black;
  border-collapse: collapse;
}
TH {
  border: 1px solid black;
  background: #dddddd;
  padding: 5px;
}
TD {
  border: 1px solid black;
  padding: 5px;
}
.ok {
  background-color: #C6EFCE;
  color: #006100;
  font-weight: bold;
}
.warning {
  background-color: #ffc7ce;
  color: #9c0006;
  font-weight: bold;
}
</style>
'@

$html = $output | ConvertTo-Html -Head $style -PreContent '<h1>Some Report</h1>'
[regex]::Replace($html, '<td>(Stopped|Started)</td>', {
    param($s)
    switch($s.Groups[1].Value) {
        Started { "<td class=ok>$_</td>" }
        Stopped { "<td class=warning>$_</td>" }
    }
}) | Out-File ./test.html

Invoke-Item ./test.html

You can run the snippet below to see how it looks.

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"  "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">

<head>
  <style>
    BODY {
      font-family: Arial;
      font-size: 10pt;
    }
    
    TABLE {
      border: 1px solid black;
      border-collapse: collapse;
    }
    
    TH {
      border: 1px solid black;
      background: #dddddd;
      padding: 5px;
    }
    
    TD {
      border: 1px solid black;
      padding: 5px;
    }
    
    .ok {
      background-color: #C6EFCE;
      color: #006100;
      font-weight: bold;
    }
    
    .warning {
      background-color: #ffc7ce;
      color: #9c0006;
      font-weight: bold;
    }
  </style>
</head>

<body>
  <h1>Some Report</h1>
  <table>
    <colgroup>
      <col/>
      <col/>
      <col/>
    </colgroup>
    <tr>
      <th>Hostname</th>
      <th>ServiceName</th>
      <th>ServiceStatus</th>
    </tr>
    <tr>
      <td>Server01</td>
      <td>SomeService</td>
      <td class=warning>Stopped</td>
    </tr>
    <tr>
      <td>Server02</td>
      <td>SomeService</td>
      <td class=ok>Started</td>
    </tr>
    <tr>
      <td>Server03</td>
      <td>SomeService</td>
      <td class=ok>Started</td>
    </tr>
    <tr>
      <td>Server04</td>
      <td>SomeService</td>
      <td class=warning>Stopped</td>
    </tr>
    <tr>
      <td>Server05</td>
      <td>SomeService</td>
      <td class=warning>Stopped</td>
    </tr>
  </table>
</body>
</html>

  • Related