SOS - Convert a TXT file to HTML in Power shell

Elad-It
Elad-It used Ask the Experts™
on
Im going crazy :-)
my script entils as follows:

$a = "<style>"
$a = $a + "BODY{background-color:peachpuff;}"
$a = $a + "</style>"
Get-Content C:\scripts\jj\jason.txt| ConvertTo-Html -head $a –body "<H2>The J report</H2>" | Out-File C:\scripts\jj\Report.html
but my output in the HTML file is :

-I would really appreciate your input, Thanks !
Jason.

The J report

PSPath      PSParentPath      PSChildName      PSDrive      PSProvider      ReadCount      Length
C:\scripts\jj\jason.txt      C:\scripts\jj      jason.txt      C      Microsoft.PowerShell.Core\FileSystem      1      172
C:\scripts\jj\jason.txt      C:\scripts\jj      jason.txt      C      Microsoft.PowerShell.Core\FileSystem      2      372
C:\scripts\jj\jason.txt      C:\scripts\jj      jason.txt      C      Microsoft.PowerShell.Core\FileSystem      3      151
C:\scripts\jj\jason.txt      C:\scripts\jj      jason.txt      C      Microsoft.PowerShell.Core\FileSystem      4      296
C:\scripts\jj\jason.txt      C:\scripts\jj      jason.txt      C      Microsoft.PowerShell.Core\FileSystem      5      137
C:\scripts\jj\jason.txt      C:\scripts\jj      jason.txt      C      Microsoft.PowerShell.Core\FileSystem      6      368
C:\scripts\jj\jason.txt      C:\scripts\jj      jason.txt      C      Microsoft.PowerShell.Core\FileSystem      7      0
C:\scripts\jj\jason.txt      C:\scripts\jj      jason.txt      C      Microsoft.PowerShell.Core\FileSystem      8      231
C:\scripts\jj\jason.txt      C:\scripts\jj      jason.txt      C      Microsoft.PowerShell.Core\FileSystem      9      231
C:\scripts\jj\jason.txt      C:\scripts\jj      jason.txt      C      Microsoft.PowerShell.Core\FileSystem      10      230
Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®

Commented:
What's in jason.txt?

Author

Commented:
Just text.. one liners from my relay log
many lines like:

Apr 01 18:00:10 Sever ss-mra[0001]: B1ddd10a008: to=<user@domain.com>, delay=00:00:05, xdelay=00:00:05, mailer=smtp, pri=10001, relay=relay.domain.com [10.0.0.1], dsn=2.0.0, stat=Sent (B1ddd10a0008 Message accepted for delivery)

Commented:
I'm not sure what result you expect, but let's have a look at that:

$a = "<style>"
$a = $a + "BODY{background-color:peachpuff;}"
$a = $a + "</style>"
Get-Content C:\scripts\jj\jason.txt| %{new-object -TypeName PSObject -Property @{text = $_}} | 
	ConvertTo-Html -head $a –body "<H2>The J report</H2>" | Out-File C:\scripts\jj\Report.html

Open in new window

Commented:
If I were you, maybe I would like to see this kind of report, what the following code generates (maybe I'm not interpreting the data in the file correctly):
$a = "<style>"
$a = $a + "BODY{background-color:peachpuff;}"
$a = $a + "</style>"
import-csv C:\scripts\jj\jason.txt -Header info, delay, xdelay, mailer, pri, relay, dsn, stat  |
	%{
		if($_.info -match "(?<date>\w{3}\s\d{2}\s\d{2}:\d{2}:\d{2})\s(?<server>\w+)\s(?<token>[^:]+):\s(?<mac>[^:]+):\sto=<(?<email>[^>]+)>")
		{
			$_ | Add-Member -MemberType NoteProperty -Name date -Value ([datetime] "$((get-date).year) $($matches.date)") -PassThru |
			Add-Member -MemberType NoteProperty -Name server -Value ($matches.server) -PassThru |
			Add-Member -MemberType NoteProperty -Name token -Value ($matches.token) -PassThru |
			Add-Member -MemberType NoteProperty -Name mac -Value ($matches.mac) -PassThru |
			Add-Member -MemberType NoteProperty -Name email -Value ($matches.email) -PassThru
		}		
	} | Select-Object -Property * -ExcludeProperty info | %{
		$o = $_ 
		$o | gm -MemberType Properties | %{
			$o.($_.name) = $o.($_.name) -replace "$($_.name)=",""
		}
		$o
	} | 
	ConvertTo-Html -head $a –body "<H2>The J report</H2>" | Out-File C:\scripts\jj\Report.html

Open in new window

Author

Commented:
Wow man, didn't expect that ! :-)

If you'd already gone there ..
its beautiful, but could you help me fit my text into it... see the snip' below- its a little differently formatted than the previous snippet, the difference I guess makes text go into different columns. in the previous snip' there was one email address and in the new one there are multiple to demonstrate that that is what happens sometimes.
also Id love it if you know of a way to hilight email addresses in the  HTML page.


Thanks!
Apr 17 01:11:11 servername sn-mra[2005]: o2EFBRhA019003: to=<user44@domain.com>,<user3@domain.com>,<user2@domain.com>, delay=00:00:01, xdelay=00:00:01, mailer=smtp, pri=193300, relay=relay.microsoft.uk.com. [99.99.99.99], dsn=2.0.0, stat=Sent (ok 1272373889 qp 10191 server-1. Microsoft.con!107030305!30404111!1)

Open in new window

Commented:
Here you are! You can omit colums if you remove the relevant lines (line numbers 8-21).
I added a red font for emails, but you can change the style in row 28.
$a = "<style> BODY{background-color:peachpuff;} </style>"
$ofs=", "
Get-Content C:\scripts\jj\jason.txt |
	%{
		if($_ -match "(?<date>\w{3}\s\d{2}\s\d{2}:\d{2}:\d{2})\s(?<server>\w+)\s(?<token>[^:]+):\s(?<mac>[^:]+):\sto=(?<emails><.+?),\sdelay=(?<delay>\d{2}:\d{2}:\d{2}),\sxdelay=(?<xdelay>\d{2}:\d{2}:\d{2}),\smailer=(?<mailer>\w+),\spri=(?<pri>\d*),\srelay=(?<relayname>(\w+.)+)\s\[(?<relayip>(\d{1,3}\.){3}\d{1,3})],\sdsn=(?<dsn>[^,]+),\sstat=(?<statmain>[^(\s]+)\s(?<statdetail>.*)")
		{
			New-Object -TypeName PSObject -Property @{
				date = ([datetime] "$((get-date).year) $($matches.date)");
				server = $matches.server;
				token = $matches.token;
				mac = $matches.mac;
				emails = "+++" + [string] ($matches.emails -split "[<>,]+" | ?{$_});
				delay = $matches.delay;
				xdelay = $matches.xdelay;
				mailer = $matches.mailer;
				pri = $matches.pri;
				relayname = $matches.relayname;
				relayip = $matches.relayip;
				dsn = $matches.dsn;
				statmain = $matches.statmain;
				statdetail = $matches.statdetail;
			}
		}		
	} | 
	ConvertTo-Html -head $a –body "<H2>The J report</H2>" | 
	%{
		if($_ -match "\+\+\+"){
			$_ -replace "<td>\+\+\+", '<td style="color: #FF0000">'
		}
		else {$_}
	} | 
	Out-File C:\scripts\jj\Report.html

Open in new window

Commented:
Check, if the result include all the rows of the input file. If something is missing I must see that row and refine the regex expression in line 5.

Author

Commented:
Man you are absolutely unbelievable...
I didnt even ask for it!

I really appreciate your help & Time - It woks perfect!
Thanks m8!


Jason.

Author

Commented:
Best help ever given to me!

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial