-
- Operations with ConfirmImpact equal to or greater than $ConfirmPreference are confirmed
IF ConfirmImpact >= ConfirmPreference THEN prompt
- Also:
- Operations with ConfirmImpact.None are never confirmed
- no operations are confirmed when $ConfirmPreference is ConfirmImpact.None (except when explicitly requested with -Confirm).
-
- When the value of the $ConfirmPreference variable is less than or equal to the risk assigned to a cmdlet or function, PowerShell automatically prompts you for confirmation before running the cmdlet or function.
IF ConfirmPreference <= ConfirmImpact THEN prompt
IF ConfirmImpact > ConfirmPreference THEN prompt
-
- The call to the ShouldProcess method displays a confirmation prompt only when the ConfirmImpact argument is equal to or greater than the value of the $ConfirmPreference preference variable.
IF ConfirmImpact >= ConfirmPreference THEN prompt
-
- If Confirm parameter is not used, the ShouldProcess() call requests confirmation if the $ConfirmPreference preference variable is equal to or greater than the ConfirmImpact setting of the cmdlet or provider.
IF ConfirmPreference >= ConfirmImpact THEN prompt
IF ConfirmImpact < ConfirmPreference THEN prompt
-
IF ((threshold == ConfirmImpact.None) || (threshold > cmdletConfirmImpact)) THEN autoconfirm
- =>
IF ConfirmPreference <= ConfirmImpact THEN prompt
IF ConfirmImpact > ConfirmPreference THEN prompt
Last active
September 23, 2024 14:13
-
-
Save jikuja/57bfbfd01f3c9ea48c55234b769a1f86 to your computer and use it in GitHub Desktop.
PowerShell Confirms
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function Test-ShouldProcess { | |
[CmdletBinding( | |
SupportsShouldProcess, | |
ConfirmImpact = 'High' | |
)] | |
param( | |
[Switch]$Force | |
) | |
Write-Verbose "`$PSCmdLet: $PSCmdLet" | |
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")" | |
# Does this ignore $ConfirmPreference? | |
# The call to $PSCmdlet.ShouldProcess($file.name) checks for the -WhatIf (and -Confirm parameter) then handles it accordingly. | |
if ($PSCmdlet.ShouldProcess('TARGET')){ | |
Write-Output "Some Action" | |
} | |
if ($Force -or $PSCmdlet.ShouldContinue('TARGET','OPERATION')) { | |
Write-Output "Some Action" | |
} | |
} | |
<# | |
> $ConfirmPreference="Low" ; Test-ShouldProcess -force -confirm | |
Asks confirmation for ShouldProcess | |
> $ConfirmPreference="High" ; Test-ShouldProcess | |
Asks confirmation for both | |
> $ConfirmPreference="High" ; Test-ShouldProcess -Force | |
No confirmation asked | |
> $ConfirmPreference="High" ; Test-ShouldProcess -Force -Confirm | |
Asks confirmation for ShouldProcess | |
If both -Confirm and -Force are used Confirmation for ShouldProcess are requested. | |
Still logic of `if ($Force -and -not $Confirm){` does not make any sense and will not work on strict mode | |
#> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment