VMware Cloud Community
AGFlora
Enthusiast
Enthusiast

How to change font color to red if a condition is met?

HI

How would I change the font color to red in an HTML report if the PercentageFree is less than 10 %

a = "<style>"
$a = $a + "body {background-color:Ivory; font-family:Tahoma; font-size:12pt;}"
$a = $a + "td, th {border:1px solid black; border-collapse:collapse;}"
$a = $a + "th {color:white; background-color:Green;}"
$a = $a + "table, tr, td, th {padding: 2px; margin: 0px}"
$a = $a + "table {margin-left:70px;}"
$a = $a + "</style>"


##Function to get percentage of datastore free space
####################################################
function CalcPercent {    
     param(    
     [parameter(Mandatory = $true)]    
     [int]$InputNum1,    
     [parameter(Mandatory = $true)]    
     [int]$InputNum2)    
     $InputNum1 / $InputNum2*100

$datastores = Get-Datacenter "ECC-MTB (Martinsburg)" | Get-Datastore  | Where-Object {$_.Name -notlike "DataStore1*"}  
ForEach ($ds in $datastores)

{    
   if (($ds.Name -match "Shared") -or ($ds.Name -match ""))    
   {         $PercentFree = CalcPercent $ds.FreeSpaceMB $ds.CapacityMB        
             $PercentFree = "{0:N2}" -f $PercentFree       
             $ds | Add-Member -type NoteProperty -name PercentFree -value $PercentFree   
  }
}

Reply
0 Kudos
7 Replies
mattboren
Expert
Expert

Hello, AGFlora-

You could go through the $datastores array after you have created it and added the PercentFree property and create a table with a row for each datastore, adding a bit of STYLE code if the PercentFree for the given datastore is under the given value (10, here).  Like:

## create the style HTML code (using a here-string)
$a = @"
<style>
    body {background-color:Ivory; font-family:Tahoma; font-size:12pt;}
    td, th {border:1px solid black; border-collapse:collapse;}
    th {color:white; background-color:Green;}
    table, tr, td, th {padding: 2px; margin: 0px}
    table {margin-left:70px;}
</style>
"@

## put the datastore info gathering code here
#
...
#
#

## create a TABLE from the gathered info
$strTableStartHTML = "<TABLE CELLSPACING=1 CELLPADDING=1 BORDER=1>`n<TR><TH>Datastore Name</TH><TH>Capacity (GB)</TH><TH>Percent Free</TH></TR>"
$strTableBodyHTML = foreach ($dStore in $datastores) {
   
## create the row STYLE HTML based on this datastore's PercentFree value
    $strRowStyleHTML = if ($dStore.PercentFree -lt 10) {" STYLE='color: Red'"} else {$null}
   
## return the HTML for the row for this datastore
    "<TR$strRowStyleHTML><TD>$($dStore.Name)</TD><TD>$($dStore.CapacityGB)</TD><TD>$($dStore.PercentFree)%</TD></TR>`n"
}
## end foreach
$strTableEndHTML = "</TABLE>"

ConvertTo-Html -Title "Datastores Info" -Body $a$strTableStartHTML$strTableBodyHTML$strTableEndHTML | Out-File c:\temp\myDStoreInfo.html

That get it for you?

Reply
0 Kudos
AGFlora
Enthusiast
Enthusiast

Hi Matt

Close but no cigar. The percentages less than 10 % did not appear in red.

Regards,

Reply
0 Kudos
mattboren
Expert
Expert

Mhmm.  To troubleshoot this, what output do you get when running the following (assumes that you have already run the datastore info gathering code):

$datastores | select Name,FreeSpaceGB,PercentFree

And, when you look at the output in c:\temp\myDStoreInfo.html and check the HTML for the rows that you want to have red font, what does the start of the line look like (the "<TR..." part for a row that should have red font)?

Reply
0 Kudos
AGFlora
Enthusiast
Enthusiast

The column titles are in green....

NameFreeSpaceMBCapacityMBPercentFree
DS_001820880209587239.17

Here's the code that I'm running...

## create the style HTML code (using a here-string)
$a = @"
<style>   
body {background-color:Ivory; font-family:Tahoma; font-size:12pt;}   
td, th {border:1px solid black; border-collapse:collapse;}   
th {color:white; background-color:Green;}   
table, tr, td, th {padding: 2px; margin: 0px}   
table {margin-left:70px;}</style>
"@
## put the datastore info gathering code here
function CalcPercent {    
     param(    
     [parameter(Mandatory = $true)]    
     [int]$InputNum1,    
     [parameter(Mandatory = $true)]    
     [int]$InputNum2)    
     $InputNum1 / $InputNum2*100
}

$datastores = Get-Datacenter "Datacenter01" | Get-Datastore  | Where-Object {$_.Name -notlike "DataStore1*"}  
ForEach ($ds in $datastores)

{    
   if (($ds.Name -match "Shared") -or ($ds.Name -match ""))    
   {         $PercentFree = CalcPercent $ds.FreeSpaceMB $ds.CapacityMB        
             $PercentFree = "{0:N2}" -f $PercentFree       
             $ds | Add-Member -type NoteProperty -name PercentFree -value $PercentFree   
  }
}
#...
##
##create a TABLE from the gathered info
$strTableStartHTML = "<TABLE CELLSPACING=1 CELLPADDING=1 BORDER=1>`n<TR><TH>Datastore Name</TH><TH>Capacity (GB)</TH><TH>Percent Free</TH></TR>"
$strTableBodyHTML = foreach ($dStore in $datastores) {   
## create the row STYLE HTML based on this datastore's PercentFree value   
$strRowStyleHTML = if ($dStore.PercentFree -lt 10) {" STYLE='color: Red'"} else {$null}   
## return the HTML for the row for this datastore   
"<TR$strRowStyleHTML><TD>$($dStore.Name)</TD><TD>$($dStore.CapacityGB)</TD><TD>$($dStore.PercentFree)%</TD></TR>`n"
} ## end foreach
$strTableEndHTML = "</TABLE>"
ConvertTo-Html -Title "Datastores Info" -Body $a$strTableStartHTML$strTableBodyHTML$strTableEndHTML | Out-File myDStoreInfo.html

Reply
0 Kudos
mattboren
Expert
Expert

Hello-

Hmm.  You appear to be looking at a different .html output file than the one generated by the code that you are running here.  The table snippet that you pasted in has four (4) columns, but the code that I provided creates HTML with a table with only three (3) columns.  The HTML file that results from the code I provided has column headers of "Datastore Name", "Capacity (GB)", and "Percent Free".

Are you looking at the .html output file from some other testing?  The code that you pasted in last should generate "myDStoreInfo.html" in the current working directory.  Have a look at that file.

Reply
0 Kudos
AGFlora
Enthusiast
Enthusiast

Hi Matt

Yes you are correct I did paste the output from the wrong html file. Sorry. Here's the correct one;

Datastore NameCapacity (GB)Percent Free
DataStore_11544.960937510.00%

Everything works fine except the color of the percent free value doesn't change color if the value is less than 10 .

Reply
0 Kudos
mattboren
Expert
Expert

Hello, AGFlora-

Alright, then.  Well, do you want it red if the value is less than 10%, or if it is less than or equal to 10%?  The example that you pasted on 21 Sep has a row that would not be red, since it is not less than 10% (it is exactly 10%).  If you want it to be red if the value is less than or equal, you can change the comparison operation in the PowerShell code from "-lt 10" to "-le 10".

And, if you have other example rows in the HTML file where the "Percent Free" column is less than 10%, could you open up that HTML file (view its source) and grab the HTML code that is generated?  That could help further determine what is going on here.

Reply
0 Kudos