Skip to content

Write-Debug Prompts for Confirmation Stopping All Script Execution When Using -Debug For Some Reason #5148

Closed

Description

Steps to reproduce

Save this to a file with a .ps1 extension and run it

function Test-DebugMessage
{
     [CmdletBinding()]
     [OutputType()]

     param ()

     process
     {
         Write-Debug ( 'Test Message' )
     }
}

Test-DebugMessage -Debug

Expected behavior

The following message is emitted

DEBUG: Test Message

Actual behavior

Prompted to continue for every Write-Debug statement. A for "Yes to All" does nothing

DEBUG: Test Message

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

Environment data

> $PSVersionTable.GetEnumerator() | ForEach-Object { Write-Host ('{0,26} : {1,-20}' -f $PSItem.Key,($PSItem.Value -join ', ') ) }
                 PSVersion : 5.1.15063.674
                 PSEdition : Desktop
      PSCompatibleVersions : 1.0, 2.0, 3.0, 4.0, 5.0, 5.1.15063.674
              BuildVersion : 10.0.15063.674
                CLRVersion : 4.0.30319.42000
         WSManStackVersion : 3.0
 PSRemotingProtocolVersion : 2.3
      SerializationVersion : 1.1.0.1

Activity

  1. markekraus commented on Oct 17, 2017

    @markekraus
    Contributor

    I believe that is by deisgn. you can instead do the following:

    function Test-DebugMessage {
         [CmdletBinding()]
         [OutputType()]
         param ()
         process
         {
             Write-Debug 'Test Message'
         }
    }
    $DebugPreference = 'Continue'
    Test-DebugMessage 

    And the debug messages will appear without stopping and prompting.

    see about_CommonParamters and about_preference_variables

  2. added
    Issue-Questionideally support can be provided via other mechanisms, but sometimes folks do open an issue to get a
    on Oct 17, 2017
  3. RickFlist commented on Oct 17, 2017

    @RickFlist
    Author

    Okay... Why? What use case demands that all script execution stop when emitting debug messages? How does that use-case compare with the use case of being able to use -Debug without script execution being interrupted? Why doesn't -Verbose cause the same behavior?

    There is no way to tell if this is actually a bug or not, so I don't know if I need to continue harping here or take it to User Voice

  4. markekraus commented on Oct 17, 2017

    @markekraus
    Contributor

    To distinguish it from write-verbose. If it acted exactly as write-verbose what purpose would it serve? Generally you wouldn't keep write-debug statements in code outside of development. You only put it in development to code so you can perform suspend.

    function Test-DebugMessage {
         [CmdletBinding()]
         [OutputType()]
         param ()
         process
         {
             $a = 5
             Write-Debug 'First a'
             $a = 10
             Write-Debug 'Second a'
         }
    }
    Test-DebugMessage -Debug

    when it prompts choose suspend then run $a and see that it is 5. run exit and chose the option to continue. At the next prompt choose suspend and run $a and see that it is 10. run exit and then chose the option to continue. This provides an easy way to break into the debugger and check the state of or manipulat variables in your code for the purposes of debugging.

    If you just want information on the screen you have Write-Verbose and Write-Information.

  5. RickFlist commented on Oct 17, 2017

    @RickFlist
    Author

    Oh also, can you define "By design"? When I hear "by design" I think there is a specification somewhere that says "when you use -Debug we're going to inquire for every single message emission". Is that the case ? What is "By design"?

  6. markekraus commented on Oct 17, 2017

    @markekraus
    Contributor

    Todd Lehmann (@RickFlist) I mean it is probably by design because it is officially documented behavior. That's usually a good indication of the design. If the behavior differed from the documented behavior, that would like not be by design or possible a documentation issue.

  7. RickFlist commented on Oct 17, 2017

    @RickFlist
    Author

    Oh. Yes, it's usually an excellent indicator. Whoops. :\

    The reasoning is actually there as well, which is awesome.
    "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."

    Any information as to why this doesn't occur with -Verbose? Ostensibly they would work the same way but I don't get prompted with -Verbose. Just curious

  8. RickFlist commented on Oct 17, 2017

    @RickFlist
    Author

    And what about the fact that "A" for "Yes to All" doesn't actually work? e.g. if I have multiple write-debug statements and hit "A" for the first one, I am still prompted for the second one. By design or bug? If the latter I will submit a new bug.

  9. markekraus commented on Oct 17, 2017

    @markekraus
    Contributor

    That's also by design. the "yes to all" works with this usage:

    function Test-DebugMessage
    {
         [CmdletBinding()]
         [OutputType()]
         param ()
         process
         {
             1,2,3,4 | Write-Debug 
         }
    }

    It's not for all calls to Write-Debug, just for all objects passed to the current call to Write-Debug.

  10. RickFlist commented on Oct 17, 2017

    @RickFlist
    Author

    I'll open up a couple User Voice request. Danke.

  11. KirkMunro commented on Nov 30, 2018

    @KirkMunro
    Contributor

    Hey Todd Lehmann (@RickFlist), just FYI, my PR that fixes this behavior was just merged, so in an upcoming release -Debug will simply enable debug message output without any of the prompting that was there before.

  12. RobinWPollard commented on Jun 7, 2019

    @RobinWPollard

    You can switch it to output the debug, but not pause ...

    if ($DebugPreference -eq "Inquire") {
        $DebugPreference = 'Continue'
        Write-Debug "We are running with Debug active. Expect lots of logging"
    }
  13. KirkMunro commented on Jun 7, 2019

    @KirkMunro
    Contributor

    Robin Pollard (@RobinWPollard) For older releases, that workaround works. In PowerShell 6.2 and later though, there is no need for that workaround.

  14. HQJaTu commented on Jun 20, 2019

    @HQJaTu

    Thanks for that Kirk Munro (@KirkMunro).
    Since the pre 6.2 behaviour was very annoying, I simply upgraded to 6.2.1 and continued working with much less friction.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    Issue-Questionideally support can be provided via other mechanisms, but sometimes folks do open an issue to get aResolution-By DesignThe reported behavior is by design.

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions