After a lot of investigation we found that there were some line feeds in the text file being attached that were not proceeded by carriage returns (i.e. \n instead of \r\n).
Removing these stand-alone line feeds resolved the issue.
Should it be of use to others, here's the script to remove invalid characters in a file:
File: RemoveInvalidLineBreaks.ps1
if ($args.length -gt 0 -AND
$args[0] -ne $NULL -AND
$args[0] -ne "")
{
$fn = $args[0]
(Get-Content $fn) | Set-Content $fn
}
else
{
write-error "No filename supplied"
}
Example Usage:
(if called from windows batch file)
powershell -ExecutionPolicy RemoteSigned ./RemoveInvalidLineBreaks.ps1 D:\TEST\myLogFile.txt
The script works because Get-Content
, when used without the -Raw
parameter (which is only available from PS3 onwards) reads the file one line at a time, treating both \r\n
and \n
as end of line characters; but when writing back in Set-Content
only using the Windows \r\n
line end convention; thus all breaks are replaced.