diff --git a/src/System.Management.Automation/engine/MshCommandRuntime.cs b/src/System.Management.Automation/engine/MshCommandRuntime.cs index c24844be84..d390fc1b82 100644 --- a/src/System.Management.Automation/engine/MshCommandRuntime.cs +++ b/src/System.Management.Automation/engine/MshCommandRuntime.cs @@ -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 diff --git a/src/System.Management.Automation/engine/runtime/CompiledScriptBlock.cs b/src/System.Management.Automation/engine/runtime/CompiledScriptBlock.cs index 7a6763b528..01398dcb0e 100644 --- a/src/System.Management.Automation/engine/runtime/CompiledScriptBlock.cs +++ b/src/System.Management.Automation/engine/runtime/CompiledScriptBlock.cs @@ -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) { diff --git a/test/powershell/Language/Scripting/ActionPreference.Tests.ps1 b/test/powershell/Language/Scripting/ActionPreference.Tests.ps1 index ecb0eafea0..134d4d1836 100644 --- a/test/powershell/Language/Scripting/ActionPreference.Tests.ps1 +++ b/test/powershell/Language/Scripting/ActionPreference.Tests.ps1 @@ -97,8 +97,8 @@ Describe "Tests for (error, warning, etc) action preference" -Tags "CI" { } It ' does not take precedence over $ErrorActionPreference' -TestCases @( - @{switch="-Verbose"}, - @{switch="-Debug"} + @{switch="Verbose"}, + @{switch="Debug"} ) { param($switch) $ErrorActionPreference = "SilentlyContinue" diff --git a/test/powershell/Modules/Microsoft.PowerShell.Utility/Write-Debug.Tests.ps1 b/test/powershell/Modules/Microsoft.PowerShell.Utility/Write-Debug.Tests.ps1 index 1102b9f2e2..7b7dd68b8e 100644 --- a/test/powershell/Modules/Microsoft.PowerShell.Utility/Write-Debug.Tests.ps1 +++ b/test/powershell/Modules/Microsoft.PowerShell.Utility/Write-Debug.Tests.ps1 @@ -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 + } } diff --git a/test/powershell/Modules/Microsoft.PowerShell.Utility/Write-Stream.Tests.ps1 b/test/powershell/Modules/Microsoft.PowerShell.Utility/Write-Stream.Tests.ps1 index f04a87c8c0..d0520124f2 100644 --- a/test/powershell/Modules/Microsoft.PowerShell.Utility/Write-Stream.Tests.ps1 +++ b/test/powershell/Modules/Microsoft.PowerShell.Utility/Write-Stream.Tests.ps1 @@ -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"