Jump to content

Recommended Posts

Posted

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?

Posted

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

Posted

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

  • Thanks 1

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 account

Sign in

Already have an account? Sign in here.

Sign In Now



×
×
  • Create New...