Windows Task Scheduler Error 2147746322 Sending Mail with Attachment

0

When sending a *.txt attachment using the Windows Task Scheduler's Send Mail action, the job fails with error id 2147746322.

When sending using PowerShell's Send-MailMessage command with exactly the same parameters the mail & attachment are sent successfully.

Symptoms seem the same as mentioned here: http://www.networksteve.com/forum/topic.php/Task_Scheduler_-_Can%27t_Send_Email/?TopicId=18073&Posts=0

JohnLBevan

Posted 2015-02-12T14:06:51.850

Reputation: 357

Answers

0

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.

JohnLBevan

Posted 2015-02-12T14:06:51.850

Reputation: 357