Output PowerShell psdebug trace log to file

  • 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 key word here.)
  • I need to initiate the execution and debugging 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 debug output is correct :

 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 conclusion has a strange meaning:

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

Debugging messages stop after the first function, although the script ends correctly.

Any ideas why and how to get around or avoiding this?

As an alternative, I am looking for another solution to achieve the two goals listed at the top.

BTW: I also tried using trace-command , which has a filepath parameter, but I don’t know how to track the whole script , and I don’t know how to get the information. Set-PSDebug provides: executable lines and executable commands , without the rest . I want to automatically handle the debug output, and the Set-PSDebug output is exactly what I need.

+7
debugging powershell trace
source share
2 answers

So, here is what I learned when testing in PowerShell v4.0 using both ISE and a regular host.

  • Using any of the Write- cmdlets seemed to interrupt PSDebug output from the point at which the first of these cmdlets occurs. Commenting them out, for example. the Write-Host "ABC" will let you see more traces until Write-Host is called in FuncB.

  • Using return , the problem was fixed, although it meant that FuncB was not called from FuncA, simply because of the logical flow of the script.

  • Disabling things down to the line itself seemed to lead to the expected behavior. Under this, I just want to remove the Write-Host cmdlet and leave the "ABC" and "123" parts. I'm not a fan of splashing text like this out of functions, but at least it gave what we expect in this example. See below.

  • Leaving an empty line at the end of Script A, the behavior of the output has changed, that is, if there is a carriage return at the end of line 13, then the output will be formatted as shown below. If not, you will get two lines of debugging in one line:

    DEBUG: 13+ β†’ β†’ FuncBDEBUG: 8+ FuncB Function β†’ β†’ {

  • Running in PowerShell 5.0 basically fixes the problem (the original message), although I still had problems with the DEBUG lines running right after Write-Host came out (i.e. without a new line). Again, switching to the code in the solution below fixed the output.

    ABCDEBUG: 5+ β†’ β†’ FuncB

Decision

Script A : C: \ Scripts \ PowerShell \ Test-Debug.ps1

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

Script B :: C: \ Scripts \ PowerShell \ Call-TestDebug.ps1

 $stdTraceFile = "C:\Scripts\PowerShell\Test-Debug.ps1" $stdTraceLog = Join-Path $env:TEMP test.log $PSExecutable = 'C:\Windows\System32\WindowsPowerShell\v1.0\powershell.exe' $SParguments = "-Version 4 -NoProfile -File $stdTracefile" Start-Process -FilePath $PSExecutable -ArgumentList $SParguments ` -RedirectStandardOutput $stdTracelog 

This gave the result, which I think you expected in test.log :

 DEBUG: 12+ >>>> FuncA DEBUG: 3+ Function FuncA >>>> { DEBUG: 4+ >>>> "ABC" ABC DEBUG: 5+ >>>> FuncB DEBUG: 8+ Function FuncB >>>> { DEBUG: 9+ >>>> "123" 123 DEBUG: 10+ >>>> } DEBUG: 6+ >>>> } DEBUG: 13+ >>>> FuncB DEBUG: 8+ Function FuncB >>>> { DEBUG: 9+ >>>> "123" 123 DEBUG: 10+ >>>> } 
+1
source share

Solution: Use PowerShell 5!

I had a quick look at MS Connect to see if there was any hint that this was a known bug, but couldn’t find anything. HOWEVER, the fact that a change in behavior in PSv5 suggests that there was a fix, possibly with the introduction of a stream of information.

If you cannot change the scripts that are currently invoking Write-Host , etc., then from our collective testing I'm not sure if there is a fix other than using PowerShell 5. Obviously there is no trivial suggestion if you have large / controlled environment.

+1
source share

All Articles