Friday, April 8, 2016

Log output of Powershell psdebug trace to a file

Leave a Comment
  1. I have a script "B" from which I want to capture the debug output of Set-PSDebug -Trace n to a file. (File is the keyword here)
  2. I need to initiate the execution and debug capture of script "B" from another script, called A.

Example:

Script B :

Set-PSDebug -Trace 1   Function FuncA {     Write-Host "ABC"     FuncB }  Function FuncB {     Write-Host "123" }  FuncA FuncB 

The correct debug output of this is:

DEBUG:   15+  >>>> FuncA DEBUG:    6+ Function FuncA  >>>> { DEBUG:    7+      >>>> Write-Host "ABC" ABC DEBUG:    8+      >>>> FuncB DEBUG:   11+ Function FuncB  >>>> { DEBUG:   12+      >>>> Write-Host "123" 123 DEBUG:   13+  >>>> } DEBUG:    9+  >>>> } DEBUG:   16+  >>>> FuncB DEBUG:   11+ Function FuncB  >>>> { DEBUG:   12+      >>>> Write-Host "123" 123 DEBUG:   13+  >>>> } 

.

But when I try to run it now from Script A via start-process to capture the output to a file:

$SParguments = "-NoProfile -file `"$stdTracefile`"" Start-Process 'C:\Windows\System32\WindowsPowerShell\v1.0\powershell.exe' -ArgumentList $SParguments -RedirectStandardOutput $stdTracelog 

the output is oddly just this:

DEBUG:   15+  >>>> FuncA DEBUG:    6+ Function FuncA  >>>> { DEBUG:    7+      >>>> Write-Host "ABC" ABC 123 123 

The debugging messages stop after the first function, although the script finishes correctly.

Any ideas why and how to circumvent or avoid this?

Alternatively, I am looking for another solution to reach the two goals stated at the top.

Btw: I also tried using trace-command, which has a filepath parameter, but I dont know how to trace a whole script and I do not know how to get the information set-psdebug provides: the lines being executed and the commands being executed, without all the rest. I want to automatically process the debug output and the output of PSdebug is exactly what i need.

Btw2: i checked all the other similar threads here, they do not answer the question. So, this is not a duplicate, please do not flag it as such.

0 Answers

If You Enjoyed This, Take 5 Seconds To Share It

0 comments:

Post a Comment