Advanced Functions Support -confirm
January 6, 2009 at 2:01 PM
I was using the "Module" Module to generate some scripts and started looking at SupportsShouldProcess , a parameter the CmdletBinding attribute.
Turns out with V2 it is very simple to implement some sanity checking in the functions we write so people don't go and blow their pinky tow off with a shotgun. So the trick is a simple IF statement in the PROCESS Block of your script or function.
There is a variable called $PSCmdlet that you can use to determine if the user entered the -confirm or -whatif switch. The first parameter in the ShouldProcess method is the target and the second parameter is the operation. Note that if the operation is not specified, it will default to the name of the function.
Here I have a parameter called $test and if I type -confirm it will ask me if I want to "Destroy Everything" on target "Whatever the value of $test is"
The ISE gives a nice Windows dialog box with the options when I type "Get-Something "That you can see" -confirm