Consider the following contrived example:
function Test-ProcessContinue {
[CmdletBinding(SupportsShouldProcess=$true, ConfirmImpact='High')]
Param()
for ($i = 1; $i -le 3; $i++) {
if ($PSCmdlet.ShouldProcess("$i", "Process")) {
Write-Output "Processing $i"
}
else {
Write-Verbose "No chosen"
}
}
for ($i = 1; $i -le 3; $i++) {
if ($PSCmdlet.ShouldProcess("$i", "Process")) {
Write-Output "Processing $i"
}
else {
Write-Verbose "No chosen"
}
}
$yta = $false; $nta = $false
for ($i = 1; $i -le 3; $i++) {
if ($PSCmdlet.ShouldContinue("$i", "Continue", [ref]$yta, [ref]$nta) -or $yta) {
Write-Output "Continuing with $i"
}
elseif ($nta) {
Write-Verbose "No to all chosen"
break
}
else {
Write-Verbose "No chosen"
}
}
}
...and one of its potential outputs:
PS C:\> Test-ProcessContinue -Verbose
Confirm
Are you sure you want to perform this action?
Performing the operation "Process" on target "1".
[Y] Yes [A] Yes to All [N] No [L] No to All [S] Suspend [?] Help (default is "Y"): a
Processing 1
Processing 2
Processing 3
Processing 1
Processing 2
Processing 3
Continue
1
[Y] Yes [A] Yes to All [N] No [L] No to All [S] Suspend [?] Help (default is "Y"): a
Continuing with 1
Continuing with 2
Continuing with 3
In the case of the ShouldContinue loop (third for loop), I can see that the overload with the two by-reference boolean parameters is responsible for storing whether the end-user chose Yes to All or No to All into those two booleans.
However, in the case of the two ShouldProcess blocks (first two for loops), how is this state preserved?
In particular, in between the first two ShouldProcess blocks, how could I check if Yes to All or No to All were specified and/or what would I need to reset or clear in order to make the second ShouldProcess block ask for confirmation again?
(Favouring ShouldContinue over ShouldProcess is an option for fine-grained control, but it appears to lose the native/built-in support for [CmdletBinding(SupportsShouldProcess=$true)]