title: Increasing stack size with WinRM for ScriptMethods
body: <p>We are currently refactoring our administration scripts.
  It had just appeared that a combination of WinRM, error handling and ScriptMethod dramatically decreases available recursion depth.</p>
  
  <p>See the following example:</p>
  
  <pre><code>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
      }
  }
  </code></pre>
  
  <p>Just <strong>six</strong> nested calls are enough to overflow the call stack!
  Indeed, more than 200 local nested calls work fine, and without the try-catch the available depth doubles. Regular functions are also not that limited in recursion.</p>
  
  <p><em>Note: I used recursion only to reproduce the problem, the real code contains many different functions on different objects in different modules. So trivial optimizations as "use functions not ScriptMethod" require architectural changes</em></p>
  
  <p>Is there a way to increase the available stack size? (I have an administrative account.)</p>