Unable to run PS script as Scheduled Task

This topic contains 4 replies, has 4 voices, and was last updated by  Drew 4 years, 11 months ago.

  • Author
  • #6670

    J Henke

    I have simple PS script that collects information and writes a file and I want to run this as a scheduled task.

    I have execution policy set to remotesigned, and can run this with two different admin users from the powershell console.  However, when I run this as a scheduled task, it returns back with no errors, but it never really runs (i.e. no file is written)

    To debug this, my first step was to get this running simply from the command line (CMD).   Based on numerous examples I ran this simple command:

    Powershell.exe e:\scripts\myscript.ps1 – which worked straight away!

    Is set this up to run as a scheduled task again:

    Program:= Powershell.exe

    Argument:  e:\scripts\myscript.ps1 

    But again, although it runs to succes, it never produces any outuput as a scheduled task

    Based on many examples I have tried changing the arugment to include an ampersand

    powershell.exe -noexit & e:\scripts\myscript.ps1 

    Interestingly enough, when I tried that syntax from a CMD window I got this message –

    Ampersand not allowed. The & operator is reserved for future use; use "&" to pa
    ss ampersand as a string.
    At line:1 char:25
    + powershell.exe -noexit & < <<<  e:\scripts\taskquery.ps1
        + CategoryInfo          : ParserError: (:) [], ParentContainsErrorRecordEx
        + FullyQualifiedErrorId : AmpersandNotAllowed

    I have also experimented with numerous combinations preceeding my script aruguent such as -nologo -noninteractive –Command.

    I have also tried the fully qualified path to powershell.exe, and pointing the start in argument to the powershell.ex folder (and even my scripts folder)

    After all of this I gave up on running native PS as a scheduled task and saw examples of running it within a CMD, or bat file.  So, I made a simple one that mimmicked my original working command.  Simply "Powershell.exe e:\scripts\myscript.ps1". 

    When I ran this in the command window it worked.  Unfortunately, setting that as a scheduled task – pointing to the CMD file directly (no arguments) it ran with no errros, but didn't write the file (again).

    I can run scheduled tasks for othe cmd files, but seemingly not for powershell – either directly (i.e. powershell.exe with a .ps1 argument) or from a cmd file – both of which will run manually.

    I am running Windows Servder 2008 R2

    PS Version:

    Name             : ConsoleHost
    Version          : 2.0
    InstanceId       : d21c127e-3dc9-40aa-b789-ff64703d0546
    UI               : System.Management.Automation.Internal.Host.InternalHostUserInterface
    CurrentCulture   : en-US
    CurrentUICulture : en-US
    PrivateData      : Microsoft.PowerShell.ConsoleHost+ConsoleColorProxy
    IsRunspacePushed : False
    Runspace         : System.Management.Automation.Runspaces.LocalRunspace

    At this point I am stumped and have run out of things to try.  Any suggestions would be appreciated.


    Jeff Henke



  • #6682

    Richard Siddaway

    Your script runs if invoked manually but not if invoked as a scheduled task. That suggests that the scheduled task isn't configured correctly. I routinely run powershell scripts from scheduled tasks. I've found the task scheduler to be somewhat tempremental

    First things to check
    – is your scheduled tasks set to run with highest priority (Elevated)
    – does the scheduled task actually start the powershell script? Put a simple test at the top of the script to indicate its started (write to event log is sufficient)
    – does the script need to load any modules and is that included
    – does the account running the task have the correct permissions.

    If you can find those things out and come back with the info I'll see what else comes to mind

  • #6687


    In the action dialog box I usually start the task as follows:

    Program/Script: powershell
    "Add arguments (optional)": -command "&'M:\SkyDriveBackup\SkyDrive\BackupScripts\Complete backup.ps1'"
    See attached picture it works like a charm for me.

    Can't remember if the "run with highest privileges" is nesseary, I just sets it for my tasks.

    Regards Lars.

  • #6721

    J Henke

    Thanks for your helpful suggestions.

    I wasn't thinking it was a problem with the script itself since it ran from powershell console with same ID that I was using for the scheduled task.  Plus, I did see in the event logs and task history that it kept returning with error code zero.

    But on your advice, I tried running a different PS script – a simple one to touch the date/timestamp of a file.  It worked splendidly as a scheduled task.  I was somewhat happy to see that because it meant things were in place.

    On the other hand, it did mean I would have to look more closely at my script.  The simple idea was to write all the scheduled tasks to a file - the attributes, last / next run...etc.

    We have tended to go crazy with these over time and we wanted to do some cleanup, and stay on top of it going forward.  So, the ps script does just that – gets the tasks from the "\" folder and writes all the info to a file that gets processed into a larger data source through some other process.   All well and good – except that it didn't work.

    As I mentioned, on this particular server where I was testing this, I noticed I was running PS version 2.  So, I went to a different serer where I had PS 3 and same script worked as a scheduled task!  So the difference was between PS 2 and PS 3.

    Looking more closely at the output from I got from the PS 3 server I saw what is likely the problem.  

    Essentially, I am running a scheduled task to report back on scheduled tasks.  Somewhere in the middle of that, one of the tasks it reports back on is "itself".  I noticed that in output run on a PS 3 machine, it would write a status (message number) that indicated it was currently running.  I am wondering if maybe the PS 2 environment the idea of reporting on itself was the problem.

    I have since been able to replicate this on a few more servers – where they are PS2, the script "runs" but give no output.  On the PS 3 servers it runs every time.

    So yes, thanks to your encouragemen, I put my pride aside and properly looked at my script and found it was the culprit.

    I have obviously since rethought how to collect scheduled task info – but that is beyond this post.

    Thanks again!








  • #9241


    Hi, I am experiencing what Jeff described above..

    I have simple PS script that collects information and writes a file and I want to run this as a scheduled task. Script is running on a Win2008 SP2 Server with Powershell v2 and the IIS Snap-in.

    I have execution policy set to remotesigned, and can run this from the powershell console. However, when I run this as a scheduled task, it returns back with no errors, but it partially runs as only some of the output doesn't get produced.. The script starts, writes some lines to a file and then emails me the output.

    The command that fails to write output (when scheduled) to file is from the results of a GET-PROCESS command.

    Thanks for any suggestions or hints.

You must be logged in to reply to this topic.