PSCmdlet.ShouldProcess output verbose stream even when $VerbosePreference='Silen

This topic contains 1 reply, has 2 voices, and was last updated by Profile photo of Don Jones Don Jones 5 months, 3 weeks ago.

Viewing 2 posts - 1 through 2 (of 2 total)
  • Author
    Posts
  • #37219
    Profile photo of Bor
    Bor
    Participant

    Hi,
    I have just found very strong behavior of $PSCmdlet.ShouldProcess(). I want to have it in my advanced function (I want to use -Confirm and -WhatIf) but I don't want to see it's verbose stream. I want to see only my custom streams (Write-Verbose). Please do you have an idea what it reason that the function output it? How to disable it?

    Thank you,
    Bor

    Function Set-SomeThing {
        [CmdletBinding(
            DefaultParametersetName = 'Bla',
            SupportsShouldProcess = $true,
            PositionalBinding = $false,
            ConfirmImpact = 'Medium'
        )]
    
        Param(
            [Parameter(
                Mandatory = $false,
                Position = 0,
                ParameterSetName = 'Bla'
            )]
            [string] $Bla,
    
            [Parameter(
                Mandatory = $false,
                ParameterSetName = 'Bla'
            )]
            [string] $Force
        )
    
        Begin {
            # Default:  $VerbosePreference = 'SilentlyContinue'
            # -Verbose: $VerbosePreference = 'Continue'
        }
    
        Process {
            Write-Verbose -Message 'Something that I want to see'
            
            $VerbosePreference = 'SilentlyContinue'
            $global:VerbosePreference = 'SilentlyContinue'
            $script:VerbosePreference = 'SilentlyContinue'
    
            if ($Force -or $PSCmdlet.ShouldProcess('I do NOT want to see this', 'Action')) {
                'Action'
            }
        }
    
        End {
        }
    }
    
    
    Set-SomeThing -Verbose
    
    
    Output:
    
    VERBOSE: Something that I want to see
    VERBOSE: Performing the operation "Action" on target "I do NOT want to see this".
    Action
    
    
    #37324
    Profile photo of Don Jones
    Don Jones
    Keymaster

    That's a hardcoded behavior – you can't work around that.

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

You must be logged in to reply to this topic.