title: How to change cell color in HTML table in a PowerShell script using if condition
body: <p>I have this below script to calculate the age of snapshots (snapshots older than one day to be precise) in our environment. My code and current output are as below:</p>
  
  <pre class="lang-cs prettyprint-override"><code>Add-PSsnapin VMware.VimAutomation.Core
  
  # HTML formatting
  $a = "&lt;style&gt;"
  $a = $a + "BODY{background-color:white;}"
  $a = $a + "TABLE{border-width: 1px;border-style: solid;border-color: black;border-collapse: collapse;}"
  $a = $a + "TH{border-width: 1px;padding: 5px;border-style: solid;border-color: black;foreground-color: black;background-color: LightBlue}"
  $a = $a + "TD{border-width: 1px;padding: 5px;border-style: solid;border-color: black;foreground-color: black;background-color: white}"
  $a = $a + "&lt;/style&gt;"
  
  Connect-VIServer -Server ******-User *****-Password *****
  
  # Main section of check
  Write-Host "Checking VMs for for snapshots"
  $date = Get-Date
  $datefile = Get-Date -UFormat '%m-%d-%Y-%H%M%S'
  
  
  $filename = "C:\Temp\snaps_older_than_3\" + $datefile + ".htm"
  $ss = Get-VM |
        Get-Snapshot |
        Where {$_.Created -lt (Get-Date).AddDays(-1)} |
        Select-Object vm, name, SizeGB, SizeMB, Created, powerstate,
            @{Name='Age';Expression={(New-TimeSpan -Start $_.Created -End $date).Days}} |
        ConvertTo-Html -Head $a -Body "&lt;H2&gt;VM Snapshot Report &lt;/H2&gt;" |
        Out-File $filename
  
  Write-Host " Complete " -ForegroundColor Green
  Write-Host "Your snapshot report has been saved to:" $filename
  
  $SMTPServer = "******"
  $SMTPPort = 25
  $username = "******"
  
  #Define the receiver of the report
  $to = "******"
  $subject = "VM Snapshot Report"
  $body = "VM Snapshot Report"
  $attachment = New-Object Net.Mail.Attachment($filename)
  $message = New-Object System.Net.Mail.MailMessage
  $message.Subject = $subject
  $message.Body = $body
  $message.To.Add($to)
  $message.From = $username
  $message.Attachments.Add($attachment)
  $smtp = New-Object System.Net.Mail.SmtpClient($SMTPServer, $SMTPPort);
  $smtp.EnableSSL = $false
  #$smtp.Credentials = New-Object System.Net.NetworkCredential($Username, $Password);
  $smtp.Send($message)
  
  Write-Host "Mail Sent"
  </code></pre>
  
  <p><a href="https://i.stack.imgur.com/taFCx.jpg" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/taFCx.jpg" alt="enter image description here"></a></p>
  
  <p>What I want to do is, in the below piece of code I want to determine the color of my age cell according to the age value. For example, if age is less than 3 I want that cell to be colored in green, if it is 3 should be in blue, if it crosses it should be red.</p>
  
  <pre><code>$ss = Get-VM |
        Get-Snapshot |
        Where {$_.Created -lt (Get-Date).AddDays(-1)} |
        Select-Object vm, name, SizeGB, SizeMB, Created, powerstate,
            @{Name='Age';Expression={(New-TimeSpan -Start $_.Created -End $date).Days}} |
        ConvertTo-Html -Head $a -Body "&lt;H2&gt;VM Snapshot Report &lt;/H2&gt;" |
        Out-File $filename
  </code></pre>
  
  <p>I have googled a lot and found below piece of code by someone, but I seriously cannot make this work.</p>
  
  <pre><code>$servers = "mccoyb02", "foo", "mccoyb01", "reisrv1"
  $path = "c:\ephemeral\uptime.html"
  
  $header = @"
  &lt;!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd"&gt;
  &lt;html xmlns="http://www.w3.org/1999/xhtml" lang="en" xml:lang="en"&gt;
  &lt;head&gt;
  &lt;title&gt;System Status Report&lt;/title&gt;
  &lt;style type="text/css"&gt;
  &lt;!--
  body {
  background-color: #E0E0E0;
  font-family: sans-serif
  }
  table, th, td {
  background-color: white;
  border-collapse:collapse;
  border: 1px solid black;
  padding: 5px
  }
  --&gt;
  &lt;/style&gt;
  "@
  $body = @"
  &lt;h1&gt;Server Status&lt;/h1&gt;
  &lt;p&gt;The following report was run on $(get-date).&lt;/p&gt;
  "@
  
  $results = foreach ($server in $servers) {
      if (Test-Connection $server -Count 1 -EA 0 -Quiet) {
          $status = "Up"
      } else {
          $status = "Down"
      }
      [PSCustomObject]@{
          ServerName = $server
          Reuslts = $status
      }
  }
  
  $results | ConvertTo-Html -Head $header -Body $body | foreach {
      $PSItem -replace "&lt;td&gt;Down&lt;/td&gt;", "&lt;td style='background-color:#FF8080'&gt;Down&lt;/td&gt;"
  } | Out-File C:\Ephemeral\uptime.html
  &amp; .\uptime.html
  </code></pre>
  
  <p>Output:</p>
  
  <p><a href="https://i.stack.imgur.com/UZH1m.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/UZH1m.png" alt="enter image description here"></a></p>