59

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

我正在使用 PowerShell 脚本,它们运行良好。但是,我想知道是否有任何方法可以显示所有已运行的命令,就像您自己手动输入它们一样。这类似于批处理文件中的“回显”。我查看了 PowerShell 命令行参数和 cmdlet,但没有发现任何明显的东西。

4

4 回答 4

60
Set-PSDebug -Trace 1
  • 0:关闭脚本跟踪。
  • 1:在运行时跟踪脚本行。
  • 2:跟踪脚本行、变量赋值、函数调用和脚本。

欲了解更多信息:https ://docs.microsoft.com/en-us/powershell/module/microsoft.powershell.core/set-psdebug?view=powershell-6

于 2018-04-04T09:30:04.833 回答
13

Start-Transcript 不会捕获任何 exe 输出。这对我来说是一个表演终结者。我不想这么说,但我发现这样做的最好方法是:

cmd /c powershell.exe -file c:\users\hillr\foo.ps1 > foo.log

这捕获了 AFAICT 的所有内容。

于 2010-01-14T15:16:09.283 回答
4
C:\workspaces\silverlight> start-transcript -?

NAME
    Start-Transcript
    
SYNOPSIS
    Creates a record of all or part of a Windows PowerShell session in a text file.
    
    
SYNTAX
    Start-Transcript [[-Path] <string>] [-Append] [-Force] [-NoClobber] [-Confirm] [-WhatIf] [<CommonParameters>]
    
    
DESCRIPTION
    The Start-Transcript cmdlet creates a record of all or part of a Windows PowerShell session in a text file. The transcript includes all command that the user
     types and all output that appears on the console.
    

RELATED LINKS
    Online version: http://go.microsoft.com/fwlink/?LinkID=113408
    Stop-Transcript 

REMARKS
    To see the examples, type: "get-help Start-Transcript -examples".
    For more information, type: "get-help Start-Transcript -detailed".
    For technical information, type: "get-help Start-Transcript -full".

注意#1:它只记录写入主控制台输出流的内容,而不是警告/错误/调试。

注意#2:如果您需要录制本机控制台应用程序,您需要一个小解决方法

于 2010-01-14T15:04:05.860 回答
3

我将 -verbose 添加到所需的命令中。例如

Copy-Item c:\xxx d:\xxx -verbose
于 2013-10-01T09:54:19.300 回答