Proper export from txt to html in Powershell

The name of the picture


Proper export from txt to html in Powershell



I have a script, which stores output in txt and I need to convert this output into html.



The format of txt file is following:


This file is generated for commits from: 2018-07-15 to: 2018-07-21 for branch: repositories contains: mineq


=============somerepo=============


Branch is development
1234567 Merge pull request #1227 from qp-10421_service_version_information
1234567 Merge branch 'development' into qp-10421_service_version_information
1234567 merged with development
1234567 QP-2071: update packages




=============Someotherrepo=============


Branch is development




=============MineqConfigApi=============


Branch is development
1234567 QP-10881 Remove WindowsVpnSettings service
1234567 Merge pull request #9 from quarti/QP-10881
1234567 QP-10881 Set SshClient ConnectionTimeout 10 minutes and Change sql query to skip Deleted Assets



To send this text into html, I use following code:


$SourceFile = "$env:WORKSPACEcommits.txt"
$TargetFile = "$env:WORKSPACEcommits.html"
$TextData = Get-Content $SourceFile
$LineData = $TextData -join ''
New-Object psobject -Property @{Text = $LineData} | ConvertTo-HTML | Out-File $TargetFile



But the output is following -

How to rework script, to receive exact output, with linebreaks, as in txt file?





is it just line breaks you are missing? if so just change your join statement.
– WayneA
53 mins ago





I'm new in PS - can you give me an example of proper join statement?
– Vasiliy Vegas
51 mins ago





"n" will inset a [newline] character "r" will insert a [return] character if you are not sure which to use, you can get the current default by doing: $nl = [Environment]::NewLine
– WayneA
48 mins ago




n" will inset a [newline] character "





DOH! looks like i need to somehow escape that first character. It is a [tick] follewed by n or r. [tick] is the character in the far left top of a US keyboard. It looks kind of like a backwards apostrophe
– WayneA
40 mins ago




1 Answer
1



I know that ConvertTo-Html is cool, but if I want more control over generated HTML I do it manually:


$content = cat File.txt -Raw
$title = 'My HTML'
$html = @"
<html>
<head><title>$title</title></head>
<body>
<pre>$content</pre>
</body>
</html>
"@
$html | Out-File 'file.html'



pre tag is used in HTML to preserves preformatted element.


pre





Awesome! Thanks!
– Vasiliy Vegas
20 mins ago






By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

Popular posts from this blog

Arduino Mega cannot recieve any sketches, stk500_recv() programmer is not responding

Visual Studio Code: How to configure includePath for better IntelliSense results

C++ virtual function: Base class function is called instead of derived