-1

I am trying to read some of bottom lines from my log file and send the content as a table in the email.

Log File:

      0 \\Server\Copy\db_materials\TEST\

               Total    Copied   Skipped  Mismatch    FAILED    Extras
    Dirs :       508         0       508         0         0         0
   Files :      9109         0      9109         0         0         0
   Bytes :   1.115 g         0   1.115 g         0         0         0
   Times :   0:01:03   0:00:00                       0:00:00   0:01:03
   Ended : Thursday, March 19, 2020 11:03:22 PM

Powershell Script:

$body= (Get-Content -Path D:\logs\logfile.log -Tail 8 | Out-String) |ConvertFrom-Csv | ConvertTo-html | Set-Content D:\RMSscripts\Sample.html -Encoding UTF8

$EmailFrom = "xyz@abc.com"
$EmailTo = "myemail@abc.com"
$EmailSubject = "Report"
$SMTPServer = "smtp.server.com"

end-MailMessage -Port 25 -SmtpServer $SMTPServer -From $EmailFrom -To $EmailTo -Subject $EmailSubject -Body $body-Bodyashtml;

In the email, the content are pasted as below.

       Total    Copied   Skipped  Mismatch    FAILED    Extras
    Dirs :       508         0       508         0         0         0
   Files :      9109         0      9109         0         0         0
   Bytes :   1.115 g         0   1.115 g         0         0         0
   Times :   0:01:03   0:00:00                       0:00:00   0:01:03
   Ended : Thursday, March 19, 2020 11:03:22 PM

Expected is: Expected report in email

How to convert the raw content into table format?

  • Do you really use PowerShell 1.0? Did you try looking into the Excel module of PowerShell? Maybe this can help you. Otherwise you could create another column for your Headers. Looks a little off but should work. – Alex_P Mar 20 '20 at 07:54
  • yes i am using powershel 1.0. I tried using Excel but module didnt work. – Purushothaman Mar 20 '20 at 09:02
  • There are too many discrepancies to easily recreate a table from your log file. Rather then creating a html table, I would look for other export capabilities of your application or consider to just wrap the concerned text in a [`Preformatted text
    `](https://www.w3schools.com/tags/tag_pre.asp) tag. Anyways, if you really want to go along with trying to build a table out of this, have a look at: [How to remove nth column from the text table in powershell?](https://stackoverflow.com/q/59541667/1701026)
    – iRon Mar 20 '20 at 09:12
  • To [determine installed PowerShell version](https://stackoverflow.com/questions/1825585/determine-installed-powershell-version): `$PSVersionTable.PSVersion` – iRon Mar 20 '20 at 09:16

1 Answers1

0

As suggested in the comment: there are too many discrepancies to easily recreate a table from your log file. Rather then creating a html table, I would consider to just wrap the concerned text table in a Preformatted text <PRE> tag:

# $Table = (Get-Content -Path D:\logs\logfile.log -Tail 8 | Out-String)
$Table = @'
               Total    Copied   Skipped  Mismatch    FAILED    Extras
    Dirs :       508         0       508         0         0         0
   Files :      9109         0      9109         0         0         0
   Bytes :   1.115 g         0   1.115 g         0         0         0
   Times :   0:01:03   0:00:00                       0:00:00   0:01:03
   Ended : Thursday, March 19, 2020 11:03:22 PM
'@

$Body = @"
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"  "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title>HTML TABLE</title>
</head><body>
<pre>
$Table
</pre>
</body></html>
"@
iRon
  • 20,463
  • 10
  • 53
  • 79
  • Thank You so much @iRon .. it is writing in a good table format. But there is no border for table. i tried CSS to get border, but couldn't. – Purushothaman Mar 23 '20 at 05:23
  • As it isn't easy to reconstruct the columns, you can't created the borders around the cells, but to put a border around the **whole** table, you might consider to wrap the preformatted text in a single table cell:
    $Table
    
    – iRon Mar 23 '20 at 09:26
  • Perfect! Thank You so much @iRon I used div and added css background-color to differentiate. – Purushothaman Mar 23 '20 at 12:35
  • 1
    If his answer was helpful, upvote it. If it solved the issue, accept it as the answer. – Doug Maurer Oct 03 '20 at 22:36