简体   繁体   中英

why task scheduler status is success even if the powershell script is getting failed

Basically i have scheduled a task in windows task scheduler.

In the task i have scheduled a powershell script

in the program/script path we have given powershell path and in the argument path we have given

-command "& C:\Users\xxxx\Desktop\1.PS1"

I was checking if the task fails or not on powershell script failure.

so the powershell script is getting failed but the last run status says

"The operation completed successfully"

How to configure my task such that it will return fail if powershell script does not run successfully?

edit

I have 3 tasks(all 3 are powershell scripts having parameters) basically i have created a custom trigger for task 2 and task 3

ie if task1 is success then the task 2 will be triggered and if task 2 is success then task3 will be triggered.

while testing the above scenario, evenif the powershell script used in task 2 returns error(intentionally done error in code to check the scenario), the last run status says the operation completed successfully and the task 3 task getting triggered

Instead of the current situation, why do you not have 3 Scheduled Tasks. The first runs the first script, and if the script itself deems itself successful, runs the 2nd Scheduled Task itself? For example, using Start-ScheduledTask ( https://technet.microsoft.com/en-us/library/jj649818(v=wps.630).aspx ).

This way, each of your scripts could check themselves for issues, and if none are found, can call the next task. This has the additional bonus of letting you have full control over which scheduled task to run, and when.

Based on the comments in this thread and my own testing, it sounds like it's not possible to get the scheduled task to log itself as failed by the failure of the script because the purpose of the task is to kick off the program, which it does successfully.

However, it is possible to return an exit code to the scheduled task. The exit code will be logged in the event history (Information Level). As stated in this thread , the return code must be referenced in the parameters with which the scheduled task calls the script:

-ExecutionPolicy Bypass –NoProfile –Command "& {C:\ProgramData\ORGNAME\scripts\SetDNS.ps1; exit $LastExitCode}"

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM