penfold Posted August 3, 2022 Posted August 3, 2022 Colleague is trying to use throw to create a custom error in his script. But he doesn't want the additional details found displayed. All he wants is the error message. Example output I see is this Error occurred.At line:3 char:1+ throw "Error occurred."+ ~~~~~~~~~~~~~~~~~~~~~~ + CategoryInfo : OperationStopped: (Error occurred.:String) [], RuntimeException + FullyQualifiedErrorId : Error occurred. What he actually wants is just Error occurred Is there anyway to just throw the error message and not the whole details?
mbedford Posted August 3, 2022 Posted August 3, 2022 Have a look at the $error variable. Its an array so to get the most recent exception, try $error[0].exception sometimes the inner exception message is more useful. $error[0].exception.innerexception
Steve21 Posted August 3, 2022 Posted August 3, 2022 If it's part of a bigger set of code I'd just wrap a try/catch around it, and output either the "Exception.Message" or "Exception.Innerexception" try { "BrokenErrorCode" } catch { Write-Output $_.Exception.Message } etc Steve 1
penfold Posted August 4, 2022 Author Posted August 4, 2022 @Steve21 - this is what I suggested. I think he was trying to use throw to recognise the script failed.
Recommended Posts
Create an account or sign in to comment
You need to be a member in order to leave a comment
Create an account
Sign up for a new account in our community. It's easy!
Register a new accountSign in
Already have an account? Sign in here.
Sign In Now