gpt4 book ai didi

debugging - -调试请求确认

转载 作者:行者123 更新时间:2023-12-02 22:37:32 24 4
gpt4 key购买 nike

我有一个在 Powershell 3.0 中运行的简单脚本:
# Testing write-debug
[CmdletBinding()]
param()
write-debug "Debug message"
Write-Output "General output"

当我不带参数运行它时,我得到了所需的输出:

PS C:\scripts\Test> .\debugtest.ps1
General output

当我使用 -debug 运行它时参数,Powershell 在打印调试消息后要求我确认:
PS C:\scripts\Test> .\debugtest.ps1 -Debug
DEBUG: Debug message

Confirm
Continue with this operation?
[Y] Yes [A] Yes to All [H] Halt Command [S] Suspend [?] Help (default is "Y"):
General output

为什么要我确认?不应该 write-debug只需编写调试输出并继续脚本?

更新: $DebugPreference设置为 SilentlyContinue :
PS C:\scripts\Test> $DebugPreference
SilentlyContinue
PS C:\scripts\Test> .\debugtest.ps1 -Debug
DEBUG: Debug message

Confirm
Continue with this operation?
[Y] Yes [A] Yes to All [H] Halt Command [S] Suspend [?] Help (default is "Y"):
General output

最佳答案

听起来您的 $DebugPreference 变量设置为“查询”。

来自 Get-Help about_preference_variables :

$DebugPreference
------------------
Determines how Windows PowerShell responds to debugging messages
generated by a script, cmdlet or provider, or by a Write-Debug
command at the command line.

Some cmdlets display debugging messages, which are typically very
technical messages designed for programmers and technical support
professionals. By default, debugging messages are not displayed,
but you can display debugging messages by changing the value of
$DebugPreference.

You can also use the Debug common parameter of a cmdlet to display
or hide the debugging messages for a specific command. For more
information, type: "get-help about_commonparameters".

Valid values:
Stop: Displays the debug message and stops
executing. Writes an error to the console.

Inquire: Displays the debug message and asks you
whether you want to continue. Note that
adding the Debug common parameter to a
command--when the command is configured
to generate a debugging message--changes
the value of the $DebugPreference variable
to Inquire.

Continue: Displays the debug message and continues
with execution.

SilentlyContinue: No effect. The debug message is not
(Default) displayed and execution continues without
interruption.

编辑:-Debug 也是一个 cmdlet 通用参数,通过添加 CmdletBinding(),它也是你脚本的一个通用参数。

来自 Get-Help about_common_parameters :

常用参数说明
-Debug[:{$true | $false}]
Alias: db

Displays programmer-level detail about the operation performed by the
command. This parameter works only when the command generates
a debugging message. For example, this parameter works when a command
contains the Write-Debug cmdlet.

**The Debug parameter overrides the value of the $DebugPreference
variable for the current command, setting the value of $DebugPreference
to Inquire.** Because the default value of the $DebugPreference variable
is SilentlyContinue, debugging messages are not displayed by default.

Valid values:

$true (-Debug:$true). Has the same effect as -Debug.

$false (-Debug:$false). Suppresses the display of debugging
messages when the value of the $DebugPreference is not
SilentlyContinue (the default).

关于debugging - -调试请求确认,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23819601/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com