将Powershell psdebug跟踪的输出记录记录到文件 [英] Log output of Powershell psdebug trace to a file
问题描述
- 我有一个脚本B,从中我要捕获
的输出Set-PSDebug -Trace n
到文件(文件是
关键字在这里) - 我需要从另一个脚本(称为 A )启动执行和调试脚本 B 的捕获。
示例:
脚本 B
Set-PSDebug -Trace 1
功能FuncA {
写主机ABC
FuncB
}
功能FuncB {
写主机123
}
FuncA
FuncB
正确的调试输出是:
DEBUG:15+>>>> FuncA
DEBUG:6+功能FuncA>>>>> {
DEBUG:7+>>>>>写主机ABC
ABC
DEBUG:8+>>>> FuncB
DEBUG:11+功能FuncB>>>>> {
DEBUG:12+>>>>>写主机123
123
DEBUG:13+>>>> }
DEBUG:9+>>>>> }
DEBUG:16+>>>>> FuncB
DEBUG:11+功能FuncB>>>>> {
DEBUG:12+>>>>>写主机123
123
DEBUG:13+>>>>
。
但是当我尝试现在通过开始进程从脚本 A 运行它,以将输出捕获到文件:
$ SParguments =-NoProfile -file`$ stdTracefile`
开始进程'C:\Windows\System32\WindowsPowerShell\v1.0\powershell.exe'-ArgumentList $ SParguments - RedirectStandardOutput $ stdTracelog
输出只是这样:
DEBUG:15+>>>> FuncA
DEBUG:6+功能FuncA>>>>> {
DEBUG:7+>>>>>写主持人ABC
ABC
123
123
任何想法为什么以及如何规避或避免这种情况?
或者,我正在寻找另一种解决方案来达到顶部所述的两个目标。
Btw:我也尝试使用
trace-command
,它有一个 filepath
参数,但是我不知道如何跟踪整个脚本,我不知道如何获取psdebug提供的信息:正在执行的行和命令被执行, 我想自动处理调试输出,PSdebug的输出正是我所需要的。
解决方案:使用PowerShell 5 !
我已经快速查看了MS Connect,看看是否有任何提示,这是一个已知/报告的错误,但没有找到任何东西。然而,PSv5中的行为发生变化的事实表明,已经有一个修复,可能是引入了信息输出流。
如果您无法修改目前正在调用 Write-Host
等的脚本,然后从我们的集体测试中,我不确定除了使用PowerShell 5之外还有一个修复。显然不是一个如果你有一个大/受控的环境,那么这个微不足道的建议。
- 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) - 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.
Solution: Use PowerShell 5!
I have had a quick look on MS Connect to see if there is any hint of this being a known/reported bug but haven't found anything. HOWEVER, the fact that the behaviour changes in PSv5 suggests that there has been a fix, possibly with the introduction of the Information output stream.
If you can't modify the scripts which are currently calling Write-Host
, etc. then, from our collective testing, I'm not sure there is a fix other than to use PowerShell 5. Obviously not a trivial proposal if you have a large/controlled environment.
这篇关于将Powershell psdebug跟踪的输出记录记录到文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!