Other examples couldn't clearly explain to me how to trigger the conditional logic.
This example also shows that underlying commands will also listen to the -Confirm flag!
<#
Restart-Win32Computer
#>
function Restart-Win32Computer
{
[CmdletBinding(SupportsShouldProcess=$true,ConfirmImpact="High")]
param (
[parameter(Mandatory=$true,ValueFromPipeline=$true,ValueFromPipelineByPropertyName=$true)]
[string[]]$computerName,
[parameter(Mandatory=$true)]
[string][ValidateSet("Restart","LogOff","Shutdown","PowerOff")] $action,
[boolean]$force = $false
)
BEGIN {
# translate action to numeric value required by the method
switch($action) {
"Restart"
{
$_action="/?originalUrl=https%3A%2F%2Friptutorial.com%2F2%2520%2520%2520%2520%2520%2520%2520%2520break%2520%2520%2520%2520%257D%2520%2520%2520%2520%26quot%3BLogOff%26quot%3B%2520%2520%2520%2520%257B%2520%2520%2520%2520%2520%2520%2520%2520%24_action%2520%3D%25200%2520%2520%2520%2520%2520%2520%2520%2520break%2520%2520%2520%2520%257D%2520%2520%2520%2520%26quot%3BShutdown%26quot%3B%2520%2520%2520%2520%257B%2520%2520%2520%2520%2520%2520%2520%2520%24_action%2520%3D%25202%2520%2520%2520%2520%2520%2520%2520%2520break%2520%2520%2520%2520%257D%2520%2520%2520%2520%26quot%3BPowerOff%26quot%3B%2520%2520%2520%2520%257B%2520%2520%2520%2520%2520%2520%2520%2520%24_action%2520%3D%25208%2520%2520%2520%2520%2520%2520%2520%2520break%2520%2520%2520%2520%257D%257D%23%2520to%2520force%2C%2520add%25204%2520to%2520the%2520valueif(%24force)%2520%7B%2520%2520%2520%2520%24_action%2520%2B%3D%25204%7Dwrite-verbose%2520%26quot%3BAction%2520set%2520to%2520%24action%26quot%3B%7DPROCESS%2520%7B%2520%2520%2520%2520write-verbose%2520%26quot%3BAttempting%2520to%2520connect%2520to%2520%24computername%26quot%3B%2520%2520%2520%2520%23%2520this%2520is%2520how%2520we%2520support%2520-whatif%2520and%2520-confirm%2520%2520%2520%2520%23%2520which%2520are%2520enabled%2520by%2520the%2520SupportsShouldProcess%2520%2520%2520%2520%23%2520parameter%2520in%2520the%2520cmdlet%2520bindnig%2520%2520%2520%2520if(%24pscmdlet.ShouldProcess(%24computername))%2520%7B%2520%2520%2520%2520%2520%2520%2520%2520get-wmiobject%2520win32_operatingsystem%2520-computername%2520%24computername%2520%7C%2520invoke-wmimethod%2520-name%2520Win32Shutdown%2520-argumentlist%2520%24_action%2520%2520%2520%2520%7D%7D%7D%2520%23Usage%3A%23This%2520will%2520only%2520output%2520a%2520description%2520of%2520the%2520actions%2520that%2520this%2520command%2520would%2520execute%2520if%2520-WhatIf%2520is%2520removed."localhost','server1'| Restart-Win32Computer -action LogOff -whatif
#This will request the permission of the caller to continue with this item.
#Attention: in this example you will get two confirmation request because all cmdlets called by this cmdlet that also support ShouldProcess, will ask for their own confirmations...
'localhost','server1'| Restart-Win32Computer -action LogOff -Confirm