如何做一个cmdlet的知道什么时候真的应该叫WriteVerbose(),
WriteDebug()等?
也许我想念一些简单的东西,但我找不到答案.到目前为止我见过的所有cmdlet实现都WriteVerbose()没有任何犹豫.我知道这样做是正确的,但它没有效果.
当详细模式关闭但cmdlet仍然为WriteVerbose()调用准备数据时,性能会受到影响,即无需任何操作.
换句话说,在cmdlet中我希望能够:
if (<VerboseMode>)
{
.... data preparation, sometimes expensive ...
WriteVerbose(...);
}
Run Code Online (Sandbox Code Playgroud)
但我不知道怎么做到这一点if (<VerboseMode>).有任何想法吗?
结论: @ stej的答案显示了如何在理论上获得所需的信息.在实践中,这是hacky并且不太可能合适.因此,如果cmdlet产生非常昂贵的详细或调试输出,那么引入指定详细级别的附加参数似乎是合理的.
这是方法System.Management.Automation.MshCommandRuntime.
internal void WriteVerbose(VerboseRecord record)
{
if ((this.Host == null) || (this.Host.UI == null))
{
tracer.TraceError("No host in CommandBase.WriteVerbose()", new object[0]);
throw tracer.NewInvalidOperationException();
}
ActionPreference verbosePreference = this.VerbosePreference;
if (this.WriteHelper_ShouldWrite(verbosePreference, this.lastVerboseContinueStatus))
{
if (record.InvocationInfo == null)
{
record.SetInvocationInfo(this.MyInvocation);
}
this.CBhost.InternalUI.WriteVerboseRecord(record);
}
this.lastVerboseContinueStatus = this.WriteHelper(null, null, verbosePreference, this.lastVerboseContinueStatus, "VerbosePreference");
}
Run Code Online (Sandbox Code Playgroud)
MshCommandRuntime实现ICommandRuntime对冗长度一无所知的接口:| (通过反射器找到).MshCommandRuntime应该在Cmdlet(public ICommandRuntime CommandRuntime { get; set; })中提供实例.
所以应该可以将属性转换CommandRuntime为MshCommandRuntime并检查详细程度.无论如何,这真的很难看.
我完全同意应该有一个简单的方法来找到它.除此之外(做梦)编译器应该足够聪明,不要在这样的情况下评估一些字符串:
$DebugPreference = 'SilentlyContinue'
$array = 1..1000
Write-Debug "my array is $array"
Run Code Online (Sandbox Code Playgroud)
输入Write-Debug将永远不会被使用,所以$array不应该在传递的字符串中进行评估..(可以测试它是否真的像这样评估:Write-Debug "my array is $($array|%{write-host $_; $_})"
怎么样:
BEGIN {
if ($PSCmdlet.MyInvocation.BoundParameters["Debug"].IsPresent) {
$HasDebugFlag = $true
}
if ($PSCmdlet.MyInvocation.BoundParameters["Verbose"].IsPresent) {
$HasVerboseFlag = $true
}
}
PROCESS {
if ($HasVerboseFlag) {
expensive_processing
}
}
Run Code Online (Sandbox Code Playgroud)
警告:仅在 PowerShell 3 上测试。
因此,我们不仅需要考虑cmdlet的公共参数-Debug和-Verbose,还要考虑全局$ DebugPreference和$ VerbosePreference标志,以及如果从另一个cmdlet调用cmdlet,则继承这些公共参数的事实.
它可以在内部没有黑客攻击的情况下完成.
这个答案向您展示了在PowerShell cmdlet中可以解决的问题.
function f { [cmdletbinding()]Param()
$debug = $DebugPreference -ne 'SilentlyContinue'
$verbose = $VerbosePreference -ne 'SilentlyContinue'
"f is called"
" `$debug = $debug"
" `$verbose = $verbose"
}
function g { [cmdletbinding()]Param()
"g is called"
f
}
f
f -Debug -Verbose
g
g -Debug -Verbose
Run Code Online (Sandbox Code Playgroud)
从C#开始,我们必须检查这些全局标志,以及常见参数.一定要继承PSCmdlet而不是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;
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
3912 次 |
| 最近记录: |