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

前端 未结 8 686
庸人自扰
庸人自扰 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:59

    The best way to do it is by setting the $VerbosePreference. This will enable the verbose level for the entire script. Do not forget to disable it by the end of the script.

    Function test
    {
        [CmdletBinding()]
        param($param1)
    
        if ($psBoundParameters['verbose'])
        {
            $VerbosePreference = "Continue"
            Write-Verbose " Verbose mode is on"
        }
        else
        {
            $VerbosePreference = "SilentlyContinue"
            Write-Verbose " Verbose mode is Off"
        }
    
    
        # 
    
    }
    

提交回复
热议问题