Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

PowerShell: Is there a way to add time/date stamps to the debug output?

I've been using set-psdebug to send debug output to the console. I'd like to improve that by adding a time/date stamp since the output is captured and this would help troubleshoot issues after the fact. Anyone doing this or know how to do this?

This scripts all use:

set-psdebug -trace 1

this causes the output to look like this:

Debug:   14+  >>>> $ToFolder = "${UnixUserid}@${UnixServer}:${UnixFile}"

the goal is to add the time/date stamp to the left hand side of the Debug

like image 644
Sean Avatar asked Oct 30 '22 16:10

Sean


1 Answers

I think the built-in cmdlet Set-TraceSource is the most frictionless way to accomplish what you're wanting. You'll want to play around with the values available for -Option. I did try instantiating my own TraceListeners with higher TraceLevels but couldn't figure out how to get them bound to the running powershell console.

Set-PSDebug -Trace 1
Set-TraceSource -Name "ParameterBinding" -Option ExecutionFlow -PSHost -ListenerOption "DateTime"
foreach ($i in 1..3) { $i }

Reference: https://learn.microsoft.com/en-us/powershell/module/microsoft.powershell.utility/set-tracesource?view=powershell-6

like image 66
RiverHeart Avatar answered Dec 16 '22 12:12

RiverHeart