We have a bunch of Rest APIs that are consuming the same powershell module, allowing them to copy data from Prod to Test in a specific way.
Our modules use try / catch to stop executing the code if something breaks, and then does a throw printing the error message to a logfile on the PSU server - this parts works perfectly.
Our clients are requesting that this data is also shown to them, so i looked into the New-PSUAPIResponse cmdlet, hoping that it would solve the issue.
Then the return message is “Invoke-RestMethod : The remote server returned an error: (500) Internal Server Error” - BUT the code continues to run (creating the PSU.txt file).
How do i return the $_.Exception.Message in the New-PSUAPIResponse & stop the code from executing after that?
I deleted the psu.txt and ran it again, and on the first try it returns an error 500, but also creates the file.
On the second go it returns an error 400, because the PSU.txt file already exists.