We are trying to streamline our administration scripts - but suddenly it turned out that the combination of WinRM, error handling and ScriptMethod reduces the available recursion depth dramatically.
An example to demonstrate the problem:
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) # Работает (ошибка поймана) } catch { Write-Host $_.Exception } try { $object.foo(6) # Сбой сценария из-за переполнения глубины вызовов. } catch { Write-Host $_.Exception } } SIX! A total of six nested calls cause stack overflow! This is despite the fact that locally at least 200 nested calls are processed normally, without try-catch, the allowable depth is doubled - and the usual functions in recursion are not so limited.
Is there a way to increase the maximum stack depth? All administrative rights - there is.