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

前端 未结 8 688
庸人自扰
庸人自扰 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条回答
  •  猫巷女王i
    2020-12-13 05:08

    There is no need. PowerShell already does this as the code below proves.

    function f { [cmdletbinding()]Param()    
        "f is called"
        Write-Debug Debug
        Write-Verbose Verbose
    }
    function g { [cmdletbinding()]Param() 
        "g is called"
        f 
    }
    g -Debug -Verbose
    

    The output is

    g is called
    f is called
    DEBUG: Debug
    VERBOSE: Verbose
    

    It is not done as direct as passing -Debug to the next cmdlet though. It is done through the $DebugPreference and $VerbrosePreference variables. Write-Debug and Write-Verbose act like you would expect, but if you want to do something different with debug or verbose you can read here how to check for yourself.

提交回复
热议问题