How to convert .log file to .html file using PowerShell

I am using PowerShell to check the services status of a server. I am storing the result in a .log file called "ServiceTesting.log" (please see the attachment).

Now the problem is that when I am trying to convert the log file into HTML file called "report.htm", I am not getting the content of the log file in that html file. It's giving some differnt output with different column name. Request you to help me on that so that I can get the desired output. ServiceTesting.log report.htm
$log_file="z:\ServiceTesting.log"
Clear-Content $log_file
$head = @'
	<style>
		BODY{font-family:Verdana; background-color:lightblue;} 
			TABLE{border-width: 1px;border-style: solid;border-color: black;border-collapse: collapse;} 
				TH{font-size:1.3em; border-width: 1px;padding: 2px;border-style: solid;border-color: black;background-color:#FFCCCC} 
					TD{border-width: 1px;padding: 2px;border-style: solid;border-color: black;background-color:yellow}
	</style>
'@
$header = "<H1>Service Status</H1>"
$title = "Service Status"
Get-Service | Select-Object Status, Name, DisplayName >> $log_file
Get-Content $log_file | ConvertTo-HTML -head $head -body $header -title $title | Out-File z:\report.htm
& "z:\report.htm" #This will popup the htm file

Open in new window

hchabriaAsked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

soostibiCommented:
Test attached script. Hope I did not mixed up the paths.
Clear-Content z:\ServiceTesting.log  
$head = @'  
        <style>  
                BODY{font-family:Verdana; background-color:lightblue;}   
                        TABLE{border-width: 1px;border-style: solid;border-color: black;border-collapse: collapse;}   
                                TH{font-size:1.3em; border-width: 1px;padding: 2px;border-style: solid;border-color: black;background-color:#FFCCCC}   
                                        TD{border-width: 1px;padding: 2px;border-style: solid;border-color: black;background-color:yellow}  
        </style>  
'@  
$header = "<H1>Service Status</H1>"  
$title = "Service Status"  
Get-Service | Select-Object Status, Name, DisplayName | export-csv z:\ServiceTesting.log -notypeinformation
import-csv z:\ServiceTesting.log | ConvertTo-HTML -head $head -body $header -title $title | Out-File z:\report.htm  
& "z:\report.htm" #This will popup the htm file

Open in new window

0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
hchabriaAuthor Commented:
Excellent. Thank you very much for your valuable support. However, it will be very helpful if you could provide the code that will directly convert the .log file to .htm file.

The reason I am asking for is that I have various PS scripts that store output to .log files and I want to convert those .log files directly to .htm files.
0
soostibiCommented:
Here you are, but I highly recommend to switch to my first version...
$log_file="z:\ServiceTesting.log"  
Clear-Content $log_file  
$head = @'  
        <style>  
                BODY{font-family:Verdana; background-color:lightblue;}   
                        TABLE{border-width: 1px;border-style: solid;border-color: black;border-collapse: collapse;}   
                                TH{font-size:1.3em; border-width: 1px;padding: 2px;border-style: solid;border-color: black;background-color:#FFCCCC}   
                                        TD{border-width: 1px;padding: 2px;border-style: solid;border-color: black;background-color:yellow}  
        </style>  
'@  
$header = "<H1>Service Status</H1>"  
$title = "Service Status"  
Get-Service | Select-Object Status, Name, DisplayName >> $log_file  
$texthead = Get-Content $log_file -TotalCount 2 | Select-Object -Skip 1
$parts = $texthead | Select-String "^\s*\w+\s(\w+)\s+(\w+)" | Select-Object -ExpandProperty matches | Select-Object -ExpandProperty groups
$col1 = $parts[1].index
$col2 = $parts[2].index

Get-Content $log_file | Select-Object -Skip 3 | %{
    New-Object -TypeName PSObject -Property @{
        Status = $_.Substring(0,$col1) -replace "\s{2,}",""
        Name = $_.Substring($col1,$col2-$col1) -replace "\s{2,}",""
        DisplayName = $_.Substring($col2,$_.length-$col2) -replace "\s{2,}",""
    }
} | Select-Object -Property Status, name, displayname | ConvertTo-HTML -head $head -body $header -title $title | Out-File z:\report.htm  
& "z:\report.htm" #This will popup the htm file

Open in new window

0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Powershell

From novice to tech pro — start learning today.