So I've resorted to this method to get a PowerShell script to return a non-zero exit code:
Write-Output "Testing for errors..."
Write-Error "This is an error"
return
If i switch on "Extra Task Debugging" i get all kings of strange ASCII at the start of the output!
Generally, I've found that PowerShell scripts can only return two things that VisualCron can pick up on:
1) Write-Output == StdOut or "Output"
2) Write-Error == StdErr or "Output (stderr)"
(If I'm wrong, please correct me as that functionality would be useful in several of my scripts.)
To get different exit codes, you could probably either have the script connect to the server through the API and modify the Exit code in the base task object (which is probably the nuclear option/doesn't work if the task is run as multi-instanced; I'm also not sure if that field is exposed in the object through the API) or use the "On Error" options combined with returning the exit code and message in the StdErr.
To expand on the second option, to just have the task's "On Error" return a catch-all custom exit code by checking for a non-empty/null StdErr return and leaving the real exit code in the StdErr, you might want to do the following:
In the script, add this block to your try-catch statements or wherever you're doing the error handling:
$errorCode = 1
Write-Error("Exit Code: $errorCode`n$errorMsgString")
exit 1
You would have to build your $errorMsgString yourself. Look at
this blog post for more information on what fields can be dragged out of the error object and returned in your error message.
Locate all locations in the script that return non-fatal error output, which includes cmdlet/function calls that are not assigned to a variable or being captured and handled in some way. Append "> Out-Null" to the end of the cmdlet/function calls to redirect their screen output/return to nowhere. This'll allow you to correctly target the strings you want returned.
To set the "On Error" options:
1) Navigate to the "On Error" tab in the task.
2) Navigate to the bottom half of the pop-up under "Output/Variable error handling".
3) Select "Raise error" from the drop down menu.
4) Set the custom error code.
5) Select "If error output" (if the error string is returned as Write-Error).
6) Select, under "Condition", "Not Equal (!=)".
7) Leave the bottom field blank.
I think what this should do now is throw an error if anything is returned as StdErr. From there, you can now locate your tasks that exit with your custom catch-all exit code and then read their StdErr string in the GUI/API for the real exit code.
That said, you lose access to non-handled, non-fatal error messages in the StdErr field if you do this, which may be undesirable.
The upside is that you would now be able to differentiate between jobs/tasks that return a VisualCron exit code and ones that are returning your exit code (in the StdErr). Basically, it's your solution plus a little extra.
Edited by user
2017-08-17T17:39:54Z
|
Reason: Not specified