PoshCode Archive  Artifact [9eef850948]

Artifact 9eef85094805f8e99ff2395940f145df559f397d90dd2b89985a899d8059400b:

  • File Demo-ShouldProcess.ps1 — part of check-in [22701d8556] at 2018-06-10 13:23:25 on branch trunk — Shows how to use SupportsShouldProcess and Confirm (user: Joel Bennett size: 862)

# encoding: ascii
# api: powershell
# title: Demo-ShouldProcess
# description: Shows how to use SupportsShouldProcess and Confirm
# version: 0.1
# author: Joel Bennett
# license: CC0
# x-poshcode-id: 3291
# x-archived: 2016-06-10T15:25:52
# x-published: 2012-03-15T15:50:00
#
#
[CmdletBinding(SupportsShouldProcess=$true, ConfirmImpact="Medium")]
param([Switch]$Force)

$RejectAll = $false;
$ConfirmAll = $false;

foreach($file in ls) {
   if($PSCmdlet.ShouldProcess( "Removed the file '$($file.Name)'",
                               "Remove the file '$($file.Name)'?",
                               "Removing Files" )) {
      if($Force -Or $PSCmdlet.ShouldContinue("Are you REALLY sure you want to remove '$($file.Name)'?", "Removing '$($file.Name)'", [ref]$ConfirmAll, [ref]$RejectAll)) {
         "Removing $File"
      }
   }
}