使用 PowerShell,如何让 Write-Debug 输出显示在控制台中? [英] With PowerShell, how can I get Write-Debug output to appear in the console?

查看:112
本文介绍了使用 PowerShell,如何让 Write-Debug 输出显示在控制台中?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在学习 PowerShell 并且正在使用 Write-Host 以检查新 PowerShell 脚本文件中的变量分配.然后我读了一篇文章,表明这是一个坏主意.

因此,在我的 .ps1 文件中,我替换了如下语句:

写主机开始"写主机结束"

...与此:

写调试开始"写调试结束"

但是当我在 Windows PowerShell ISE 中运行保存的脚本时,没有输出写入控制台.我将 -debug 附加到调用脚本的语句中,如下所示:

PS E:\trialrun>.\MyScript.ps1 -debug

但同样,输出不会写入控制台.显然我错误地使用了Write-Debug.如何将调试输出写入控制台?

解决方案

tl;dr:

  • 运行 $DebugPreference = 'Continue' 以开始查看 Write-Debug 调用的输出.

  • 完成后,使用 $DebugPreference = 'SilentlyContinue'

    将首选项变量 $DebugPreference 恢复为其默认值
  • 要仅为给定的 cmdlet 或高级函数打开调试输出,请使用 -Debug 通用参数.

    • 警告:在 Windows PowerShell 中(但不再在 PowerShell [Core] v6+ 中,这将为每个 Write-Debug 提供交互式调试提示)代码>遇到语句.

是否打印 Write-Debug 语句的输出由两种机制控制:

$DebugPreference 默认为 SilentlyContinue,这解释了为什么默认情况下您看不到 Write-Debug 语句的任何输出.>

当您使用公共参数-Debug时,您可以有效地设置$DebugPreference 仅用于调用的命令,和:

  • Windows PowerShell 中,您总是将其设置为值 Inquire>不仅打印Write-Debug消息,而且暂停在每一个这样的语句上,询问你想如何继续.

  • PowerShell [Core] v6+ 中,该值现在(更明智地)设置为 Continue.

    • 对于支持 -Debug 通用参数的自定义脚本或函数,它必须是 advanced 一个,使用 [CmdletBinding()] 属性声明其 param() 块,如 Mathias 的回答所示.

由于在 Windows PowerShell 中,这种每次提示的Write-Debug 调用行为可能会破坏性$DebugPreference = 'Continue' 可能是更好的方法.如前所述,在 PowerShell [Core] v6+ 中,这不再是一个问题.

注意:如果从高级函数或脚本内部,您想区分 $DebugPreference 已被调用者设置为 首选项变量>common parameter -Debug 已经被传递(它被翻译成一个函数/脚本本地 $DebugPreference 变量),使用$PSBoundParameters.ContainsKey('Debug');$true 表示使用了 -Debug.


参考官方文档:https://docs.microsoft.com/en-us/powershell/module/microsoft.powershell.utility/write-debug

<块引用>

默认情况下,调试消息不显示在控制台中,但是您可以使用 Debug 参数或 $DebugPreference 显示它们变量.

I'm learning PowerShell and was using Write-Host to check variable assignments in a new PowerShell script file. Then I read an article suggesting this was a bad idea.

So, in my .ps1 file I replaced statements like this:

Write-Host "Start"
Write-Host "End"

... with this:

Write-Debug "Start"
Write-Debug "End"

But when I ran the saved script in Windows PowerShell ISE no output was written to the console. I appended -debug to the statement that calls the script, like so:

PS E:\trialrun> .\MyScript.ps1 -debug

But again, the output doesn't get written to the console. Apparently I'm using Write-Debug incorrectly. How can I get the debug output to write to the console?

解决方案

tl;dr:

  • Run $DebugPreference = 'Continue' to start seeing output from Write-Debug calls.

  • When you're done, restore preference variable $DebugPreference to its default value, using $DebugPreference = 'SilentlyContinue'

  • To turn on debug output for a given cmdlet or advanced function only, use the -Debug common parameter.

    • Caveat: In Windows PowerShell (but no longer in PowerShell [Core] v6+, this will present an interactive debugging prompt for every Write-Debug statement encountered.

Whether output from Write-Debug statements is printed is controlled by two mechanisms:

$DebugPreference defaults to SilentlyContinue, which explains why you don't see any output from Write-Debug statements by default.

When you use common parameter -Debug, you effectively set $DebugPreference for the invoked command only, and:

  • in Windows PowerShell, you invariably set it to the value Inquire, which not only prints Write-Debug messages, but also pauses at every such statement to ask how you want to proceed.

  • in PowerShell [Core] v6+, the value is now (more sensibly) set to Continue.

    • For a custom script or function to support the -Debug common parameter, it must be an advanced one, declared with the [CmdletBinding()] attribute for its param() block, as Mathias' answer shows.

Since, in Windows PowerShell, this prompt-at-every-Write-Debug-call behavior can be disruptive, $DebugPreference = 'Continue' may be the better approach. As stated, in PowerShell [Core] v6+ this is no longer a concern.

Note: If, from inside an advanced function or script, you want to distinguish between $DebugPreference having been set as a preference variable by the caller vs. common parameter -Debug having been passed (which is translated to a function/script-local $DebugPreference variable), use $PSBoundParameters.ContainsKey('Debug'); $true indicates that -Debug was used.


Reference official documentation: https://docs.microsoft.com/en-us/powershell/module/microsoft.powershell.utility/write-debug

By default, debug messages are not displayed in the console, but you can display them by using the Debug parameter or the $DebugPreference variable.

这篇关于使用 PowerShell,如何让 Write-Debug 输出显示在控制台中?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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