PowerShell"回声"

Nel*_*mel 27 powershell command echo

这是https://serverfault.com/questions/102098/powershell-script-showing-commands-run的副本.我认为在这里提出这个问题会更合适.

我正在玩PowerShell脚本,他们工作得很好.但是,我想知道是否还有任何方法可以显示所有运行的命令,就像你自己手动输入它们一样.这与批处理文件中的"echo on"类似.我查看了PowerShell命令行参数cmdlet,但我没有找到任何明显的参数.

wis*_*cky 14

Set-PSDebug -Trace 1
Run Code Online (Sandbox Code Playgroud)
  • 0:关闭脚本跟踪。
  • 1:在脚本行运行时对其进行跟踪。
  • 2:跟踪脚本行,变量分配,函数调用和脚本。

有关更多信息:https : //docs.microsoft.com/zh-cn/powershell/module/microsoft.powershell.core/set-psdebug?view=powershell-6

  • 这不显示*“运行的所有命令”*。实际上根本没有显示任何命令。 (3认同)
  • 很有用,但不幸的是,它不会像 bash `set -x` 那样扩展变量。 (2认同)

Kei*_*ill 8

Start-Transcript不会捕获任何exe输出.这对我来说是个噱头.我不想这么说,但我发现这样做的最好方法是:

cmd /c powershell.exe -file c:\users\hillr\foo.ps1 > foo.log
Run Code Online (Sandbox Code Playgroud)

这捕获了AFAICT的一切.

  • 前几天才看到这个.http://blogs.msdn.com/powershell/archive/2010/01/04/workaround-for-start-transcript-on-native-processes.aspx不太理想,但是如果你将exe输出管道输出到Out-Default,那么将被捕获 (6认同)