Reputation: 2015
It has been a while since the last time I wrote a powershell script. The first thing I did is implement a logging function into my script but now I noticed that no matter if I log $_ or $_.Exception in a catch-block, there is always some information missing when the log is written to a file. How can I log the exception with all details like line number, stack trace and so on to a file without writing like 5 lines of code just for formatting the exception manually? Is there really no standard way of doing this in powershell?
Upvotes: 0
Views: 700
Reputation: 4634
Technically it depends on exception.
Every exception is an instance of Exception class or one of derivatives.
This means that all Exceptions guaranteed to have basic properties and methods listed in MSDN Exception doc, but additionally Exceptions can have it's own properties or methods. For example, ArgumentException
is derivative from Exception
, but is has an optional ParamName
property.
You can catch predicted exceptions using different catchers or you can get all properties using, for example, Get-Member, and log them. But catch
block should not be complex.
Additionally, try using Start-Transcript instead of custom logging. Maybe this will be enough for your needs. Don't forget to set compression flag on folder where transcriptions will be written to.
Upvotes: 1