-Verbose dangerous?

This topic contains 3 replies, has 3 voices, and was last updated by Profile photo of Don Jones Don Jones 2 weeks, 4 days ago.

Viewing 4 posts - 1 through 4 (of 4 total)
  • Author
    Posts
  • #53630
    Profile photo of Tim Ker
    Tim Ker
    Participant

    Hi, when I add -Verbose to a command it changes the termination behavior.

    when adding -verbose when $ErrorActionPreference = "Stop", the command continues instead of stopping.

     $ErrorActionPreference = "Stop"
     New-Item existingItem.txt  -Verbose 
    

    I believe this should stop, not continue. Why am I mistaken?

    https://gist.github.com/timker/2ff654aef81e72046a45596fe7d78212.js

    $ErrorActionPreference = "Stop"
    $VerbosePreference = "continue"
    
    #creating a file for first run only
    if(-Not (Test-Path test.txt))
    {
        new-item test.txt  -Verbose 
    }
    
    #create the same file
    new-item test.txt  -Verbose
    Write-output 'This will never output because of the error above... or will it'
    
    
    new-item test.txt 
    Write-output 'This will ALSO never output because of the error above'
    
    
    #53636
    Profile photo of Jonathan Warnken
    Jonathan Warnken
    Participant

    please see get-help about_Preference_Variables and\or https://technet.microsoft.com/en-us/library/hh847796.aspx

    The default value of the VerbosePreference variable is SilentlyContinue. This will be used in place of the ErrorActionPreference variable when the -verbose switch is used, unless you specify the ErrorAction on the command.

    This will work the way you think it should

    $ErrorActionPreference = "Stop"
    $VerbosePreference = "continue"
    
    #creating a file for first run only
    if(-Not (Test-Path test.txt))
    {
        new-item test.txt  -Verbose 
    }
    
    #create the same file
    new-item test.txt  -Verbose -ErrorAction Stop
    Write-output 'This will never output because of the error above... or will it'
    
    
    new-item test.txt 
    Write-output 'This will ALSO never output because of the error above'
    
    #53642
    Profile photo of Tim Ker
    Tim Ker
    Participant

    Thanks Jonothan, but I'm not seeing any documentation that -Verbose overrides $ErrorActionPreference

    I've also looked in about_Common_Parameters
    https://technet.microsoft.com/en-us/library/hh847884.aspx

    But i can't find anything that states the -Verbose will override $ErrorActionPreference

    I would have guessed that -Verbose would only effect the the Verbose pipeline, but it looks to change the ErrorPipeline as well!

    #53730
    Profile photo of Don Jones
    Don Jones
    Keymaster

    It depends on how the command is coded internally. When -Verbose is enabled, it's possible that the command – internally – isn't following a code path that can throw an exception. That's not a good command design, and it isn't a universal shell behavior. If you've verified the behavior, I'd suggest opening a ticket on UserVoice for the PowerShell team. Or, since New-Item is part of PowerShell Core, you might review the code on the PowerShell Core GitHub, and see if you can find the problem and propose a fix.

Viewing 4 posts - 1 through 4 (of 4 total)

You must be logged in to reply to this topic.