Author Posts

April 4, 2016 at 11:35 am

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

April 6, 2016 at 9:46 am

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