How to convert some of log file content to table format using powershell

563 views Asked by At

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 = "[email protected]"
$EmailTo = "[email protected]"
$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?

1

There are 1 answers

4
iRon On

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>
"@