使用 PowerShell,如何让 Write-Debug 输出显示在控制台中? [英] With PowerShell, how can I get Write-Debug output to appear in the console?
问题描述
我正在学习 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 提供交互式调试提示)代码>遇到语句.
- 警告:在 Windows PowerShell 中(但不再在 PowerShell [Core] v6+ 中,这将为每个
是否打印 Write-Debug
语句的输出由两种机制控制:
范围范围:根据
$DebugPreference
首选项变量的值 - 参见获取帮助 about_Preference_Variables
.临时、命令范围,当调用 cmdlet 或高级脚本/函数(覆盖
$DebugPreference
value) 与-Debug
通用参数 - 请参阅获取帮助 about_CommonParameters
.
$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 fromWrite-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.
- Caveat: In Windows PowerShell (but no longer in PowerShell [Core] v6+, this will present an interactive debugging prompt for every
Whether output from Write-Debug
statements is printed is controlled by two mechanisms:
Scope-wide: by the value of the
$DebugPreference
preference variable - seeGet-Help about_Preference_Variables
.Ad-hoc, command-scoped, when calling a cmdlet or advanced script/function (which overrides the
$DebugPreference
value) with the-Debug
common parameter - seeGet-Help about_CommonParameters
.
$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 printsWrite-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 itsparam()
block, as Mathias' answer shows.
- For a custom script or function to support the
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屋!