Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>So not only do we have to take the common parameters -Debug and -Verbose of the cmdlet into account, but also the global $DebugPreference and $VerbosePreference flags and the fact that these common parameters are inherited if a cmdlet is called from another cmdlet.</p> <p>It can be done without hacking around in the internals.</p> <p><a href="https://stackoverflow.com/a/9916709/1242">This answer</a> shows you it can be done with little problems in a PowerShell cmdlet.</p> <pre><code>function f { [cmdletbinding()]Param() $debug = $DebugPreference -ne 'SilentlyContinue' $verbose = $VerbosePreference -ne 'SilentlyContinue' "f is called" " `$debug = $debug" " `$verbose = $verbose" } function g { [cmdletbinding()]Param() "g is called" f } f f -Debug -Verbose g g -Debug -Verbose </code></pre> <p>From C# we have to check both these global flags, but also the common parameters. Be sure to inherit from PSCmdlet instead of Cmdlet to get to the GetVariableValue method.</p> <pre><code>bool debug = false; bool containsDebug = MyInvocation.BoundParameters.ContainsKey("Debug"); if (containsDebug) debug = ((SwitchParameter)MyInvocation.BoundParameters["Debug"]).ToBool(); else debug = (ActionPreference)GetVariableValue("DebugPreference") != ActionPreference.SilentlyContinue; bool verbose = false; bool containsVerbose = MyInvocation.BoundParameters.ContainsKey("Verbose"); if (containsVerbose) verbose = ((SwitchParameter)MyInvocation.BoundParameters["Verbose"]).ToBool(); else verbose = (ActionPreference)GetVariableValue("VerbosePreference") != ActionPreference.SilentlyContinue; </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload