question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

SupportsShouldProcess does not create $Confirm in a way recognized by Strict Mode

See original GitHub issue

Steps to reproduce

Set-StrictMode -Version:Latest

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"
    }
}

Test-ShouldProcess -Force

Actual behavior

InvalidOperation: C:\temp\testsp.ps1:12
Line |
  12 |      if ($Force -and -not $Confirm){
     |                           ~~~~~~~~
     | The variable '$Confirm' cannot be retrieved because it has not been set.

This error does not occur when Strict Mode is off. If you try to add $Confirm as a switch parameter you get the following error:

Test-ShouldProcess: C:\temp\testsp.ps1:22
Line |
  22 |  Test-ShouldProcess -Force -confirm:$true
     |  ~~~~~~~~~~~~~~~~~~
     | A parameter with the name 'Confirm' was defined multiple times for the command.

So Strict Mode does not see that $Confirm has been defined, but SupportsShouldProcess does not allow you to define it.

Environment data

This was tested in PowerShell 5.1 and 7.1.

Issue Analytics

  • State:closed
  • Created 3 years ago
  • Comments:6 (1 by maintainers)

github_iconTop GitHub Comments

1reaction
SeeminglySciencecommented, Dec 1, 2020

Yeah. Creating a variable for the actual switch parameter wouldn’t make sense because a parameter isn’t the only way these things are scoped. Verbose, warning, whatif, debug, confirm, etc are all propagated via the preference variables and also to child scopes and cmdlet calls.

Force is different because it’s purely convention.

1reaction
SeeminglySciencecommented, Nov 30, 2020

@sdwheeler A parameter is defined, but it is not translated into a variable. Same way other common parameters work like -Verbose, there’s no $Verbose variable created.

The example should be more like this:

if ($Force -or $PSCmdlet.ShouldProcess('Target')) {
  # do thing
}
Read more comments on GitHub >

github_iconTop Results From Across the Web

Everything you wanted to know about ShouldProcess
ShouldProcess is an important feature that is often overlooked is. The WhatIf and Confirm parameters make it easy to add to your functions....
Read more >
Powershell Cmdlet with 'dynamic' ConfirmImpact attribute ...
It looks to be a more direct method of requesting confirmation from the user than the ShouldProcess , where the confirmation message depends ......
Read more >
Supports should process…? Oh really?
I want it to prompt me even if I don't use -Confirm. I set the $ConfirmPreference to “Low”, but it doesn't work without...
Read more >
Functions/Mock.ps1 4.3.1
The last one created will be the first to be evaluated. The mock of the first filter to pass will be used. The...
Read more >
Powershell script to remove files older the X days, keep ...
Tried this after some minor changes (for deleting file i used CreationTime and Lastwrite was not recognized and changed this to LastWriteTime).
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found