Link to home
Start Free TrialLog in
Avatar of WeTi
WeTi

asked on

Powershell change CSS to other color when showing specific property

Dear expert

Please read the code below, now I would like to do this: when the property show on 'TotalRecovered' I would CSS to change the color of that column header to green background.
Is there a way to do?

Thanks again

$credential = Import-CliXml -Path 'C:\test\cred.xml'
$attachment = 'C:\test\corona.html'
$smtp = 'smtp.office365.com'
$url = "https://www.worldometers.info/coronavirus/#countries"
$countries = 'Sweden', 'China', 'Iran','USA', 'Italy', 'Finland', 'Norway', 'Denmark', 'Russia'
# Available properties: 'Country,Other', 'TotalCases', 'NewCases', 'TotalDeaths', 'NewDeaths', 'TotalRecovered', 'ActiveCases', 'Serious,Critical', 'Tot Cases/1M pop'
$properties = 'Country,Other', 'TotalCases', 'Tot Cases/1M pop', 'TotalDeaths' , 'TotalRecovered'

$htmlHead = @' 
<style>
	BODY {background-color:white;font-family:verdana;font-size:12px;} 
	TABLE {border-width: 1px;border-style: solid;border-color: black;border-collapse: collapse;} 
	TH {border-width: 1px;padding: 0px;border-style: solid;border-color: black;padding:3px;background-color:#FF0000} 
	TD {border-width: 1px;padding: 0px;border-style: solid;border-color: black;padding:3px} 
	tr:nth-child(odd) {background-color:#d3d3d3;} 
	tr:nth-child(even) {background-color:white;} 
</style>
'@

Add-Type -Path C:\Test\HtmlAgilityPack.dll
$web = New-Object -TypeName HtmlAgilityPack.HtmlWeb
$doc = $web.Load($url)
$header = $doc.DocumentNode.SelectNodes('//th') | Select-Object -ExpandProperty InnerText | ForEach-Object {($_ -replace '&nbsp;', ' ').Trim()}
$doc.DocumentNode.SelectNodes('//tr') | Select-Object -Skip 1 | ForEach-Object {
	$i = 0 
	$out = [ordered]@{} 
	$_.SelectNodes('td') | ForEach-Object {
		$out[$header[$i++]] = $_.InnerText.Trim()
	}
	[PSCustomObject]$out
} | Where-Object {$countries -contains $_.'Country,Other'} |
	Select-Object -Property $properties |
	Sort-Object -Property "Country,Other" |
	ConvertTo-Html -Head $htmlhead |
	Set-Content -Path $attachment

Send-MailMessage -From 'me@xxxxxxx.com' -To 'me@cccccccc.com' -Subject 'Corona status report' -Attachments $attachment -Credential $credential -SmtpServer $smtp -Port 587 -UseSsl

Open in new window

Avatar of oBdA
oBdA

Can you clarify what you mean with "when the property show on 'TotalRecovered' I would CSS to change the color of that column header"?
It's somehow not sure which parts should be green based on which condition.
Avatar of WeTi

ASKER

$htmlHead = @' 
<style>
	BODY {background-color:white;font-family:verdana;font-size:12px;} 
	TABLE {border-width: 1px;border-style: solid;border-color: black;border-collapse: collapse;} 
	TH {border-width: 1px;padding: 0px;border-style: solid;border-color: black;padding:3px;background-color:#FF0000} 
	TD {border-width: 1px;padding: 0px;border-style: solid;border-color: black;padding:3px} 
	tr:nth-child(odd) {background-color:#d3d3d3;} 
	tr:nth-child(even) {background-color:white;} 
</style>

Open in new window

When $properties.name -eq 'TotalRecovered'
td[$properties.name -eq 'TotalRecovered']{background-color: green;}
Remember we did some days ago with the boolean? That we pulled the DB property to true or false, I would like to to this here as well, when When $properties.name -eq 'TotalRecovered'  change CSS to something else, you used the "SelectSingleNode"
So you just want the header cell "TotalRecovered" in green, not any of the data cells based on its content?
Avatar of WeTi

ASKER

correct, but I would also want to know how to put whole column if possible :) Thanks thanks alot
ASKER CERTIFIED SOLUTION
Avatar of oBdA
oBdA

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
Avatar of WeTi

ASKER

This work, is there a way if I want to add more properties in the column and with different colors?
The header cells all have an attribute "Col" with their name that can be used to select them (like in line 17 above).