none
Script does not run as a scheduled task help PlEASE

    Question

  • I have a powershell script that runs fine in the shell or from the command line. When I schedule it to run from the Windows 2003 scheduler  it does nor run. But the status is 0 in task scheduler. The account that runs the task is a local administrator. Following is the command

    C:\WINDOWS\system32\windowspowershell\v1.0\powershell.exe -ExecutionPolicy Unrestricted -noprofile -File c:\scripts\hpfsotre-del-files-ps.ps1

    The script will cycle through several folders deleting files that are older than 14 days.

    What is wrong?

    Friday, November 30, 2012 10:09 PM

Answers

  • Task scheduler says status is 0 because as far as it is concerned, it started the task, and no errors were encountered doing that.

    So Powershell is starting no problem, the script itself seems to be the issue.

    In order to stop the Powershell session exiting when you run the task, use this...

    C:\WINDOWS\system32\windowspowershell\v1.0\powershell.exe -NoExit -ExecutionPolicy Unrestricted -NoProfile -File "c:\temp\test.ps1"

    That should keep the session open so you can see any errors generated by your script.

    The location of -NoExit matters, it doesn't appear to work if it's not the first parameter.

    As jrv said, if the above step doesn't help, then some tracing will need to be implemented.

    Are all the folders on the same machine? No remote access required?


    Admiral Ackbar says...


    Monday, December 03, 2012 8:24 AM

All replies

  • You will need to place trace statements in the script to determine what is happening.  PowerShell does not end with an exitcode unless you set one.


    ¯\_(ツ)_/¯

    Friday, November 30, 2012 10:20 PM
  • The scheduled job goes to a running status for about 2 sec then ends with no errors in Windows or Powershell. When the powershell script runs in shell or command it runs for 20 minutes. This leads me to think that the task does not run the powershell command.

    Any other ideas?

    Friday, November 30, 2012 11:30 PM
  • The scheduled job goes to a running status for about 2 sec then ends with no errors in Windows or Powershell. When the powershell script runs in shell or command it runs for 20 minutes. This leads me to think that the task does not run the powershell command.

    Any other ideas?

    YOu must place trace statements in the code to log all steps to see what i shappening.

    Did yuo ever wonder about all of the big 'log' files in C:\Windows?  This is why they are there.  All processes that trun without a UI must log to a file or we have no idea what is happening.  This is called tracing or generating a trace filt.

    PowerShell makes this a bit easy by having a trace CmdLet:

    Type: "HELP TRACE"  and read about how to set tracing on and to a specific file.  It can be as detailed as you need and will tell you what is happening or not happening.


    ¯\_(ツ)_/¯

    Saturday, December 01, 2012 12:09 AM
  • Task scheduler says status is 0 because as far as it is concerned, it started the task, and no errors were encountered doing that.

    So Powershell is starting no problem, the script itself seems to be the issue.

    In order to stop the Powershell session exiting when you run the task, use this...

    C:\WINDOWS\system32\windowspowershell\v1.0\powershell.exe -NoExit -ExecutionPolicy Unrestricted -NoProfile -File "c:\temp\test.ps1"

    That should keep the session open so you can see any errors generated by your script.

    The location of -NoExit matters, it doesn't appear to work if it's not the first parameter.

    As jrv said, if the above step doesn't help, then some tracing will need to be implemented.

    Are all the folders on the same machine? No remote access required?


    Admiral Ackbar says...


    Monday, December 03, 2012 8:24 AM
  • None of the above answers worked.

    I got the script to run by putting it in to a batch file and scheduling the batch file in Task Scheduler.

    Thursday, February 28, 2013 9:14 PM