Share via


Add formatting and style to a html report in powershell

Question

Friday, February 8, 2019 9:47 AM

I am converting a text file to html within a powershell script using the below:

$text = Get-Content $filename -Raw
"<pre>$text</pre>" | Out-File $fiename2

However I also want to change the font, add title and color in the html report i.e. filename2 in this case.

Any clues, how I do extra formatting to any already existing html file in powershell?

All replies (9)

Friday, February 8, 2019 7:22 PM ✅Answered

Thanks for the response. I tried it, however getting the error:

+ "<pre style= "color: red;font-family: verdana">$text</pre>" | Out-File $filepath ...
+               ~~~~~~
Unexpected token 'color:' in expression or statement.

I also want to add title, heading etc.. to this already existing html file.

Too many bad guesses today...

You need to use correct quotes when doing this: (this line done with below code.

"<pre style='color: red;font-family: verdana>"

** **

\(ツ)_/


Friday, February 8, 2019 10:11 AM

Start here: https://www.w3schools.com/

**<pre style="color: red;font-family: verdana"> **

Go here for more: https://www.w3schools.com/css/default.asp

\(ツ)_/


Friday, February 8, 2019 10:47 AM

Thanks for the response. I tried it, however getting the error:

+ "<pre style= "color: red;font-family: verdana">$text</pre>" | Out-File $filepath ...
+               ~~~~~~
Unexpected token 'color:' in expression or statement.

I also want to add title, heading etc.. to this already existing html file.


Friday, February 8, 2019 12:01 PM

If I try something like below, it does change the font to Red.

"<body style=color:red><pre>$text</pre></body>" | Out-File $filename


Friday, February 8, 2019 3:47 PM

you can read each line from source file and format it write to your target file. See outline code snippet below.

....
$text = Get-Content $filename
....
foreach ($line in $text)
{
   # format each line 
   $format = "<b>"+$line+"</b>"
   #write $format to target file
...
...
}
.....


Friday, February 8, 2019 7:24 PM

you can read each line from source file and format it write to your target file. See outline code snippet below.

....
$text = Get-Content $filename
....
foreach ($line in $text)
{
   # format each line 
   $format = "<b>"+$line+"</b>"
   #write $format to target file
...
...
}
.....

That will not work.  It will make one very long bold line.  The point of "pre" tag is to maintain line formatting.

You must learn HTML before setting of on this adventure.

\(ツ)_/


Monday, February 11, 2019 6:47 AM

Hi,

Was your issue resolved?

If you resolved it using our solution, please "mark it as answer" to help other community members find the helpful reply quickly.

If you resolve it using your own solution, please share your experience and solution here. It will be very beneficial for other community members who have similar questions.

If no, please reply and tell us the current situation in order to provide further help.

Best Regards,

Lee

Just do it.


Monday, February 11, 2019 9:02 AM

Thanks will try it out.


Monday, February 11, 2019 9:24 AM

This is a multiline text block that is colorized using the "pre" tag style.
It is set to a fixed font.
This maintains line breaks and text format and will display tables correctly:   Directory: C:\windows


Mode                LastWriteTime         Length Name
                          
-a       12/19/2018   8:34 PM            762 comsetup.log
-a       11/30/2018   9:49 PM          33092 DPINST.LOG
-a        1/30/2019   4:25 PM           1759 DtcInstall.log
-a       12/19/2018   8:33 PM          12341 iis.log
-a       12/19/2018   1:46 PM          15168 iis_gather.log
-a         2/4/2019   5:33 PM          40458 PFRO.log
-a       12/19/2018   8:30 PM              0 setuperr.log
-a       12/19/2018   8:30 PM            565 Synaptics.log
-a       12/19/2018   8:30 PM           2145 Synaptics.PD.log
-a        2/11/2019   3:20 AM            276 WindowsUpdate.log

The following is all done with "pre" tags.

$text = Get-Content $filename -Raw
"<pre style='color: red;font-family: verdana'>" + $text + "</pre>" | Out-File $fiename2

\(ツ)_/