Rob*_*Rob 8 debugging powershell trace
Set-PSDebug -Trace n
到文件的调试输出.(文件是这里的关键字.)例:
脚本B:
Set-PSDebug -Trace 1
Function FuncA {
Write-Host "ABC"
FuncB
}
Function FuncB {
Write-Host "123"
}
FuncA
FuncB
Run Code Online (Sandbox Code Playgroud)
这个的正确调试输出是:
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+ >>>> }
Run Code Online (Sandbox Code Playgroud)
.
但是当我尝试从脚本A通过start-process 运行它以将输出捕获到文件时:
$SParguments = "-NoProfile -file `"$stdTracefile`""
Start-Process 'C:\Windows\System32\WindowsPowerShell\v1.0\powershell.exe' -ArgumentList $SParguments -RedirectStandardOutput $stdTracelog
Run Code Online (Sandbox Code Playgroud)
输出奇怪就是这样:
DEBUG: 15+ >>>> FuncA
DEBUG: 6+ Function FuncA >>>> {
DEBUG: 7+ >>>> Write-Host "ABC"
ABC
123
123
Run Code Online (Sandbox Code Playgroud)
尽管脚本正确完成,但调试消息在第一个函数后停止.
任何想法为什么以及如何规避或避免这种情况?
或者,我正在寻找另一种解决方案来达到顶部所述的两个目标.
BTW:我也试过使用trace-command
,它有一个filepath
参数,但我不知道如何跟踪整个脚本,我不知道如何获取Set-PSDebug提供的信息:正在执行的行和正在执行的命令,没有剩下所有.
我想自动处理调试输出,Set-PSDebug的输出正是我需要的.
以下是我使用 ISE 和常规主机在 PowerShell v4.0 中进行的一些测试中了解到的内容。
使用任何cmdletWrite-
似乎都会从遇到第一个 cmdlet 时中断 PSDebug 输出。将它们注释掉,例如,在调用 in FuncBWrite-Host "ABC"
之前,该行将允许看到更多跟踪。Write-Host
使用return
修复了问题,尽管这确实意味着 FuncB 不是从 FuncA 内部调用的,这仅仅是由于脚本的逻辑流程。
将事物剥离回字符串本身似乎会产生预期的行为。我的意思是简单地删除Write-Host
cmdlet 并保留"ABC"
和"123"
部分。我不喜欢从函数中吐出这样的文本,但至少它给出了我们在这个示例中所期望的内容。见下文。
在脚本 A 末尾留下一个空行会改变输出的行为,即,如果第 13 行末尾有回车符,则输出格式如下所示。如果没有,那么您最终会在同一行上得到两行调试:
调试:13+ >>>> FuncBDEBUG:8+ 函数 FuncB >>>> {
在 PowerShell 5.0 中运行主要解决了(原始帖子)问题,尽管我仍然遇到在 Write-Host 输出之后立即运行 DEBUG 行的问题(即没有换行符)。同样,切换到下面解决方案中的代码修复了输出。
ABCDEBUG: 5+ >>>> FuncB
脚本 A:C:\Scripts\PowerShell\Test-Debug.ps1
Set-PSDebug -Trace 1
Function FuncA {
"ABC"
FuncB
}
Function FuncB {
"123"
}
FuncA
FuncB
Run Code Online (Sandbox Code Playgroud)
脚本 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
Run Code Online (Sandbox Code Playgroud)
这给出了我认为您在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+ >>>> }
Run Code Online (Sandbox Code Playgroud)
归档时间: |
|
查看次数: |
2381 次 |
最近记录: |