1

I currently running a script and where I can see the exceptions in the control panel. For example

Exception calling "ExecuteNonQuery" with "0" argument(s): "Cannot drop the table 'testtabel', because it does not exist or you do not have permission."
At line:383 char:36
+         $insertData.ExecuteNonQuery <<<< ()
    + CategoryInfo          : NotSpecified: (:) [], MethodInvocationException
    + FullyQualifiedErrorId : DotNetMethodException

This is no error in the script so the script moves on but I want to save these exception in a text file. I tried different methods like

$_ | Out-File C:\errors.txt -Append

or other log functions I had found on the Internet.

Is there just an easy way to extract these exception to a text file?

2
  • $error is the automatic variable for errors. So if you want to export the last error it would be $error[0] | Out-File C:\errors.txt -Append Commented Aug 28, 2017 at 13:38
  • Are you getting the $_ from a try catch...from a pipe? Commented Aug 28, 2017 at 13:57

1 Answer 1

3

There are two methods, the $Error automatic variable that stores all error objects in it, or Try{}Catch{} using the $PSItem or $_ automatic variables.

Try {
  .. commands ..
} Catch {
  # Error TYPE
  "[$($_.Exception.GetType().FullName)]" | Out-File C:\Temp\errorlog.txt
  # Error MESSAGE
  $_.Exception.Message | Out-File C:\Temp\errorlog.txt
}
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.