ShouldContinue

The ShouldContinue method is also made available when the SupportsShouldProcess property is set in CmdletBinding.

ShouldContinue differs from ShouldProcess in that it always prompts. This technique is used by commands such as Remove-Item to force a prompt when the Recurse parameter is not present and a directory name is passed.

ShouldContinue is rarely necessary, since ShouldProcess is the better option. It is available for the cases where a function must have a confirmation prompt that cannot be bypassed. Using ShouldContinue may make it impossible to run a function without user interaction unless also providing a means to bypass the prompt.

The use of ShouldContinue is similar to ShouldProcess. The most significant difference is that the Yes to All and No to All options are not automatically implemented:

function Test-ShouldContinue {
[CmdletBinding(SupportsShouldProcess)]
param ( )

$yesToAll = $noToAll = $false
if ($pscmdlet.ShouldContinue(
"Warning: Deleting SomeObject $value",
'Question: Are you sure you want to do continue?')) {

Write-Host 'Deleting SomeObject' -ForegroundColor Cyan
}
}

Running this function will show the confirmation prompt:

PS> Test-ShouldContinue

Question: Are you sure you want to do continue?
Warning: Deleting SomeObject
[Y] Yes [N] No [S] Suspend [?] Help (default is "Y"): y

Adding support for Yes to All and No to All means using three extra arguments. The first of these new arguments, hasSecurityImpact, affects whether the default option presented is Yes (when hasSecurityImpact is false) or No (when hasSecurityImpact is true):

function Test-ShouldContinue {
[CmdletBinding(SupportsShouldProcess)]
param ( )

$yesToAll = $noToAll = $false
if ($pscmdlet.ShouldContinue(
"Warning: Deleting SomeObject $value",
'Question: Are you sure you want to do continue?',
$false,
[Ref]$yesToAll,
[Ref]$noToAll)) {

Write-Host 'Deleting SomeObject' -ForegroundColor Cyan
}
}

The confirmation prompt will now include the Yes to All and No to All options:

PS> Test-ShouldContinue

Question: Are you sure you want to do continue?
Warning: Deleting SomeObject
[Y] Yes [A] Yes to All [N] No [L] No to All [S] Suspend [?] Help (default is "Y"):

If necessary, it is possible to provide a means of bypassing the prompt by implementing another switch parameter. For example, a Force parameter might be added:

function Test-ShouldContinue {
[CmdletBinding(SupportsShouldProcess)]
param (
[Switch]$Force
)


$yesToAll = $noToAll = $false
if ($Force -or $pscmdlet.ShouldContinue(
"Warning: Deleting SomeObject $value",
'Question: Are you sure you want to do continue?',
$false,
[Ref]$yesToAll,
[Ref]$noToAll)) {

Write-Host
'Deleting SomeObject' -ForegroundColor Cyan
}

}

As the value of Force is evaluated before ShouldContinue, the ShouldContinue method will not run if the Force parameter is supplied.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
18.188.241.82