• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 1531
  • Last Modified:

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

0
hchabria
Asked:
hchabria
  • 2
2 Solutions
 
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
 
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
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Improved Protection from Phishing Attacks

WatchGuard DNSWatch reduces malware infections by detecting and blocking malicious DNS requests, improving your ability to protect employees from phishing attacks. Learn more about our newest service included in Total Security Suite today!

  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now