windows 如何运行带有详细输出的 PowerShell 脚本?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/41324882/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-08 19:45:47  来源:igfitidea点击:

How to run a PowerShell script with verbose output?

windowsshellpowershellwindows-10

提问by James Ko

I'm wondering if there's a way to run a PowerShell script such that both the commands and the output of each line of the script are printed. For example, in Bash you would write bash -x myscriptor place a set -xat the top of your script. In Batch, you would omit the @echo offtraditionally left at the top of your script. Does PowerShell have an equivalent of these constructs?

我想知道是否有一种方法可以运行 PowerShell 脚本,以便打印脚本每一行的命令和输出。例如,在 Bash 中,您可以在脚本的顶部编写bash -x myscript或放置 a set -x。在 Batch 中,您将省略@echo off传统上位于脚本顶部的左侧。PowerShell 是否具有与这些结构等效的结构?

Things I've tried:Running powershell -? | sls verbose, which turned up nothing.

我尝试过的事情:Running powershell -? | sls verbose,结果什么也没有。

回答by Michael Sorens

Just goes to show, @JamesKo, if you ask the wrong question you get the wrong answer :-(. Several people put forth good-faith answers here based on (a) lack of Linux exposure and (b) your use of the term verbose. In the following I will walk you through how Linux relates to PowerShell on this topic, but feel free to jump to the answer at the end if you are in a hurry. :-)

只是表明,@JamesKo,如果你问错误的问题,你会得到错误的答案:-(。有几个人基于 (a) 缺乏 Linux 暴露和 (b) 你对这个术语的使用提出了善意的答案详细。在下面,我将向您介绍 Linux 与 PowerShell 在此主题上的关系,但如果您赶时间,请随时跳到最后的答案。:-)

Background

背景

In PowerShell, verbose has a very specific meaning which the PowerShell man pageis even rather vague about:

在 PowerShell 中,verbose 有一个非常具体的含义,PowerShell 手册页甚至对这个含义相当模糊:

Displays detailed information about the operation performed by the command. This information resembles the information in a trace or in a transaction log. This parameter works only when the command generates a verbose message.

显示有关命令执行的操作的详细信息。此信息类似于跟踪或事务日志中的信息。此参数仅在命令生成详细消息时起作用。

It even sounds like what you want... but let's compare that to the Linux documentation for set -xwhich, depending on your flavor of Linux, could be this (from man-pages project)...

它甚至听起来像你想要的......但是让我们将它与 Linux 文档进行比较set -x,根据你的 Linux 风格,可能是这个(来自手册页项目)......

The shell shall write to standard error a trace for each command after it expands the command and before it executes it.

在扩展命令之后和执行它之前,shell 应将每个命令的跟踪写入标准错误。

or this (from gnu)...

或者这个(来自gnu)...

Print a trace of simple commands, for commands, case commands, select commands, and arithmetic for commands and their arguments or associated word lists after they are expanded and before they are executed.

在扩展之后和执行之前,打印简单命令的踪迹,用于命令、case 命令、选择命令和命令及其参数的算术或关联的单词列表。

The very first line of your question clearly and concisely agrees with these. But verbose in PowerShell is different. In a nutshell, turning on verbose mode (be it with the -Verbosecommand line switch or the $VerbosePreferencevariable) simply enables output from the verbose stream to the console. (Just like Linux offers two streams, stdout and stderr, PowerShell offers multiple streams: output stream, error stream, warning stream, verbose stream, and debug stream. You work with these streams in an identical fashion to that of Linux--you can even use, e.g., commands 4>&1to merge the verbose stream to stdout, for example. (You can read more about PowerShell's multiple output streams in the Basic Writing Streams section of PowerShell One-Liners: Accessing, Handling and Writing Dataand a good quick reference is the Complete Guide to PowerShell Punctuation.)

你的问题的第一行清楚而简洁地同意这些。但是 PowerShell 中的 verbose 是不同的。简而言之,打开详细模式(无论是使用-Verbose命令行开关还是$VerbosePreference变量)只是启用从详细流到控制台的输出。(就像 Linux 提供两个流,stdout 和 stderr,PowerShell 提供多个流:输出流、错误流、警告流、详细流和调试流。您以与 Linux 相同的方式使用这些流——您可以甚至使用,例如,commands 4>&1将详细流合并到标准输出,例如。(您可以在PowerShell One-Liners的 Basic Writing Streams 部分阅读有关 PowerShell 的多个输出流的更多信息:访问、处理和写入数据一个很好的快速参考是PowerShell 标点符号完整指南。)

The Answer

答案

The Set-PSDebugcommand will give you bash-equivalent tracing. You can even adjust the tracing detail with the -Traceparameter. First, here's the control, before using Set-PSDebug:

设置-PSDebug命令会给你来砸等效跟踪。您甚至可以使用-Trace参数调整描摹细节。首先,这是使用之前的控件Set-PSDebug

PS> Get-PSDepth
0

With a value of 1you get each line of code as it executes, e.g.:

值为1 时,您会在执行时获得每一行代码,例如:

PS> Set-PSDebug -Trace 1
PS> Get-PSDepth
DEBUG:    1+  >>>> Get-PSDepth
DEBUG:  141+  >>>> {
DEBUG:  142+   >>>> $nest = -1
DEBUG:  143+   >>>> $thisId = $pid
DEBUG:  144+  while ( >>>> (ps -id $thisId).Name -eq 'powershell') {
DEBUG:  145+    >>>> $thisId = (gwmi win32_process -Filter "processid='$thisId'").ParentProcessId
DEBUG:  146+    >>>> $nest++
DEBUG:  144+  while ( >>>> (ps -id $thisId).Name -eq 'powershell') {
DEBUG:  148+   >>>> $nest
0
DEBUG:  149+  >>>> }

With a value of 2you also get variable assignments and code paths:

值为2 时,您还可以获得变量分配和代码路径:

PS> Set-PSDebug -Trace 2
PS> Get-PSDepth
DEBUG:    1+  >>>> Get-PSDepth
DEBUG:     ! CALL function '<ScriptBlock>'
DEBUG:  141+  >>>> {
DEBUG:     ! CALL function 'Get-PSDepth'  (defined in file 'C:\Users\msorens\Documents\WindowsPowerShell\profile.ps1')
DEBUG:  142+   >>>> $nest = -1
DEBUG:     ! SET $nest = '-1'.
DEBUG:  143+   >>>> $thisId = $pid
DEBUG:     ! SET $thisId = '9872'.
DEBUG:  144+  while ( >>>> (ps -id $thisId).Name -eq 'powershell') {
DEBUG:  145+    >>>> $thisId = (gwmi win32_process -Filter "processid='$thisId'").ParentProcessId
DEBUG:     ! SET $thisId = '10548'.
DEBUG:  146+    >>>> $nest++
DEBUG:     ! SET $nest = '0'.
DEBUG:  144+  while ( >>>> (ps -id $thisId).Name -eq 'powershell') {
DEBUG:  148+   >>>> $nest
0
DEBUG:  149+  >>>> }

Those are traces of a simple cmdlet I wrote called Get-PSDepth. It prints the commands, assignments, etc. with the DEBUGprefix, intermixed with the actual output, which in this case is the single line containing just 0.

这些是我写的一个简单的 cmdlet 的痕迹,名为Get-PSDepth. 它打印带有DEBUG前缀的命令、赋值等,与实际输出混合,在这种情况下是仅包含0.

回答by Ranadip Dutta

You can always use the below in your script.

您始终可以在脚本中使用以下内容。

$VerbosePreference="Continue"

$VerbosePreference="继续"

Note:You have to open the shell in elevated mode.

注意:您必须以提升模式打开外壳。

Below screenshot is for reference.

以下截图供参考。

$VerbosePreference

$VerbosePreference

Hope it helps.

希望能帮助到你。

回答by kjohnsonthecoder

This actually is very easy every PowerShell CMDLET has a built in Verbose tag. All you have to do for example:

这实际上非常简单,因为每个 PowerShell CMDLET 都有一个内置的 Verbose 标签。所有你需要做的,例如:

Test-Connection -ComputerName www.google.com -Verbose

测试连接 -ComputerName www.google.com -Verbose

That is it. I hope this helps

这就对了。我希望这有帮助

回答by Steve Rathbone

If you're using write-verbose in your scripts this will happen automatically,

如果您在脚本中使用 write-verbose,这将自动发生,

However, if you need to manually write verbose output from your functions, you'll need to manually check that each function is called with the verbose flag. This can be done by checking$PSCmdlet.MyInvocation.BoundParameters["Verbose"] from inside your function.

但是,如果您需要从函数中手动编写详细输出,则需要手动检查是否使用详细标志调用了每个函数。这可以通过从函数内部检查 $PSCmdlet.MyInvocation.BoundParameters["Verbose"] 来完成。