Powershell script works in Powershell but fails in Task Scheduler

3

I have a PowerShell script that sends an email via SMTP. The script runs fine inside Powershell ISE, but fails in Task Scheduler. I am on Windows Server 2012. I have other Powershell scripts that I run on this server using the exact same setup, but those scripts do not send an email. The return code I see in Task Scheduler is (0xFFFD0000) and I cannot find any information on this. I have the task set to run with highest privileges and I have checked that the executionpolicy is RemoteSigned. Anybody run into this before?

Here is the command in the task:

powershell -f "c:\scripts\EmailTest.ps1"

Here is the script:

$EmailFrom = "user@domain.com"
$EmailTo = "someone@somewhere.com"
$Subject = "Email Subject" 
$Body = @"
Person,

Some message here

Thanks,
User
"@

$SMTPServer = "smtp.domain.com" 
$SMTPClient = New-Object Net.Mail.SmtpClient($SmtpServer, 25) 
$SMTPClient.Credentials = New-Object System.Net.NetworkCredential("user@domain.com", "password"); 
$SMTPClient.Send($EmailFrom, $EmailTo, $Subject, $Body)

Update: I was able to resolve the issue. Apparently I had an additional line in the script that was commented out. I'm not sure why this would cause an error but once I removed that commented out line it ran fine in Task Scheduler. the comment looked like this and was just below the other $EmailTo declaration in the above script:

#$EmailTo = "someone@somewhere.com"
smtp
powershell-2.0
asked on Stack Overflow Dec 11, 2012 by shark92651 • edited Dec 11, 2012 by shark92651

3 Answers

7

I found another possible issue while looking at a similar problem. I was unable to execute a PowerShell script as a Task Scheduler action, even though the script ran correctly when logged into Windows as the target user and running within PowerShell.

Task Scheduler would consistently display the 0xFFFD0000 error when I nominated the script in the task's action arguments using what I believed to be normal PowerShell quoting rules:

-ExecutionPolicy Bypass -File 'D:\full path\to\script.ps1'

PowerShell acquiesced and Task Scheduler fired off the task immediately and without issue when I changed the quotes I used from single to double:

-ExecutionPolicy Bypass -File "D:\full path\to\script.ps1"

Dropping to a command prompt and executing the full command immediately revealed the problem:

D:\>C:\Windows\System32\WindowsPowerShell\v1.0\powershell.exe -ExecutionPolicy Bypass -File 'D:\full path\to\script.ps1'

Processing -File ''D:\full path\to' failed: The given path's format is not supported. Specify a valid path for the -File parameter.

Notice the strange use of two single quotes before the path and one single quote after.

The moral of the story: When feeding the full path of a script to PowerShell as a command line parameter, use double quotes!

answered on Stack Overflow Mar 26, 2014 by SJSpain
5

I was receiving the same error and ultimately I had a different issue - the optional start in directory setting wasn't applied.

Essentially, I was running a .bat file - c:\tasks\process.bat

This .bat file referenced multiple ps1 scripts that were in the tasks directory and the references were just by file name (not the full directory). On the action tab in task scheduler, there is a Start in (optional) field that I had not populated. Setting it to c:\tasks allowed the references to function properly.

answered on Stack Overflow Aug 30, 2013 by mcook
0

First of all you have to check "ExecutionPolicy" configured on your machine. to do so, check default values by following this link https://technet.microsoft.com/en-us/library/hh847748.aspx

I fixed my probleme by using this command: On "Add arguments" option I put:

"-Executionpolicy Bypass -command "& 'T:\deleteOldMessages.ps1' "

1

and

1

answered on Stack Overflow Nov 2, 2016 by Sam Doxy

User contributions licensed under CC BY-SA 3.0