6

Powershell error objects contain a lot of data but I can't seem to track down the time of occurrence for a given error. Using $Error[0] | fl -f retrieves a lot of information but none that appears to be a date time of when the error was generated. It occurs to me this data would/should be part of the error object and I'm just not able to find the exact property.

I could get a close approximation by using try/catch blocks and capturing time then, but I need as precise of a time stamp as is possible. Anyone know where/if the time of occurrence is stored in the $Error object?

  • 2
    I just spent a half hour going through every object within the error array, and was unable to find it :( Starting to think it's not in there. – Reaces Feb 27 '17 at 20:40
  • 1
    I've submitted the suggestion to microsoft to include a datetime property. Please vote windowsserver.uservoice.com/forums/301869-powershell/… – Colyn1337 Feb 28 '17 at 16:26
  • 1
    Voted for it, I agree it's probably a good quick win addition to the object. – Reaces Feb 28 '17 at 17:27
4

As far as I know there is no date-time that is part of the error object. If you want the datetime, just add a Get-Date into your catch block or whatever and add it to your output, logs whatever output method you are using for the rest of the error.

The 'o' format has lots of precision and useful format for log files.

PS C:\users> Get-Date  -Format o
2017-02-27T11:57:31.3946789-08:00
  • 1
    +1. If the execution is remote one can store all log objects to return in an arraylist. Thus one can store multiple log entries including $arrayobjects[x].error and $arrayobjects[x].time and whatever other properties in one comprehensive object to pass back (return $arrayobjects). At the receiving end this is processed into n entries of whatever log format desirable. For this reason I like passing raw datetime objects as they are so easy to post process. – ErikE Feb 27 '17 at 20:51
2

PowerShell error objects don't contain date/time information.

Source: Windows PowerShell Error Records

The InvocationInfo class doesn't contain that information either.

If you want that metadata, you'll need to put it somewhere when you catch a terminating error. This won't help too much with non-terminating errors, though.

It might help if you more fully explain your use case.

  • Couldn't -ErrorAction be used to log a timestamp for non-terminating errors? – SturdyErde Mar 19 '17 at 10:43

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.