在Cmdlet中,如何检测Debug标志是否已设置?
我正在编写PowerShell Cmdlet并使用WriteDebug ,但我想编写一个需要额外API调用的对象,而且在关闭调试时我宁愿不进行调用。 如何检测Debug标志是否已设置,以便我可以完全跳过对WriteDebug的调用?
例如,我的WriteDebug调用将如下所示:
WriteDebug(string.Format("Name : {0}", api.GetName(myobj)));
在该示例中,我想避免调用api.GetName
除非打开调试。
试试这个:
$Debug = $psboundparameters.debug.ispresent if ($Debug){ Write-Debug(string.Format("Name : {0}", api.GetName(myobj)) }
要从C#访问Debug标志,你应该可以做与mjolinor建议的基本相同的事情:
if (this.MyInvocation.BoundParameters.ContainsKey("Debug")) { ... do something ... }
但请注意,您可以使用值false指定debug参数:
MyCmdlet -Debug:$false
要处理这种情况,您可能希望添加类似的内容(从PSCmdlet中):
bool debug = MyInvocation.BoundParameters.ContainsKey("Debug") && ((SwitchParameter)MyInvocation.BoundParameters["Debug"]).ToBool();
在C#中,BoundParameters是一个字典,所以你也可以使用TryGetValue(),但请注意,switch(如Debug)的值是SwitchParameter而不是bool。
有关更多信息,请参阅以下内容
- PSCmdlet.MyInvocation: http : //msdn.microsoft.com/en-us/library/system.management.automation.pscmdlet.myinvocation( v=vs.85) .aspx
- BoundParameters: http : //msdn.microsoft.com/en-us/library/system.management.automation.invocationinfo.boundparameters( v=vs.85) .aspx
- SwitchParameter: http : //msdn.microsoft.com/en-us/library/windows/desktop/system.management.automation.switchparameter( v= vs.85).aspx
实验表明您可能想要查看$ DebugPreference变量。 我尝试使用高级函数,但它可能在cmdlet中也可以正常工作。
尝试以下命令序列:
function f { [cmdletbinding()]Param() $DebugPreference } function g { [cmdletbinding()]Param() f } f f -Debug g g -Debug
只是检查-Debug标志是否已设置可以使用下面的代码完成,但这还不够。
bool debug = false; bool containsDebug = MyInvocation.BoundParameters.ContainsKey("Debug"); if (containsDebug) debug = ((SwitchParameter)MyInvocation.BoundParameters["Debug"]).ToBool();
PowerShell还允许您设置一个名为$ DebugPreference的全局标志,上面的代码不会检查它。 从另一个cmdlet调用cmdlet会inheritance这些公共参数,这些参数不会通过Debug标志inheritance,因为上面的代码会检查。 下面的代码将检查$ DebugPreference并解决这些问题。
debug = (ActionPreference)GetVariableValue("DebugPreference") != ActionPreference.SilentlyContinue;
不幸的是,与PowerShell中的cmdlet相反,您必须同时检查两者。 所以最终的C#代码如下。 我还添加了Verbose通用参数的代码作为奖励。 一定要inheritancePSCmdlet而不是Cmdlet来获取GetVariableValue方法。
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;