Capture all errors to a file

Welcome Forums General PowerShell Q&A Capture all errors to a file

Viewing 4 reply threads
  • Author
    Posts
    • #182016
      Participant
      Topics: 8
      Replies: 13
      Points: 111
      Rank: Participant

      Is there a way to capture all the errors to a csv file with complete information?  I need it to a file because these are task scheduled scripts.

      Any ideas?  My main goal is to get the complete error data.

    • #182103
      Participant
      Topics: 1
      Replies: 20
      Points: 66
      Rank: Member

      The $Error variable captures all occurring errors.  You could just pipe it to export-csv, or select just the properties you want to have in your csv.

      example

      $Error | select targetobject,exception | Export-Csv c:\temp\errorfile.csv -NoTypeInformation

    • #187897
      Participant
      Topics: 8
      Replies: 13
      Points: 111
      Rank: Participant

      In your above example how would I get information like $error.Invocationinfo.Line piece of the data.

      I would like to select pieces of the data and one of my most needed pieces is Invocationinfo.Line.  To see the command that failed.

    • #187981
      Senior Moderator
      Topics: 8
      Replies: 1136
      Points: 3,903
      Helping Hand
      Rank: Community Hero

      You already got it, $error.Invocationinfo.Line

      You can use a Foreach-Object on $Error and then pick Invocationinfo.Line in the process scriptblock.

    • #188599
      Participant
      Topics: 8
      Replies: 13
      Points: 111
      Rank: Participant

      Do I have to collect those into a new object then to export all this into a csv?

Viewing 4 reply threads
  • You must be logged in to reply to this topic.