mirror of
https://github.com/PowerShell/PowerShell
synced 2026-06-08 12:12:50 +00:00
Debug parameter now sets debugpreference to continue (#8195)
With this commit, users who invoke a command with -Debug will no longer be presented with a prompt asking them if they want to enter a nested prompt, continue execution, or halt execution entirely. Instead, any messages sent to the debug stream will simply be sent to the debug stream and the script will continue execution.
This commit is contained in:
@@ -2922,24 +2922,13 @@ namespace System.Management.Automation
|
||||
get
|
||||
{
|
||||
if (_isDebugPreferenceSet)
|
||||
{
|
||||
return _debugPreference;
|
||||
}
|
||||
|
||||
if (IsDebugFlagSet)
|
||||
{
|
||||
if (Debug)
|
||||
{
|
||||
// If the host couldn't prompt for the debug action anyways, use 'Continue'.
|
||||
// This lets hosts still see debug output without having to implement the prompting logic.
|
||||
if (CBhost.ExternalHost.UI == null)
|
||||
{
|
||||
return ActionPreference.Continue;
|
||||
}
|
||||
else
|
||||
{
|
||||
return ActionPreference.Inquire;
|
||||
}
|
||||
}
|
||||
else
|
||||
return ActionPreference.SilentlyContinue;
|
||||
return Debug ? ActionPreference.Continue : ActionPreference.SilentlyContinue;
|
||||
}
|
||||
|
||||
if (!_isDebugPreferenceCached)
|
||||
@@ -2957,6 +2946,7 @@ namespace System.Management.Automation
|
||||
|
||||
_isDebugPreferenceCached = true;
|
||||
}
|
||||
|
||||
return _debugPreference;
|
||||
}
|
||||
set
|
||||
|
||||
@@ -2163,7 +2163,7 @@ namespace System.Management.Automation
|
||||
if (_commandRuntime.IsDebugFlagSet)
|
||||
{
|
||||
_localsTuple.SetPreferenceVariable(PreferenceVariable.Debug,
|
||||
_commandRuntime.Debug ? ActionPreference.Inquire : ActionPreference.SilentlyContinue);
|
||||
_commandRuntime.Debug ? ActionPreference.Continue : ActionPreference.SilentlyContinue);
|
||||
}
|
||||
if (_commandRuntime.IsVerboseFlagSet)
|
||||
{
|
||||
|
||||
@@ -97,8 +97,8 @@ Describe "Tests for (error, warning, etc) action preference" -Tags "CI" {
|
||||
}
|
||||
|
||||
It '<switch> does not take precedence over $ErrorActionPreference' -TestCases @(
|
||||
@{switch="-Verbose"},
|
||||
@{switch="-Debug"}
|
||||
@{switch="Verbose"},
|
||||
@{switch="Debug"}
|
||||
) {
|
||||
param($switch)
|
||||
$ErrorActionPreference = "SilentlyContinue"
|
||||
|
||||
@@ -16,4 +16,18 @@ Describe "Write-Debug tests" -Tags "CI" {
|
||||
$DebugPreference = $origDebugPref
|
||||
}
|
||||
}
|
||||
|
||||
It "Should not prompt the user" {
|
||||
# This script generates an error if Write-Debug prompts the user
|
||||
# (i.e. if $DebugPreference is set to Inquire, the old v1 way)
|
||||
$p = [Diagnostics.Process]::new()
|
||||
$p.StartInfo.FileName = (Get-Process -Id $PID).Path
|
||||
$encoded = [Convert]::ToBase64String([Text.Encoding]::Unicode.GetBytes("Write-Debug -Message 'A debug message' -Debug"))
|
||||
$p.StartInfo.Arguments = "-EncodedCommand $encoded -ExecutionPolicy Bypass -NoLogo -NonInteractive -NoProfile -OutputFormat text"
|
||||
$p.StartInfo.UseShellExecute = $false
|
||||
$p.StartInfo.RedirectStandardError = $true
|
||||
$p.Start() | Out-Null
|
||||
$out = $p.StandardError.ReadToEnd()
|
||||
$out | Should -BeNullOrEmpty
|
||||
}
|
||||
}
|
||||
|
||||
@@ -10,7 +10,7 @@ Describe "Stream writer tests" -Tags "CI" {
|
||||
[CmdletBinding()]
|
||||
|
||||
param()
|
||||
If ($PSBoundParameters['Debug']) { $DebugPreference = 'Continue' }
|
||||
|
||||
Write-Verbose "Verbose message"
|
||||
|
||||
Write-Debug "Debug message"
|
||||
|
||||
Reference in New Issue
Block a user