Skip to content

-Force implementation on ShouldProcess page should be reviewed and changed. #11418

@jikuja

Description

@jikuja

Type of issue

Code doesn't work

Feedback

function Test-ShouldProcess {
    [CmdletBinding(
        SupportsShouldProcess,
        ConfirmImpact = 'High'
    )]
    param(
        [Switch]$Force
    )

    if ($Force -and -not $Confirm){
        $ConfirmPreference = 'None'
    }

    if ($PSCmdlet.ShouldProcess('TARGET')){
        Write-Output "Some Action"
    }
}

According to my testing $Confirm is never present

function Test-ShouldProcess {
    [CmdletBinding(
        SupportsShouldProcess,
        ConfirmImpact = 'High'
    )]
    param(
        [Switch]$Force
    )

    Write-Verbose "`$DebugPreference: $DebugPreference"
    Write-Verbose "`$ConfirmPreference: $ConfirmPreference"
    Write-Verbose "`$Confirm: $Confirm"
    Write-Verbose "`$Confirm: $($null -eq $Confirm ? "NULL": "NONNULL")"

    if ($Force -and -not $Confirm){
        Write-Verbose "Changing `$ConfirmPreference"
        $ConfirmPreference = 'None'
    } else {
        Write-Verbose "XXXX"
    }

    Write-Verbose "`$DebugPreference: $DebugPreference"
    Write-Verbose "`$ConfirmPreference: $ConfirmPreference"
    Write-Verbose "`$Confirm: $Confirm"
    Write-Verbose "`$Confirm: $($null -eq $Confirm ? "NULL": "NONNULL")"

    if ($PSCmdlet.ShouldProcess('TARGET')){
        Write-Output "Some Action"
    }
}

Earlier ticket/work:


The example is really hard to understand and it does not work with strict mode.

Page URL

https://learn.microsoft.com/en-us/powershell/scripting/learn/deep-dives/everything-about-shouldprocess?view=powershell-7.4

Content source URL

https://github.com/MicrosoftDocs/PowerShell-Docs/blob/main/reference/docs-conceptual/learn/deep-dives/everything-about-shouldprocess.md

Author

@sdwheeler

Document Id

530b7644-3211-0f7c-3c63-d9959a72f541

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions