Debugging -调试请求确认
我有一个在Powershell 3.0中运行的简单脚本:Debugging -调试请求确认,debugging,powershell,Debugging,Powershell,我有一个在Powershell 3.0中运行的简单脚本: #测试写调试 [CmdletBinding()] param() 编写调试“调试消息” 写入输出“一般输出” 当我在没有参数的情况下运行它时,我会得到所需的输出: PS C:\scripts\Test> .\debugtest.ps1 General output 当我使用-debug参数运行它时,Powershell要求我在打印调试消息后确认: PS C:\scripts\Test> .\debugtest.ps1 -De
#测试写调试
[CmdletBinding()]
param()
编写调试“调试消息”
写入输出“一般输出”
当我在没有参数的情况下运行它时,我会得到所需的输出:
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
为什么要我确认?不应该只写调试输出并继续脚本吗
更新:$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变量设置为“Inquire” 从获取帮助: Edit:-Debug也是cmdlet公共参数,通过添加CmdletBinding(),它也是脚本的公共参数 从获取帮助: 通用参数描述
-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).
我认为下列函数的第二个参数声明可以添加到任何函数中
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)
}
}我也是这么想的,不过我的DebugPreference已经设置为SilentlyContinue。问题更新了更多信息。你说得对-当我更新脚本以显示
$DebugPreference
变量时,它被设置为Inquire。不完全是我所希望的,但我可以使用-verbose
开关来代替我所追求的。谢谢你的回答!
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)
}