OutOfMemory Exception on remote execution using Powershell Invoke-Command
Asked Answered
M

3

11

I am trying to execute an exe on a remote computer using invoke-command. Executing the exe on the remote machine after logging into the machine using remote desktop takes 1GB of memory and executes to completion after a minute. Whereas when I execute the same exe using Invoke-Command on the same machine, the process returns an OutOfMemoryException and ends suddenly. My invoke command is as simple as Invoke-Command -Session $someSessionVariable -ScriptBlock {Invoke-Expression "abc.exe --arg arg"} -AsJob.

Am I missing something regarding the restrictions on remote invocation?

Thanks in Advance.

Margravine answered 12/3, 2012 at 11:11 Comment(7)
blog.patricknielsen.net/2012/01/…Inclinatory
Why do you even use iex for calling a native program? PowerShell is a shell. Executing commands is what it does.Amphiboly
@Amphiboly - while that is true, powershell has so MANY problems with passing the arguments to an external program that it is at times easier to just use iexHexarchy
But iex ist just a layer above all that problems, at least how I understand it. You just add a layer of indirection while solving no problem at all.Amphiboly
Thanks David, the suggestion helped.Margravine
any final solution about it ?Dybbuk
@DavidBrabant that was the solution for me! Thanks very much!!!Domoniquedomph
P
11

Complete PowerShell script, based on mjolinor's answer, for anyone who wants to skip the reasons and just make it work:

Set-Item WSMan:\localhost\Shell\MaxMemoryPerShellMB 1000000
Set-Item WSMan:\localhost\Plugin\Microsoft.PowerShell\Quotas\MaxMemoryPerShellMB 1000000
Restart-Service WinRM
Persuade answered 24/2, 2016 at 13:8 Comment(0)
L
7

From:

http://msdn.microsoft.com/en-us/library/windows/desktop/aa384372(v=vs.85).aspx

The defult memory limit on remote shells is 150MB

MaxMemoryPerShellMB Specifies the maximum amount of memory allocated per shell, including the shell's child processes. The default is 150 MB.

Lyckman answered 12/3, 2012 at 13:2 Comment(2)
Thanks for your reply. The page has many more default parameters that one using winrm has to checkout.Margravine
Using remote/background jobs means you're going to be dealing with constrained runspaces and deserialized objects being returned. What works in a local/unconstrained runspace may need to be re-factored to work in that enviroment.Lyckman
E
0

As an addition to mjolinor's answer, you can change the MaxMemoryPerShellMB by running:

sl WSMan:\localhost\Shell
Set-Item MaxMemoryPerShellMB 300

Ref: http://blogs.technet.com/b/heyscriptingguy/archive/2013/07/30/learn-how-to-configure-powershell-memory.aspx

Ettaettari answered 13/11, 2014 at 18:25 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.