Skip to content

Instantly share code, notes, and snippets.

@codykonior
Last active July 18, 2019 10:13
Show Gist options
  • Save codykonior/6a8fab079eb8895905d42ce9dd78828d to your computer and use it in GitHub Desktop.
Save codykonior/6a8fab079eb8895905d42ce9dd78828d to your computer and use it in GitHub Desktop.
<#
IsReady is the option of an internal determination of whether an operation needs to execute or is safe to execute.
A switch of -WhatIf will override anything else.
A switch of -Force can override an unsafe operation. It is not prompted.
A prompt for confirmation only occurs if needed, and this will show on interactive sessions, while being denied on
noninteractive sessions.
#>
function Test-ShouldProcess {
[CmdletBinding(SupportsShouldProcess)]
param (
# IsReady would be set your code and is only specified here for testing purposes
[switch] $IsReady,
[switch] $Force
)
<#
...
Phase that determines if a change is necessary and safe
...
#>
# Confirmation phase
if (
# Simulate if WhatIf was used
$WhatIfPreference -or
(
# Simulate if we're not ready and $Force wasn't specified
-not ($isReady -or $Force) -or
# Simulate if settings do not require confirmation from the user
-not ($ConfirmPreference -eq "None" -or $ConfirmPreference -ge "Medium" -or (
# Simulate if we are able to receive confirmation from the user
$Host.UI.SupportsVirtualTerminal -and $PSCmdlet.ShouldProcess("Target", "Operation")
)
)
)
) {
# I prefer to use a Warning here because simulation messages get easily lost in Verbose printing
Write-Warning "Simulate"
} else {
Write-Verbose "Execute" -Verbose
}
}
Test-ShouldProcess # It's not ready, simulate
Test-ShouldProcess -IsReady # It's ready, execute
Test-ShouldProcess -Force # It's not ready, force execute
Test-ShouldProcess -WhatIf # It's irrelevant if it's ready, simulate
Test-ShouldProcess -IsReady -Confirm # It's ready, check with the user whether to execute or simulate
Test-ShouldProcess -IsReady -Confirm:$false # It's ready, execute
<#
Notes:
IsReady is the option of an internal determination of whether an operation needs to execute or is safe to execute.
A switch of -WhatIf will override anything else.
A switch of -Force can override an unsafe operation. It is not prompted.
A prompt for confirmation only occurs if needed, and this will show on interactive sessions, while being denied on
noninteractive sessions.
#>
function Test-ShouldContinue {
[CmdletBinding(SupportsShouldProcess)]
param (
# IsReady would be set your code and is only specified here for testing purposes
[switch] $IsReady,
[switch] $Force
)
<#
...
Phase that determines if a change is necessary and safe
...
#>
# Confirmation phase
if (
# Simulate if WhatIf was used
$WhatIfPreference -or
(
# Simulate if we're not ready and $Force wasn't specified
-not ($isReady -or $Force) -or
# Simulate if settings do not require confirmation from the user
-not ($ConfirmPreference -eq "None" -or $ConfirmPreference -ge "Medium" -or (
# Simulate if we are able to receive confirmation from the user
$Host.UI.SupportsVirtualTerminal -and $PSCmdlet.ShouldContinue("Target", "Operation")
)
)
)
) {
# I prefer to use a Warning here because simulation messages get easily lost in Verbose printing
Write-Warning "Simulate"
} else {
Write-Verbose "Execute" -Verbose
}
}
Test-ShouldContinue # It's not ready, simulate
Test-ShouldContinue -IsReady # It's ready, execute
Test-ShouldContinue -Force # It's not ready, force execute
Test-ShouldContinue -WhatIf # It's irrelevant if it's ready, simulate
Test-ShouldContinue -IsReady -Confirm # It's ready, check with the user whether to execute or simulate
Test-ShouldContinue -IsReady -Confirm:$false # It's ready, execute
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment