The $Error Variable in PowerShell accommodates all error objects of the present session. On this weblog submit I’ll perform some duties with $Error and present you ways $Error will help you to troubleshoot and debug your PowerShell code. Let’s leap in.
Assume you are attempting to checklist a folder that doesn’t exist.
We’re confronted with an error message. This error message shall be saved within the $Error Variable.
However, wait a minute, what sort of error is it?
Now let’s clear this error.
What about different error classes? Try this command.
Enter-PSSession -ComputerName NotThere
The explanation on this case is an exception of RemoteTransporting.
We will look at the assertion that causes the error or exception in additional element.
$Error.InvocationInfo.line
It’s value mentioning that if there are a number of errors in $Error, all errors could be known as with sq. brackets.
Lastly, it may be a good suggestion to retailer the content material of the $Error variable in a file.
$Error | Out-File $homeerrors.txt
Open it with Begin-Course of.
Begin-Course of $homeerrors.txt
On the finish I need to present that you may create an error your self with throw.
throw 'Operation aborted.'
That’s what I wished to indicate. See you then!
Printed by