91 lines
3.3 KiB
PowerShell
91 lines
3.3 KiB
PowerShell
function Test-ShouldProcess {
|
|
|
|
<#
|
|
.SYNOPSIS
|
|
A wrapper for PSCmdlet.ShouldProcess to enable Pester testing.
|
|
|
|
.DESCRIPTION
|
|
A wrapper for PSCmdlet.ShouldProcess to enable Pester testing.
|
|
|
|
.PARAMETER VerboseDescription
|
|
Textual description of the action to be performed. This is what will be displayed to the user for ActionPreference.Continue.
|
|
|
|
.PARAMETER VerboseWarning
|
|
Textual query of whether the action should be performed, usually in the form of a question. This is what will be displayed to the user for ActionPreference.Inquire.
|
|
|
|
.PARAMETER Caption
|
|
Caption of the window which may be displayed if the user is prompted whether or not to perform the action. caption may be displayed by some hosts, but not all.
|
|
|
|
.PARAMETER ShouldProcessReason
|
|
Indicates the reason(s) why ShouldProcess returned what it returned. Only the reasons enumerated in ShouldProcessReason are returned.
|
|
|
|
.PARAMETER Target
|
|
Name of the target resource being acted upon. This will potentially be displayed to the user.
|
|
|
|
.PARAMETER Action
|
|
Name of the action which is being performed. This will potentially be displayed to the user. (default is Cmdlet name)
|
|
|
|
.PARAMETER Force
|
|
When specified, always returns true. Added based on recommendations in the official documentation to support this option
|
|
|
|
.LINK
|
|
https://learn.microsoft.com/en-us/dotnet/api/system.management.automation.cmdlet.shouldprocess?view=powershellsdk-1.1.0
|
|
#>
|
|
|
|
[CmdletBinding(DefaultParameterSetName = 'TargetActionSet', SupportsShouldProcess = $true)]
|
|
[OutputType([System.Boolean])]
|
|
param (
|
|
|
|
[Parameter(ParameterSetName = 'VerboseSet', Mandatory = $true)]
|
|
[Parameter(ParameterSetName = 'ProcessReasonSet', Mandatory = $true)]
|
|
[string]$VerboseDescription,
|
|
|
|
[Parameter(ParameterSetName = 'VerboseSet', Mandatory = $true)]
|
|
[Parameter(ParameterSetName = 'ProcessReasonSet', Mandatory = $true)]
|
|
[string]$VerboseWarning,
|
|
|
|
[Parameter(ParameterSetName = 'VerboseSet', Mandatory = $true)]
|
|
[Parameter(ParameterSetName = 'ProcessReasonSet', Mandatory = $true)]
|
|
[string]$Caption,
|
|
|
|
[Parameter(ParameterSetName = 'ProcessReasonSet', Mandatory = $true)]
|
|
[string]$ShouldProcessReason,
|
|
|
|
[Parameter(ParameterSetName = 'TargetActionSet', Mandatory = $true)]
|
|
[Parameter(ParameterSetName = 'TargetSet', Mandatory = $true)]
|
|
[string]$Target,
|
|
|
|
[Parameter(ParameterSetName = 'TargetActionSet', Mandatory = $true)]
|
|
[string]$Action,
|
|
|
|
[Parameter(ParameterSetName = '__AllParameterSets', Mandatory = $false)]
|
|
[switch]$Force
|
|
)
|
|
|
|
if ($Force.IsPresent) {
|
|
|
|
return $true
|
|
}
|
|
|
|
$parameterSet = $PSCmdlet.ParameterSetName
|
|
|
|
if ($parameterSet -eq "VerboseSet") {
|
|
|
|
return $PSCmdlet.ShouldProcess($VerboseDescription, $VerboseWarning, $Caption)
|
|
}
|
|
|
|
if ($parameterSet -eq "ProcessReasonSet") {
|
|
|
|
return $PSCmdlet.ShouldProcess($VerboseDescription, $VerboseWarning, $Caption, [ref]$ShouldProcessReason)
|
|
}
|
|
|
|
if ($parameterSet -eq "TargetActionSet") {
|
|
|
|
return $PSCmdlet.ShouldProcess($Target, $Action)
|
|
}
|
|
|
|
if ($parameterSet -eq "TargetSet") {
|
|
|
|
return $PSCmdlet.ShouldProcess($Target)
|
|
}
|
|
} |