Updated PowerShell wrapping how-to with Try/Catch block to fix failing silently #6
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
With the current recommended PowerShell wrapping, if it cannot run the script (due to a missing script or bad script i.e. misplaced characters/bad syntax), it returns 0 and fails silently. This is due to $lastexitcode returning a 0 when it is unable to even start to run a script.
Failing silently is never a good default for monitoring software, and this can be a big issue when mass deploying custom checks as it is impractical to manually verify each one.
This PR updates the how-to documentation to include a Try/Catch block on the PS wrappings. This way if powershell is unable to run the script, it exits with a 3 and reports the $error. Otherwise, the script will run and return it's $lastexitcode.
Testing and discussion occurred here originally. https://support.nagios.com/forum/viewtopic.php?f=6&t=51591
I looked if the default nsclient.ini included the wrapping and it did not, so it appears the documentation is the only thing that needs to be updated. While updating the documentation I fixed some wording and a few typos as well, hope you don't mind. Feel free to make any changes you'd like.