如何运行具有详细输出的 PowerShell 脚本? [英] How to run a PowerShell script with verbose output?

查看:21
本文介绍了如何运行具有详细输出的 PowerShell 脚本?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

我尝试过的事情: 运行 powershell -?|sls verbose,结果什么也没出现.

解决方案

只是表明,@JamesKo,如果你问错问题,你会得到错误的答案:-(.几个人在这里提出了善意的答案基于关于 (a) 缺乏对 Linux 的了解和 (b) 你对术语 verbose 的使用.下面我将引导你了解 Linux 与 PowerShell 在这个主题上的关系,但请随意跳到如果你赶时间,请在最后回答.:-)

背景

在 PowerShell 中,详细具有非常具体的含义,PowerShell 手册页 甚至相当模糊:

<块引用>

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

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

<块引用>

shell 将在之后的每个命令的跟踪中写入标准错误它会在执行命令之前扩展命令.

或者这个(来自 gnu)...p><块引用>

打印简单命令的痕迹,对于命令,case命令,select命令,以及命令及其参数的算术或相关单词列表在它们展开之后和之前执行.

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

答案

Set-PSDebug 命令将为您提供 bash 等效项追踪.您甚至可以使用 -Trace 参数调整跟踪细节.首先,这里是控件,在使用Set-PSDebug之前:

PS>获取PSD深度0

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

PS>设置-PSDebug -Trace 1PS>获取PSD深度调试:1+>>>>获取PSD深度调试:141+>>>>{调试:142+>>>>$nest = -1调试:143+>>>>$thisId = $pid调试:144+ while (>>>> (ps -id $thisId).Name -eq 'powershell') {调试:145+>>>>$thisId = (gwmi win32_process -Filter "processid='$thisId'").ParentProcessId调试:146+>>>>$巢++调试:144+ while (>>>> (ps -id $thisId).Name -eq 'powershell') {调试:148+>>>>$巢0调试:149+>>>>}

使用 2 的值,您还可以获得变量赋值和代码路径:

PS>设置-PSDebug-Trace 2PS>获取PSD深度调试:1+>>>>获取PSD深度调试:!调用函数 '<ScriptBlock>'调试:141+>>>>{调试:!CALL 函数Get-PSDepth"(在文件C:UsersmsorensDocumentsWindowsPowerShellprofile.ps1"中定义)调试:142+>>>>$nest = -1调试:!SET $nest = '-1'.调试:143+>>>>$thisId = $pid调试:!设置 $thisId = '9872'.调试:144+ while (>>>> (ps -id $thisId).Name -eq 'powershell') {调试:145+>>>>$thisId = (gwmi win32_process -Filter "processid='$thisId'").ParentProcessId调试:!设置 $thisId = '10548'.调试:146+>>>>$巢++调试:!SET $nest = '0'.调试:144+ while (>>>> (ps -id $thisId).Name -eq 'powershell') {调试:148+>>>>$巢0调试:149+>>>>}

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

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 myscript or place a set -x at the top of your script. In Batch, you would omit the @echo off traditionally left at the top of your script. Does PowerShell have an equivalent of these constructs?

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

解决方案

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. :-)

Background

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

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 -x which, depending on your flavor of Linux, could be this (from man-pages project)...

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

or this (from 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.

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 -Verbose command line switch or the $VerbosePreference variable) 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>&1 to 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 Data and a good quick reference is the Complete Guide to PowerShell Punctuation.)

The Answer

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

PS> Get-PSDepth
0

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

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 2 you also get variable assignments and code paths:

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:UsersmsorensDocumentsWindowsPowerShellprofile.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 DEBUG prefix, intermixed with the actual output, which in this case is the single line containing just 0.

这篇关于如何运行具有详细输出的 PowerShell 脚本?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆