catalin
catalin

Reputation: 1116

powershell ConvertTo-Html add class

My script is pulling information from server, than it converts to HTML and send the report by email. Snippet:

$sourceFile = "log.log"
$targetFile = "log.html"
$file = Get-Content $sourceFile
$fileLine = @()
foreach ($Line in $file) {
  $MyObject = New-Object -TypeName PSObject
  Add-Member -InputObject $MyObject -Type NoteProperty -Name Load -Value $Line
  $fileLine += $MyObject
}
$fileLine | ConvertTo-Html -Property Load -head '<style> .tdclass{color:red;} </style>' | Out-File $target

Current HTML report snippet:

<table>
<colgroup><col/></colgroup>
<tr><th>Load on servers</th></tr>
<tr><td>Server1 load is 2442</td></tr>
<tr><td>Server2 load is 6126</td></tr>
<tr><td>Server3 load is 6443</td></tr>
<tr><td> </td></tr>
<tr><td>Higher than 4000:</td></tr>
<tr><td>6126</td></tr>
<tr><td>6443</td></tr>
</table>

This will generate an HTML report containing a table with tr and td. Is there any method to make it generate td with classes, so I can insert the class name into the -head property with styles and make it red for the Higher than 4000: tds ?

Upvotes: 0

Views: 2384

Answers (2)

Jimdog
Jimdog

Reputation: 83

I know this is in a old post, but stumbled cross it looking to do something similar. I was able to add CSS styling by doing a replace. Here is an example:

$Report = $Report -replace '<td>PASS</td>','<td class="GreenStatus">PASS    &#10004;</td>'

You can then output $report to a file as normal, with the relevant css code in the header. You would need some additional logic to find values over 4000

Upvotes: 1

Deepesh
Deepesh

Reputation: 610

You can use the Get-Help ConvertTo-Html command you will get all parameters for the ConvertTo-Html command. Below is output:

ConvertTo-Html [[-Property] <Object[]>] [[-Head] <String[]>] [[-Title] <String>] [[-Body] <String[]>] [-As<String>] [-CssUri <Uri>] [-InputObject <PSObject>] [-PostContent <String[]>] [-PreContent <String[]>][<CommonParameters>]

You can create an external CSS file and give the CSS file path in the [-CssUri] parameter.

Upvotes: -1

Related Questions