jeevanreddymandali
jeevanreddymandali

Reputation: 395

How to change cell color in HTML table in a PowerShell script using if condition

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:

Add-PSsnapin VMware.VimAutomation.Core

# HTML formatting
$a = "<style>"
$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 + "</style>"

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 "<H2>VM Snapshot Report </H2>" |
      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"

enter image description here

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.

$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 "<H2>VM Snapshot Report </H2>" |
      Out-File $filename

I have googled a lot and found below piece of code by someone, but I seriously cannot make this work.

$servers = "mccoyb02", "foo", "mccoyb01", "reisrv1"
$path = "c:\ephemeral\uptime.html"

$header = @"
<!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" lang="en" xml:lang="en">
<head>
<title>System Status Report</title>
<style type="text/css">
<!--
body {
background-color: #E0E0E0;
font-family: sans-serif
}
table, th, td {
background-color: white;
border-collapse:collapse;
border: 1px solid black;
padding: 5px
}
-->
</style>
"@
$body = @"
<h1>Server Status</h1>
<p>The following report was run on $(get-date).</p>
"@

$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 "<td>Down</td>", "<td style='background-color:#FF8080'>Down</td>"
} | Out-File C:\Ephemeral\uptime.html
& .\uptime.html

Output:

enter image description here

Upvotes: 0

Views: 11365

Answers (1)

Guenther Schmitz
Guenther Schmitz

Reputation: 1999

the video from Jeffrey Hicks (https://www.youtube.com/watch?v=QdK3qM5jnYw) pointed me in the right direction. And the article at Petri got the solution to style cells (and not only rows). I recommend you also watch/read those.

I've edited also the $head variable to a Here-String for easier reading and editing.

The styling of cells requires it to first convert the object to a XML object (mind the -Fragment switch!). This XML object can then be parsed, edited and finally converted to HTML. Hope the comments are clear.

Add-PSsnapin VMware.VimAutomation.Core

# HTML formatting
$head = @"
<style>
BODY{background-color:white;}
TABLE{border-width: 1px;border-style: solid;border-color: black;border-collapse: collapse;}
TH{border-width: 1px;padding: 5px;border-style: solid;border-color: black;foreground-color: black;background-color: LightBlue}
TD{border-width: 1px;padding: 5px;border-style: solid;border-color: black;foreground-color: black;background-color: white}
.green{background-color:#d5f2d5}
.blue{background-color:#e0eaf1}
.red{background-color:#ffd7de}
</style>
"@

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"

# Convert the selection to an XML object
[xml]$xmlObject = 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 -Fragment

# Parse XML object and set colour class according to value in last column ("Age")
for($i=1;$i -le $xmlObject.table.tr.count-1;$i++) {
    if(($xmlObject.table.tr[$i].td[-1] -as [int]) -lt 3) {
        $xmlObject.table.tr[$i].ChildNodes[($xmlObject.table.tr[$i].ChildNodes.Count-1)].SetAttribute('class','green')
    } elseif (($xmlObject.table.tr[$i].td[-1] -as [int]) -eq 3) {
        $xmlObject.table.tr[$i].ChildNodes[($xmlObject.table.tr[$i].ChildNodes.Count-1)].SetAttribute('class','blue')
    } else {
        $xmlObject.table.tr[$i].ChildNodes[($xmlObject.table.tr[$i].ChildNodes.Count-1)].SetAttribute('class','red')
    }
}

# Define body and append the modified XML object
$body = @"
<H2>VM Snapshot Report </H2>
$($xmlObject.innerxml)
"@

# Convert to HTML and save the file
ConvertTo-Html -Head $head -Body $body |
      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"

Upvotes: 1

Related Questions