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

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

问题描述

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

我尝试过的事情: 运行 powershell -?|sls 详细,结果什么都没有.

解决方案

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

背景

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

<块引用>

显示有关执行操作的详细信息命令.此信息类似于跟踪或交易记录.此参数仅在命令生成时有效一条冗长的消息.

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

<块引用>

shell 应将每个命令的跟踪写入标准错误它在执行命令之前展开命令.

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

打印简单命令的痕迹,对于命令,大小写命令,选择命令,以及命令及其参数的算术或展开之后和展开之前的关联单词列表执行.

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

答案

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

PS>获取-PSDepth0

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

PS>设置-PSDebug -Trace 1PS>获取-PSDepth调试: 1+ >>>获取-PSDepth调试: 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+ >>>$nest0调试: 149+ >>>}

如果值为 2,您还可以获得变量赋值和代码路径:

PS>设置-PSDebug -Trace 2PS>获取-PSDepth调试: 1+ >>>获取-PSDepth调试:!CALL 函数 ''调试: 141+ >>>{调试:!CALL 函数Get-PSDepth"(在文件C:\Users\msorens\Documents\WindowsPowerShell\profile.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+>>>>$nest0调试: 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:\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 DEBUG prefix, intermixed with the actual output, which in this case is the single line containing just 0.

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

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