How to properly use the -verbose and -debug parameters in a custom cmdlet

前端 未结 8 685
庸人自扰
庸人自扰 2020-12-13 04:08

By default, any named function that has the [CmdletBinding()] attribute accepts the -debug and -verbose (and a few others) parameters and has the p

8条回答
  •  别那么骄傲
    2020-12-13 04:52

    You can set the VerbosePreference as a global variable on starting your script and then check for the global variable in your custom cmdlet.

    Script:

    $global:VerbosePreference = $VerbosePreference
    Your-CmdLet
    

    Your-CmdLet:

    if ($global:VerbosePreference -eq 'Continue') {
       # verbose code
    }
    

    Checking explicitly for 'Continue' allows the script to be equal to -verbose:$false when you call the CmdLet from a script that doesn't set the global variable (in which case it's $null)

提交回复
热议问题