Share via


Append daily PowerShell output to HTML file

Question

Thursday, April 30, 2015 2:29 PM

Hi, hope this is the right place to post this.

I have a script that generates the number of emails that a certain email address receives per day. A simple ‘Get-MessageTrackingLog’ command and this is fine and it outputs the figure to a text file, and I even have it appending to the file each day it runs so have a nice running list.

Example:

$From = (get-date 00:00:00).AddDays(-1)
$To = $From.AddHours(23).AddMinutes(59).AddSeconds(59)

(Get-TransportServer) | Get-MessageTrackingLog -ResultSize Unlimited -Start $From -End $To
-Recipients "[email protected]" -EventID "DELIVER" | ForEach { $Rec++ }$Rec | out-file -append -filepath ........

However I would like to smarten it up a bit and append the values to a table in an html file. I can get it to export the values with table headings, but each time it appends to the file it writes the table headings and title again as it's always calling $output, so I end up with duplicate everything, whereas I just want the new values recorded for the next day added to the table.

 

$From = (get-date 00:00:00).AddDays(-1)
$To = $From.AddHours(23).AddMinutes(59).AddSeconds(59)

$output="<html><body>
 <font size=""1"" face=""Arial,sans-serif"">
   <h4 align=""center"">Daily Total</h4>
   <h5 align=""center"">Shows the total amount of emails sent to [email protected] each day</h5>
 </font>"

$output+="<table align=""center"" width=""100%""><tr><td valign=""top"" width=""100%"">
 <table align=""center"" border=""0"" cellpadding=""3"" font-size:8pt;font-family:Arial,sans-serif"">
  <tr bgcolor=""#92caec"">
   <td>Day</td>
   <td>Date</td>
   <td>Emails Received</td>
  </tr>"

(Get-TransportServer) | Get-MessageTrackingLog -ResultSize Unlimited -Start $From -End $To
-Recipients "[email protected]" -EventID "DELIVER" | ForEach { $Rec++ }

$output+="<tr"
$output+="><td>" + $From.DayOfWeek + "</td>"
$output+="><td>"+ $From.ToShortDateString() + "</td>"
$output+="><td>" + $Rec++ + "</td>"
$output+="</tr>"

$output+="</table></td>"
$output+="<td valign=""top"" width=""100%"">"

$output+="</table>"
$output+="</td></tr></table></body></html>"

$output | out-file -append c:\Test.html

$From = $From.AddDays(1) 
$To = $From.AddDays(1)

All replies (6)

Thursday, April 30, 2015 4:35 PM ✅Answered

$html=[xml](dir \test\*.txt| select mode, name, length, lastwritetime | ConvertTo-Html -fragment)\
$rowcount=$html.table.tr.count
$rows=$html.table.tr[1..$rowcount]

\(ツ)_/


Thursday, April 30, 2015 3:20 PM

$html=(Get-TransportServer) |
**     Get-MessageTrackingLog** -ResultSize Unlimited -Start$From -End$To -Recipients "[email protected]" -EventID "DELIVER" |
**     ConvertTo-Html**

Now you have a table and can email it as the body of and email.

 

\(ツ)_/


Thursday, April 30, 2015 3:43 PM

Thanks, but that doesn't help me append that to an existing table. I can already output the values, but each day it needs to append the newest ones to the current table headings in the file.

Your script also gives me all the emails, mine is just getting the count, so the number of emails that the email specified has received that day.

So for example, my html file will look like this with those three column headings. Each day it then appends the Day, Date and Number on a new line in the table, so it builds up each day.

Day            Date           Number
Thursday       30/04/15       50
Friday         01/05/15       55

Thursday, April 30, 2015 4:05 PM

There are many ways to do this.  We could use XML or we can just remove the opening <table> statement. XML is best.  YOu have to load the file and add the new rows to the existing table using XML.  It would take about 5 lines of script to do that.

\(ツ)_/


Thursday, April 30, 2015 4:28 PM

Nice. So just remove the opening <table> statement and that's it? Not sure what you mean. Also not really used xml, so could you maybe give me some pointers to how that might work/look?


Thursday, April 30, 2015 5:48 PM

Thanks, will give it a go.