In a Cmdlet, how can I detect if the Debug flag is set?

前端 未结 4 454
暗喜
暗喜 2020-12-19 13:09

I\'m writing a PowerShell Cmdlet and using WriteDebug, but I want to write an object which requires an extra API call, and I\'d rather not make that call when debugging is t

4条回答
  •  慢半拍i
    慢半拍i (楼主)
    2020-12-19 13:23

    Experimentation indicates you might want to look at the $DebugPreference variable. I tried it with advanced functions, but it probably works just as well in a cmdlet.

    Try the following sequence of commands:

    function f { [cmdletbinding()]Param() $DebugPreference }
    function g { [cmdletbinding()]Param() f }
    f
    f -Debug
    g
    g -Debug
    

提交回复
热议问题