首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >-debug请求确认

-debug请求确认
EN

Stack Overflow用户
提问于 2014-05-23 01:17:26
回答 2查看 3.3K关注 0票数 11

我有一个在Powershell 3.0中运行的简单脚本:

# Testing write-debug [CmdletBinding()] param() write-debug "Debug message" Write-Output "General output"

当我在没有参数的情况下运行它时,我得到了所需的输出:

代码语言:javascript
复制
PS C:\scripts\Test> .\debugtest.ps1
General output

当我使用-debug参数运行它时,Powershell要求我在打印调试消息之后确认:

代码语言:javascript
复制
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

代码语言:javascript
复制
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
EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2014-05-23 01:23:51

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

从自助变量

代码语言:javascript
复制
$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也是,通过添加CmdletBinding(),它也是脚本的公共参数。

从自助参数

公共参数描述

代码语言:javascript
复制
-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). 
票数 5
EN

Stack Overflow用户

发布于 2018-02-08 04:28:39

我认为以下函数的第二个参数声明可以添加任何函数

代码语言:javascript
复制
function SetDebugPreference {
[CmdletBinding()]
Param(
    [Parameter(
        Mandatory=$true,
        HelpMessage="Please enter for `$DebugPreference a value`n('SilentlyContinue','Continue' ,'Inquire' or 'Stop')",            
        ValueFromPipeline=$true,
        ValueFromPipelineByPropertyName=$true,
        Position=0
       )]
    [ValidateSet("SilentlyContinue","Continue" ,"Inquire","Stop")]
    [Alias("dbp","dbPref")]    
    [string]
    $dbPreference,

    [Parameter(
        Mandatory=$false,
        ValueFromPipelineByPropertyName=$true,
        Position=1
       )]
    [ValidateSet("SilentlyContinue","Continue" ,"Inquire","Stop")]
    [Alias("ldbp","ldbPref")]    
    [string]
    $LocalDebugPreference="Continue"
) 
Begin {
    Write-Verbose ("Local DebugPreference: " + $DebugPreference)
    $DebugPreference=$LocalDebugPreference
    Write-Verbose ("Local DebugPreference set: " + $LocalDebugPreference)
    Write-Debug "Local debug  test"

}   
Process {
    Write-Verbose ("Global DebugPreference: " + $Global:DebugPreference)
    $Global:DebugPreference=$dbPreference
    Write-Verbose ("Global DebugPreference set: " + $Global:DebugPreference)
}

}

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/23819601

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档