Forum Discussion
Externally trigger a .ps1 to exit
- Feb 09, 2022taking action within the script when an external action has stopped the scheduled job - no, this isnt possible.
the ps1 that is stopped will exit with a non-success error but as you are not launching that script from a process that can handle that error code then you have no way to check the code and take any action. Also, you are ending the task and not the actual script itself so you don't have the separation needed - the task doesnt continue processing at all to handle the exit code.
if you start a cmd session and then type powershell you will be running a powershell session in a command prompt session.
If you now type exit 20 at the PS> prompt you will intentionally exit the powershell session with exit code 20 and revert to the command prompt session
At the prompt run the command echo %errorlevel%. The result will be 20 - the powershell session exit value. You can consume this in whatever logic you have in the calling process...
this value is passed between the sessions as the 'inner' powershell session closed gracefully with an exit code. the same isnt true for when notepad.exe is closed with Stop-Process.
even if you have a cmd/bat script launch your ps1, if you end the task then there is nothing running to handle any onward actions.
I guess we have reached the point to ask what you are actually doing and trying to achieve so that we can provide a better option for you rather than proceed down the rabbit hole of process launching and exiting.