ShouldProcess

ShouldProcess is used to support WhatIf and is responsible for showing confirmation preferences based on the impact level of a command.

ShouldProcess is also used to prompt for confirmation when a command is performing a higher-risk action.

The following example will display a message instead of running the Write-Host statement when the WhatIf parameter is supplied:

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

if ($pscmdlet.ShouldProcess('SomeObject')) {
Write-Host 'Deleting SomeObject' -ForegroundColor Cyan
}
}

When run using the WhatIf parameter, the command will show the following message:

PS> Test-ShouldProcess -WhatIf
What if: Performing the operation "Test-ShouldProcess" on target "SomeObject".

The name of the operation, which defaults to the command name, can be changed using a second overload for ShouldProcess:

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

if ($pscmdlet.ShouldProcess('SomeObject', 'delete')) {
Write-Host 'Deleting SomeObject' -ForegroundColor Cyan
}
}

This would change the message to the following:

PS> Test-ShouldProcess -WhatIf
What if: Performing the operation "delete" on target "SomeObject".

The next overload grants full control over the messages that display:

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

if ($pscmdlet.ShouldProcess(

'Message displayed using WhatIf',
'Warning: Deleting SomeObject',
'Question: Are you sure you want to do continue?')) {

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

Using the Confirm parameter instead of WhatIf forces the appearance of the second two messages and adds a prompt:

PS> Test-ShouldProcess -Confirm

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"):

The different responses are automatically available without further code. If the request is within a loop, Yes to All may be used to bypass additional prompts. Replying Yes to All applies to all instances of ShouldProcess in the script or function:

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

foreach ($value in 1..2) {
if ($pscmdlet.ShouldProcess(
"Would delete SomeObject $value",
"Warning: Deleting SomeObject $value",
'Question: Are you sure you want to do continue?')) {

Write-Host "Deleting SomeObject $value" -ForegroundColor Cyan
}
}
}

Whether or not the confirmation prompt is displayed depends on the comparison between ConfirmImpact (medium by default), and the value in the $ConfirmPreference variable, which is High by default.

If the impact of the function is raised to High, the prompt will display by default instead of on demand. This is achieved by modifying the ConfirmImpact property of the CmdletBinding attribute:

function Test-ShouldProcess {
[CmdletBinding(SupportsShouldProcess, ConfirmImpact = 'High')]
param ( )

if ($pscmdlet.ShouldProcess('SomeObject')) {
Write-Host 'Deleting SomeObject' -ForegroundColor Cyan
}
}

When the function is executed, the confirmation prompt will show unless the user either uses -Confirm:$false or sets $ConfirmPreference to None.

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

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