How to know whether parameter “whatif” has been passed to a customer cmdlet programmatically?

By : Dreamer
Source: Stackoverflow.com
Question!

I would like to know whether whatif parameter has been passed to a cmdlet.

for ex: remove-mycmdlet -whatif

I tried to do it by finding the value of the variable: WhatIfPreference.

But its always yields false.

Can you please let me know how can i know whether whatif parameter is passed in?

Thank you very much,

Regards, Naresh

By : Dreamer


Answers

When I define function like that:

function Test-WhatIf {
[CmdletBinding(SupportsShouldProcess = $true)]
param (
)

    $WhatIfPreference
    $PSBoundParameters.ContainsKey('WhatIf')
}

I get $false or $true, depending on the addition of -WhatIf to the command. Although this is just advanced function, I would not expect different behavior in compiled command... Are you sure you've implemented SupportsShouldProcess correctly?

By : BartekB


This video can help you solving your question :)
By: admin