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?

Colyn1337
  • 2,457
  • 2
  • 25
  • 40

2 Answers2

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
Zoredache
  • 133,737
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.