none
Scheduled task fails on script (but script is working fine) RRS feed

  • Question

  • Hi,

    I want to run a script via scheduled task. It runs fine when ran in Powershell (ISE).
    When ran in scheduled task, it pops up a Powershell but then directly stops. No output at all. Have inserted a try catch which  would send an email with error (which works fine when not launched in scheduled task) but no email.

    Have scheduled a similar script with same settings (same user, same priviliges, same trigger) and that runs fine.

    My script also runs fine when I copy paste what's in the scheduled taks = powershell.exe -file 'E:\Scripts\MyScript.ps1'

    Please advise howto troubleshoot.
    J.


    Jan Hoedt

    Thursday, October 27, 2016 3:39 PM

Answers

  • Solved it by using -noexit in command. Then I had a window in which I could see what the problem was, actually just changed ' to " and it works fine.

    Now what I don't get is that it doesn't find modules which are in my profile script. The task runs with the same credentials as I run my script so it should take my profile and so also have the modules loaded ....

    Jan Hoedt

    • Marked as answer by janhoedt Monday, October 31, 2016 11:21 AM
    Monday, October 31, 2016 11:21 AM

All replies

  • If the script works when you run it interactively, then you don't have a scripting question but rather a security or task scheduler question.


    -- Bill Stewart [Bill_Stewart]

    Thursday, October 27, 2016 3:51 PM
    Moderator
  • I'm not sure if this is still a scripting question. 
    But, maybe you should use the whole path to your powershell.exe program file. I remember vaguely some talk about issues regarding launching a script via task scheduler in a 64-bit environment, when there is a 64-bit and a 32-bit version of powershell around.

    wizend 
    Thursday, October 27, 2016 4:37 PM
  • There's a few things you should check.

    Open the task.  Under the General tab | Security options, who is the task running as? Ensure that the user account or group has the permissions needed to execute the script.  If you're querying active directory or something outside of the local server, you probably wouldn't want to run it as SYSTEM

    Always consult your history tab, and see if there is any obvious logs. 

    Ensure "Run whether user is logged on or not" is checked.

    Check Run with highest privileges. ISE will run as the user you're logged in as.

    It would also be helpful to see what you're running script wise, and screen shots of the task, so we can see what's been configured. 

    Thursday, October 27, 2016 4:47 PM
  • Thanks for that input, very valuable! However, didn't solve the problem.

    Jan Hoedt

    Monday, October 31, 2016 8:07 AM
  • There is not much to configure on the task. Of course the user running the task has enough priviliges, if it wouldn't it would be very obvious in error.
    History tabs doens't show anything what helps. Also highest priviliges are checked.
    Like I mentioned: I have other tasks running which run perfectly but this one in particular does not run and doesn't give any indication what is not functioning.


    Jan Hoedt

    Monday, October 31, 2016 8:09 AM
  • Solved it by using -noexit in command. Then I had a window in which I could see what the problem was, actually just changed ' to " and it works fine.

    Now what I don't get is that it doesn't find modules which are in my profile script. The task runs with the same credentials as I run my script so it should take my profile and so also have the modules loaded ....

    Jan Hoedt

    • Marked as answer by janhoedt Monday, October 31, 2016 11:21 AM
    Monday, October 31, 2016 11:21 AM
  • That is odd, but I always always perform an import-module  inside of my scripts that I'm concerned about, and direct it to the path where my .psm resides.   You could always test by using get-command, for the module being used, and see what displays when it's used as a scheduled task.. Just to make sure all the functions are being grabbed properly.
    Monday, October 31, 2016 3:25 PM