locked
Task Scheduler with PowerShell - Task completed with Return code 0 RRS feed

  • Question

  • Hi,

    We have on Task scheduler job to run powershell script but it is always completed with return code 0 and not running the script. We have execution policy on the server is Unrestricted

    Following is the Action set for the Powershell script.

    1. Start a Program

    2. C:\Windows\System32\WindowsPowerShell\v1.0\powershell.exe

    3 -NoProfile C:\Script\XXX.ps1

    Can any one please suggest in to this?

    Regards,

    Sapan Shah

    • Moved by Bill_Stewart Friday, March 15, 2019 4:52 PM This is not "scripts on demand"
    Monday, December 24, 2018 5:31 AM

All replies

  • Return code 0 says that the task ran successfully with no errors.  This indicates that the script ran successfully.

    A script must fail at the OS level or return an exit code if it detects a failure.

    help about_Language_Keywords


    \_(ツ)_/

    Monday, December 24, 2018 5:39 AM
  • Hello,

    Yes It is correct that return code 0 means task ran and completed successfully. But the PowerShell script associated with it  not running.

    And one more thing that I would like to add here at the same time another 2 PowerShell script are also running fine with task scheduler but this 3rd instance in not running.

    Is there anything that I need to change in Action of Task scheduler?

    Regards,

    Sapan Shah


    Monday, December 24, 2018 7:57 AM
  • What makes you think it is not running.  The task scheduler is telling you that it did run.  I suspect the script does not work the way you expect it to work.

    Perhaps you can ask the author of the script to help you with understanding how it works and why it seems to not do what you expect.


    \_(ツ)_/

    Monday, December 24, 2018 8:25 AM
  • first try to run the script manually, and see if it perform the desired task or shows an error. 
    Monday, December 24, 2018 8:55 AM
  • Hello,

    First of all, It is a Backup PowerShell Script.

    Yes if we run script manually or run the Task from Task Scheduler manually, It is running as expected. But not as per schedule and completed the task in 2 minutes with code 0

    Regards,

    Sapan Shah


    Monday, December 24, 2018 9:25 AM
  • you need to again check the setting mentioned on this schedule task. or you can add logs in the script that will log the task in some log file, from where you can see what is the issue.

    add try catch and output the error to the a text file. This will give more idea why it is happening.

    Monday, December 24, 2018 9:41 AM
  • Add the following to the start of the problematic script to capture what its doing:

    Start-Transcript -Path 'C:\temp\problem-script.txt' -Append

    or if you simply want to check the script is actually running, code in some event log entries at the start and end of the script:

    ### CREATE CUSTOM EVENT LOG SOURCE ###
    $EventSource = 'PowerShell Scheduled Tasks'
    $source = [Diagnostics.EventLog]::SourceExists("$EventSource")
    if ($source -eq $false) {New-EventLog -LogName 'Windows PowerShell' -Source "$EventSource"}
    
    ### scriptname ###
    $scriptname = '<SCRIPT-NAME-HERE>'
    
    
    ### SCRIPT START EVENT LOG ENTRY ###
    Write-EventLog  -logname 'Windows Powershell' -source "$EventSource" -eventID XXXX -entrytype Information -message "$scriptname Scheduled Task script has started"
    
    
    ### EXISTING SCRIPT CODE HERE ###
    
    
    ### SCRIPT COMPLETED EVENT LOG ENTRY ###
    Write-EventLog  -logname 'Windows Powershell' -source "$EventSource" -eventID XXXX -entrytype Information -message "$scriptname Scheduled Task script has Completed"



    MCSA Server 2012/2008 MCITP Exchange 2010

    Monday, December 24, 2018 10:57 AM
  • Hello,

    First of all, It is a Backup PowerShell Script.

    Yes if we run script manually or run the Task from Task Scheduler manually, It is running as expected. But not as per schedule and completed the task in 2 minutes with code 0

    Regards,

    Sapan Shah


    What is a PowerShell backup script?  There is no such script in PowerShell.  Who wrote the script? What doe sit backup?

    Your question is far to vague for anyone to understand what it is that is not working.

    Start by reading the following link and follow the recommendations.  Ask your question in a correctly.


    \_(ツ)_/

    Monday, December 24, 2018 11:28 AM