2 ms·
In the PowerShell world: "-WhatIf" is your friend. To implement this in your own scripts is not difficult at all. The template snippet below automatically wire
by jiggawatts 5y ago
In the PowerShell world: "-WhatIf" is your friend.
To implement this in your own scripts is not difficult at all. The template snippet below automatically wires up "-Confirm" and "-WhatIf" support:
[CmdletBinding(ConfirmImpact='High',SupportsShouldProcess=$true)]
PARAM(
[parameter(ValueFromPipeline=$true,Mandatory=$true)]
$TheThingToProcess
)
PROCESS {
if ( $PSCmdlet.ShouldProcess( $TheThingToProcess, 'Do scary thing' )) {
# Scary step
}
}
That even wires up the full "[Y] Yes [A] Yes to All [N] No [L] No to All [S] Suspend" logic, so you don't have to track boolean variables all over the place.
It also passes through that preference to commands invoked witin the script, so things like "Format-Volume" will do nothing if you invoke the outer script with "-WhatIf".