0

So i have the below script which basically 'imports' the log file from a robocopy task and then emails me the last 14 lines ( which are basically the stats at the end). However i cant seem to get the format correct. Here is the script :

$logfile_offsite = "C:\offsitebackup.log"
$log_contents = Get-Content $logfile_offsite
$date = Get-Date -Format g
$footer = get-content $logfile_offsite | select-object -Last 14  

#Send email 
$smtp_server = "smtp.server.com"
$address_to = "[email protected]"
$address_from  = "[email protected]"

Send-MailMessage -SmtpServer $smtp_server -To $address_to -From $address_from -Subject " Offsite Backup $date "  -Body " $footer "

So when the email comes through the original formating where it looks neat is screwed up. Is there any way to fix this?

3
  • It's definitely not Outlook removing what it deems as unnecessary carriage returns? It has a habit of screwing up plain text e-mail. Oct 11, 2013 at 20:13
  • 2
    It might not hurt to know what the 14 lines look like in the log file, as well as what the received email body contains and what you expected it to look like.
    – jscott
    Oct 11, 2013 at 20:28
  • In addition to what @jscott said, this wouldn't be an issue relating to word wrap would it?
    – Colyn1337
    Oct 11, 2013 at 20:41

1 Answer 1

1

Without a significant amount of detail about your mail client receiving the email and the servers that transport your email (any sanitation that may be happening), I'm gonna give two suggestions:

  • Check whether your mail client is using a fixed-width font. Your console output likely uses a fixed-width font, whereas your mail client may not, even for a plain text email such as this one. From this perspective, assuming your log file contains the text in the format you were expecting it, then so should your mail client.

  • The help guide for the Send-MailMessage suggests that the -Body parameter is expecting a String object. It's not always easy to tell what Get-Content and Select-Object return, but in your case you end up with an array (collection), because that's how you're treating your input. So what does the CMDlet do to make a single string out of your array of strings? Maybe it just joins them without a space. Try this line, which will join the array of strings with a carriage return and a newline. This way you are telling PowerShell what you want it to do:

    $footer = (get-content $logfile_offsite | select-object -Last 14) -join "`r`n"

2
  • But as @jscott said, you would make it easier for people if you at least explained how it differs from what you were expecting if not finding a way to include an example.
    – john
    Oct 11, 2013 at 22:05
  • thats great, thanks for everything , will try the suggestions Monday morning and let everyone know
    – NickDa
    Oct 12, 2013 at 21:51

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .