SupportsShouldProcess does not create $Confirm in a way recognized by Strict Mode
See original GitHub issueSteps 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:
- Created 3 years ago
- Comments:6 (1 by maintainers)
Top 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 >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
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.@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: