-
Notifications
You must be signed in to change notification settings - Fork 1.7k
Labels
area-learnArea - LearnArea - Learn
Description
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:
- A code snippet from the page produces an error with strict mode. #6948 / SupportsShouldProcess does not create $Confirm in a way recognized by Strict Mode PowerShell/PowerShell#14294
- => removal of $Confirm: 00e88da
- Clearly point out snippet is an anti-pattern under the section Implementing Force #9007
- => Usage of $Confirm was added back: e5fadd7
The example is really hard to understand and it does not work with strict mode.
Page URL
Content source URL
Author
Document Id
530b7644-3211-0f7c-3c63-d9959a72f541
Metadata
Metadata
Assignees
Labels
area-learnArea - LearnArea - Learn