SupportsShouldProcess=$true
27. Juni 2009
Mit der neuen Windows Powershell Version 2.0 kann man sehr einfach die Standardparameter -whatif und -confirm in eigenen Funktionen benutzen:
|
001
002 003 004 005 006 007 008 009 010 |
function do-something
{ [CmdletBinding(supportsShouldProcess=$true)] param($p) if ($pscmdlet.shouldProcess("$p", "do-something")) |
Mit der Deklaration in Zeile 3 akzeptiert die Funktion automatisch –whatif und –confirm:
PS> do-something "dangerous" -whatif
What if: Performing operation "do-something" on Target "dangerous".
PS> do-something "dangerous" -confirm
Confirm
Are you sure you want to perform this action?
Performing operation "do-something" on Target "dangerous".
[Y] Yes [A] Yes to All [N] No [L] No to All [S] Suspend [?] Help (default is "Y"): y
do-something dangerous
Sehr praktisch.