小编Pav*_*rov的帖子

使用WinRM for ScriptMethods增加堆栈大小

我们目前正在重构我们的管理脚本.刚刚出现的WinRM,错误处理和ScriptMethod的组合大大降低了可用的递归深度.

请参阅以下示例:

Invoke-Command -ComputerName . -ScriptBlock {
    $object = New-Object psobject
    $object | Add-Member ScriptMethod foo {
        param($depth)
        if ($depth -eq 0) {
            throw "error"
        }
        else {
            $this.foo($depth - 1)
        }
    }

    try {
        $object.foo(5) # Works fine, the error gets caught
    } catch {
        Write-Host $_.Exception
    }

    try {
        $object.foo(6) # Failure due to call stack overflow
    } catch {
        Write-Host $_.Exception
    }
}
Run Code Online (Sandbox Code Playgroud)

只有六个嵌套调用足以溢出调用堆栈!实际上,超过200个本地嵌套调用工作正常,没有try-catch,可用深度加倍.常规函数也不限于递归.

注意:我只使用递归来重现问题,实际代码在不同模块中的不同对象上包含许多不同的函数.因此,"使用函数而不是ScriptMethod"这些微不足道的优化需要进行体系结构更改

有没有办法增加可用的堆栈大小?(我有一个管理帐户.)

powershell winrm

7
推荐指数
1
解决办法
288
查看次数

标签 统计

powershell ×1

winrm ×1