将 PowerShell psdebug 跟踪的输出记录到文件中

问题描述 投票:0回答:3
  1. 我有一个脚本“B”我想从中捕获调试
    Set-PSDebug -Trace n
    的输出到文件。File 是 此处为关键字。)
  2. 我需要启动执行并调试捕获来自另一个脚本(称为A)的脚本“B”。

示例:

脚本B

Set-PSDebug -Trace 1

Function FuncA {
    Write-Host "ABC"
    FuncB
}

Function FuncB {
    Write-Host "123"
}

FuncA
FuncB

正确的调试输出是:

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+ >>>> }

.

但是当我现在尝试通过启动进程从脚本

A运行它以将输出捕获到文件中时:

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

输出奇怪的是这样的:

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

尽管脚本正确完成,但调试消息在第一个函数之后停止。

有什么想法以及如何规避或避免这种情况吗?

或者,我正在寻找另一种解决方案来实现顶部所述的两个目标。

顺便说一句:我也尝试过使用trace-command

,它有一个
filepath
参数,但我不知道如何
跟踪整个脚本,而且我不知道如何获取Set-PSDebug提供的信息:正在执行的行和正在执行的命令没有其余的 我想自动处理调试输出,而 Set-PSDebug 的输出正是我所需要的。

powershell debugging trace
3个回答
3
投票
所以,这是我在 PowerShell v4.0 中使用 ISE 和常规主机进行一些测试时了解到的内容。

使用
    Write-
  1. cmdlet 中的

    any 似乎会从遇到第一个 cmdlet 时中断 PSDebug 输出。注释掉它们,例如Write-Host "ABC"

     线将允许看到更多跟踪,直到调用 FuncB 中的 
    Write-Host

    使用
  2. return
  3. 解决了问题,尽管这确实意味着 FuncB 不是从 FuncA 内部调用的,这仅仅是由于脚本的逻辑流程。

    将事物剥离回字符串本身似乎会产生预期的行为。我的意思是简单地删除
  4. Write-Host
  5. cmdlet 并保留

    "ABC"

    "123"
     部分。我不喜欢从函数中吐出这样的文本,但至少它给出了我们在这个示例中所期望的内容。见下文。

    在脚本 A 末尾留一个空行会改变输出的行为,即,如果第 13 行末尾有回车符,则输出格式如下所示。如果没有,那么您最终会在同一行上得到两行调试:
  6. DEBUG:13+ >>>> FuncBDEBUG:8+ 函数 FuncB >>>> {

    在 PowerShell 5.0 中运行主要解决了(原始帖子)问题,尽管我仍然遇到在 Write-Host 输出之后立即运行 DEBUG 行的问题(即没有换行符)。再次切换到下面解决方案中的代码修复了输出。
  7. ABCDEBUG:5+ >>>> FuncB

    解决方案

脚本 A

C:\Scripts\PowerShell\Test-Debug.ps1 Set-PSDebug -Trace 1 Function FuncA { "ABC" FuncB } Function FuncB { "123" } FuncA FuncB

脚本 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

这给出了我认为您在
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+ >>>> }

解决方案:使用PowerShell 5!

1
投票
我快速浏览了 MS Connect,看看是否有任何迹象表明这是一个已知/报告的错误,但没有发现任何内容。然而,PSv5 中的行为变化这一事实表明已经存在修复,可能是通过引入信息输出流。

如果您无法修改当前正在调用

Write-Host

等的脚本,那么,从我们的集体测试来看,我不确定除了使用 PowerShell 5 之外还有其他修复方法。如果您有大型/受控环境。


模块

0
投票
可能会获得一些新的项目资源/移交,这将使其与最新版本的 PowerShell 保持同步..

© www.soinside.com 2019 - 2024. All rights reserved.