Powershell script works in Powershell but fails in Task Scheduler

30,596

Solution 1

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!

Solution 2

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.

Share:
30,596
shark92651
Author by

shark92651

Updated on November 03, 2020

Comments

  • shark92651
    shark92651 over 3 years

    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 = "[email protected]"
    $EmailTo = "[email protected]"
    $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("[email protected]", "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 = "[email protected]"