如何在PowerShell中配置呼叫深度? [英] How can I configure call depth in PowerShell?
问题描述
我在PowerShell中尝试了一些事情,并在某些测试递归函数中将调用深度设置为1000。我在互联网上查看了一些信息,发现这是由于PowerShell中的错误处理(如果我正确的话):
递归深度限制在版本1中是固定的。由于处理异常的方式,深度递归在64位模式下导致问题。这是造成级联的内存不足错误。最终的结果是我们硬性限制了所有平台上的递归深度,以帮助确保脚本可以移植到所有平台。
- PowerShell的共同设计师Bruce Payette
我发现它 here 。
另外,我发现MSDN上的这个异常页面表明这个限制是可配置的(但是我没有找到任何关于如何做到这一点的事情) - 见备注部分 here 。
如何设置此限制?
- 在 PowerShell V1 中,最大呼叫深度为100:
使用。NET Reflector ,我们可以从这个代码片段中看到这个 System.Management.ExecutionContext
类代码,
internal int IncrementScopeDepth()
{
using(IDisposable whatever = tracer.TraceMethod({0},new object [] {this.scopeDepth}))
{
if(this.CurrentPipelineStopping)
{
throw new PipelineStoppedException();
}
this.scopeDepth ++;
if(this.scopeDepth> 100)
{
ScriptCallDepthException exceptionRecord = new
ScriptCallDepthException(this.scopeDepth,100);
tracer.TraceException(exceptionRecord);
throw exceptionRecord;
}
返回this.scopeDepth;
}
}
无法修改
- 在 PowerShell V2 中,最大呼叫深度是1000
再次看看代码时,似乎没有办法围绕默认的最大呼叫深度。 p>
- 在 PowerShell V3(CTP)中,似乎没有最大呼叫深度(除非用完了资源当然)。此行为已被描述为连接错误,所以它可能会在最终版本中更改。
I was just trying things in PowerShell and got an error about call depth being set to 1000 in some test recursive function. I looked on the Internet for some information and found that this is due to error handling in PowerShell (if I got it right):
The recursion depth limit is fixed in version 1. Deep recursion was causing problems in 64-bit mode because of the way exceptions were being processed. It was causing cascading out-of-memory errors. The net result was that we hard-limited the recursion depth on all platforms to help ensure that scripts would be portable to all platforms. - Bruce Payette, co-designer of PowerShell
I found it here.
Also I found this exception page on MSDN that states this limit is configurable (but I didn't find anything about how to do this) - see remarks section here.
How can this limit be set?
- In PowerShell V1 the maximum call depth is 100:
Using .NET Reflector, we can see in this snippet from the System.Management.ExecutionContext
class code,
internal int IncrementScopeDepth()
{
using (IDisposable disposable = tracer.TraceMethod("{0}", new object[] { this.scopeDepth }))
{
if (this.CurrentPipelineStopping)
{
throw new PipelineStoppedException();
}
this.scopeDepth++;
if (this.scopeDepth > 100)
{
ScriptCallDepthException exceptionRecord = new
ScriptCallDepthException(this.scopeDepth, 100);
tracer.TraceException(exceptionRecord);
throw exceptionRecord;
}
return this.scopeDepth;
}
}
that it is not possible to modify the hardcoded 100.
- In PowerShell V2 the maximum call depth is 1000
Again when looking at the code, there doesn't seem to be a way around the default maximum call depth.
- In PowerShell V3 (CTP) there doesn't seem to be a maximum call depth (unless you run out of resources of course). This behaviour has been described as a bug on connect, so it might change in the final version.
这篇关于如何在PowerShell中配置呼叫深度?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!