Cmdlet binding attribute

We already saw that the cmdlet binding attribute will enable all common parameters for you. But there is much more to it.

One of the more popular settings concerns risk mitigation, by enabling the parameter WhatIf parameter to simulate a change and Confirm to confirm a change:

function withbinding
{
[CmdletBinding(SupportsShouldProcess,ConfirmImpact='High')]
param
( )

if ($PSCmdlet.ShouldProcess('Target object','Action'))
{
Write-Host 'User answered yes in confirmation'
}
}
withbinding -WhatIf
withbinding
withbinding -Confirm:$false

In order to make good use of these parameters, you should pay attention to the ConfirmImpact property. It correlates with the built-in $ConfirmPreference variable. If the confirm impact is less than the confirm preference, no automatic confirmation will be displayed. The parameter can still be used.

The cmdlet binding attribute also controls the positional parameter binding, which is enabled by default. In order to disable it, the PositionalBinding  property can be set to $false. If you are using parameter sets, you can specify the default parameter set name in the cmdlet binding attribute, as well.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.144.161.116