Skip to content

Instantly share code, notes, and snippets.

@zsoumya
Created March 9, 2020 02:34
Show Gist options
  • Save zsoumya/0b38e14e8c3d9677a3d876e4791cb393 to your computer and use it in GitHub Desktop.
Save zsoumya/0b38e14e8c3d9677a3d876e4791cb393 to your computer and use it in GitHub Desktop.
ShouldContinue vs ShouldProcess
function Test-SupportShouldProcess {
[CmdletBinding(SupportsShouldProcess)]
param(
[switch]$force
)
# Use this to prompt the user by default. Use -Force to disable prompting.
# NOTE: that you have to add the $force parameter yourself, as shown above. It doesn't even have to be
# named $force. How you implement this is up to you.
if ($force -or $PSCmdlet.ShouldContinue("Some resource", "Would you like to continue?") ) {
Write-Host "If you're reading this, you either passed in `$force or typed 'Y' when prompted." -ForegroundColor Red
}
# Use this to NOT prompt by default. Use -Confirm to enable prompting. Or, use the -WhatIf option to
# print to console the anticipated outcome of running this block - without actually running it.
# NOTE: Both -Confirm and -Whatif are common PS parameters. You don't need to implemented them like $force was above.
if ($PSCmdlet.ShouldProcess("Some other resource", "Would you like to process?") ) {
Write-Host 'This will always run UNLESS the -Confirm or -Whatif option is used.' -ForegroundColor Green
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment