none
How to increase the Powershell's memory? RRS feed

  • Question

  • In asp.net MVC web app, I was trying to connect the remote Exchange server through powershell , these are the codes:

     ...

    runspace = RunspaceFactory.CreateRunspace(connectionInfo);

    try {

    runspace.Open(); // the error comes from this line

    ....

    Connecting to remote server ProdServer.ca failed with the following error message :
    <f:WSManFault xmlns:f="http://schemas.microsoft.com/wbem/wsman/1/wsmanfault" Code="3762507597"
    Machine="ProdServer.CA"><f:Message><f:ProviderFault provider="PowerShellplugin"
    path="%windir%\system32\pwrshplugin.dll"></f:ProviderFault></f:Message></f:WSManFault>
    For more information, see the about_Remote_Troubleshooting Help topic.

    After googling , it seems it caused there is no enough powershell memory, is this related to my local machine (localhost) who is running this C# codes or it is remote server computer? and how to increase the memory space?

    I tried this codes :  winrm setwinrm/config/winrs `@`{MaxMemoryPerShellMB=`"512`"`} , but it is doesnot work.

    Is this related to Powershell version? does different powershell has different command to increase the space?

    I am using Windows 10 with Visual Studio 2017 Enterprise version.   

    thanks

    Sunday, October 13, 2019 3:48 PM

All replies

  • Since you issue is with Visual Studio you will need to ask this question in VS. Remember that compiled code places greatere restrictions on the components you load. The issue you are having is related to the VS code and the web server and not to PowerShell. WsMan is reporting the error but the error is caused by your C# VS project.

    Be sure you can run PowerShell as a script on your system before trying to use it from VS,  If it works executing the same script commands then the issue is with your project and C# code.

    By default web servers are memory restricted and are restricted from accessing local resources and the local network so this could be the cause of your issue.

    I highly doubt that yo are getting the error on the "runspace.Open()" due to PowerShell.  It is either a badly coded "conectionInfo" or some other line that you are having an issue with. 

    Use the following example to create a remote runspace but do it in a simple C# console app.  When you can get that to work then copy the code to a new simple MVC app.  If it fails then the issue is with your web server.

    If you are not a well experienced programmer then you will learn a lot from this exercise and you will begin to see how web apps are different desktop apps as they are running in a more restrictive environment.

    Finally, for what you are doing, the memory error is not likely caused by the winrm subsystem.  It is an  error induced by the calling code.   Test remoting from PowerShell to validate that you PS remoting system is working correctly.  If it is then it is the web code and configuration that are causing this.

    If none of this gets you moving forward then post your questions in the "ASP.Net' forums. http://forums.asp.net where all MS IIS issues are handled.  They will have the experienced coders who can help you figure this out.


    \_(ツ)_/

    Sunday, October 13, 2019 4:11 PM
    Moderator
  • I should also note that you need to be sure you have the correct reference assemblies specified in your project.

    See: https://developer.microsoft.com/en-us/windows/downloads/sdk-archive


    \_(ツ)_/


    Sunday, October 13, 2019 4:30 PM
    Moderator
  • Hi Peter,

    As a first step : you should test WSMAN remotly

    Test-WSMan -ComputerName YourRemoteComputer
    

    The Answer should be like the following

    Wsmid           : http://schemas.dmtf.org/wbem/wsman/identity/1/wsmanidentity.xsd

    ProtocolVersion : http://schemas.dmtf.org/wbem/wsman/1/wsman.xsd

    ProductVendor   : Microsoft Corporation

    ProductVersion  : OS: 0.0.0 SP: 0.0 Stack: 3.0


    If it's not good, but locally you have check that WSMan is properly set up.
    Have you thought about the firewall ?

    Get-NetFirewallRule -DisplayGroup "Remote Management*"|

    Select-Object Name, DisplayName, DisplayGroup, Enabled, Direction |

    Sort-Object displayGroup -Unique

    Now if you are sure that the pb is just a question of memory for PS.

    Do the following (in run as admin)

    Get-ChildItem WSMan:\localhost
    Get-ChildItem WSMan:\localhost\shell
    Check that the AllowRemoteShellAccess is set up to true

    Check that the AllowRemoteAccess is set up to truea. And you have another interresting value MaxMemoryPerShellMB

    Yes ! Now, you can set up this value

    Set-Item -Path WSMan:\localhost\Shell\MaxProcessesPerShell -Value "Your value"

    Now it's done.
    - Is this related to Powershell version? does different powershell has different command to increase the space?

    You use a legacy DOS cmd. Probably a syntax error or not enough privileges. Avoid to use legacy DOS cmd, use Powershell cmdlet (more writable, more readable, you manipulate objects (with properties and methods) and not text, ...)

    - I am using Windows 10 with Visual Studio 2017 Enterprise version.

    Wisual Studio 2018 requires .Net >4.6.x (installed with the producti if missing) , then you have the latest Windows Powershell version 5.1.

    Regards

    Oliv


    Sunday, October 13, 2019 4:47 PM
  • Please note that max memory on Windows 10 is already set to the maximum:

    MaxMemoryPerShellMB = 2147483647

    Which is about 2Gb.  There is no need to set this up on Windows 10.

    There is no need to check Net F4ramwework on WIn110 as WMF 5.1 comes with all of the required Net assemblies to support remoting and PowerShell.

    The major issues are:

    1 Is WinRM working from PowerShell?
    2.  Are you using the correct reference assembly (3.0)
    3.  Is the web server correctly configured for PowerShell remoting. (it is not by default).


    \_(ツ)_/

    Sunday, October 13, 2019 5:10 PM
    Moderator
  • Hi JRV,

    I see that the same conclusions : the problem may be WSMan Remote Shell (enabled) and not necessarily a problem of memory value for PS.

    I'm thinking that he has PS 5.1 (I've the reprequisite for Visual Studio 2107, and it's .Net 4.6.x)

    Regards

    Olivier

    Sunday, October 13, 2019 5:19 PM
  • How are you authenticating to the remote Exchange server? Do you specify credentials when you call Powershell? Do you have the IIS worker process (or the IUSR) configured to run as an admin account? Are you impersonating the end user if you have the site set for integrated authentication?  

    Sunday, October 13, 2019 6:19 PM
  • W10 comes with 5.1. VS 2017/8 do not require any version of PS and can use any version of the Net Framework. It is chosen based on what is installed and not on anything t do with the project.

    Of course remoting must be enabled and configure. If the error is out-of-memory then WsMan is enabled.  The error is likely due to the C# programming and trying to run the PS host under aa web server which is always a challenge.  WsMan/WinRM are not configured for use in IIS and IIS is not configured to use remoting.  Web security makes this difficult.


    \_(ツ)_/

    Sunday, October 13, 2019 9:34 PM
    Moderator
  • How are you authenticating to the remote Exchange server? Do you specify credentials when you call Powershell? Do you have the IIS worker process (or the IUSR) configured to run as an admin account? Are you impersonating the end user if you have the site set for integrated authentication?  

    Which comes down to an issue for the aSP.net site and the need to see the "ConnectionInfo" object. which establishes the connection parameters and context.


    \_(ツ)_/

    Sunday, October 13, 2019 9:37 PM
    Moderator
  • How are you authenticating to the remote Exchange server? Do you specify credentials when you call Powershell? Do you have the IIS worker process (or the IUSR) configured to run as an admin account? Are you impersonating the end user if you have the site set for integrated authentication?  

    HI Olivier, I have user ID and Password in the web.config file of mvc web app to be used to connect exchange server remotely. the connection seems OK, by the way, I have an existing legacy VB.net web application running well, I just convert it to mvc C# codes, so the C# codes are almost  identical with the existing VB codes. If you need, I can post the source codes to you tomorrow when I back to office, thanks again,
    Monday, October 14, 2019 3:12 PM
  • How are you authenticating to the remote Exchange server? Do you specify credentials when you call Powershell? Do you have the IIS worker process (or the IUSR) configured to run as an admin account? Are you impersonating the end user if you have the site set for integrated authentication?  

    Which comes down to an issue for the aSP.net site and the need to see the "ConnectionInfo" object. which establishes the connection parameters and context.


    \_(ツ)_/

    Hi jrv, thanks a lot for your help, I need more details how to configure IIS in my local.

    There is an existing VB.net web project which running well, I just convert it from VB to C# line by line, I can run the existing VB project from my visual studio locally, so I assume that I don't need to do any local set up (IIS), the converted C# codes has no compile errors at all, if you need, I can post the C# codes the VB codes tomorrow when I back to my office. thanks again,

    Monday, October 14, 2019 3:19 PM
  • Please note that max memory on Windows 10 is already set to the maximum:

    MaxMemoryPerShellMB = 2147483647

    Which is about 2Gb.  There is no need to set this up on Windows 10.

    There is no need to check Net F4ramwework on WIn110 as WMF 5.1 comes with all of the required Net assemblies to support remoting and PowerShell.

    The major issues are:

    1 Is WinRM working from PowerShell?
    2.  Are you using the correct reference assembly (3.0)
    3.  Is the web server correctly configured for PowerShell remoting. (it is not by default).


    \_(ツ)_/

    There is an existing VB.net web project which running well, I just convert it from VB to C# line by line, I can run the existing VB project from my visual studio locally, so I assume that I don't need to do any local set up (IIS), the converted C# codes has no compile errors at all, if you need, I can post the C# codes the VB codes tomorrow when I back to my office. thanks again,


    Monday, October 14, 2019 3:20 PM
  • Please - for IIS and ASP.Net issues post in the ASP.NET forum. This forum is NOT for web applications and not for coding assistance.

    http://forums.asp.net

    http://forums.iis.net

    These are the official Microsoft forums for these products and subsystems.


    \_(ツ)_/

    Monday, October 14, 2019 3:44 PM
    Moderator
  • Hi Peter,

    As a first step : you should test WSMAN remotly

    Test-WSMan -ComputerName YourRemoteComputer

    The Answer should be like the following

    Wsmid           : http://schemas.dmtf.org/wbem/wsman/identity/1/wsmanidentity.xsd

    ProtocolVersion : http://schemas.dmtf.org/wbem/wsman/1/wsman.xsd

    ProductVendor   : Microsoft Corporation

    ProductVersion  : OS: 0.0.0 SP: 0.0 Stack: 3.0

    I have almost exactly same result, the only difference is: "Stack: 2.0", but yours is "Stack: 3.0"

    If it's not good, but locally you have check that WSMan is properly set up.
    Have you thought about the firewall ?

    I copied your following codes and tried it in powershell, nothing happened and no errors, I assume the firewall is OK. 

    Get-NetFirewallRule -DisplayGroup "Remote Management*"|

    Select-Object Name, DisplayName, DisplayGroup, Enabled, Direction |

    Sort-Object displayGroup -Unique

    Now if you are sure that the pb is just a question of memory for PS.

    Do the following (in run as admin)

    Get-ChildItem WSMan:\localhost

    Ran above command,I got this errors:

    Get-ChildItem : Cannot find path 'localhost' because it does not exist.
    At line:1 char:1
    + Get-ChildItem WSMan:\localhost
    + ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
        + CategoryInfo          : ObjectNotFound: (localhost:String) [Get-ChildItem], ItemNotFoundException
        + FullyQualifiedErrorId : PathNotFound,Microsoft.PowerShell.Commands.GetChildItemCommand

    Get-ChildItem WSMan:\localhost\shell

    //I got same error as above posted, Check that the AllowRemoteShellAccess is set up to true

    Check that the AllowRemoteAccess is set up to truea. And you have another interresting value MaxMemoryPerShellMB

    Yes ! Now, you can set up this value

    Set-Item -Path WSMan:\localhost\Shell\MaxProcessesPerShell -Value "Your value"

    Now it's done.
    - Is this related to Powershell version? does different powershell has different command to increase the space?

    You use a legacy DOS cmd. Probably a syntax error or not enough privileges. Avoid to use legacy DOS cmd, use Powershell cmdlet (more writable, more readable, you manipulate objects (with properties and methods) and not text, ...)

    - I am using Windows 10 with Visual Studio 2017 Enterprise version.

    Wisual Studio 2018 requires .Net >4.6.x (installed with the producti if missing) , then you have the latest Windows Powershell version 5.1.

    Regards

    Oliv


    Hi Oliv, Please see my response with codes above in italic text, and let me know if you have any idea, thanks
    Tuesday, October 15, 2019 3:31 PM
  • Please note that max memory on Windows 10 is already set to the maximum:

    MaxMemoryPerShellMB = 2147483647

    Which is about 2Gb.  There is no need to set this up on Windows 10.

    There is no need to check Net F4ramwework on WIn110 as WMF 5.1 comes with all of the required Net assemblies to support remoting and PowerShell.

    The major issues are:

    1 Is WinRM working from PowerShell?
    2.  Are you using the correct reference assembly (3.0)
    3.  Is the web server correctly configured for PowerShell remoting. (it is not by default).


    \_(ツ)_/

    There is an existing VB.net web project which running well, I just convert it from VB to C# line by line, I can run the existing VB project from my visual studio locally, so I assume that I don't need to do any local set up (IIS), the converted C# codes has no compile errors at all, if you need, I can post the C# codes the VB codes tomorrow when I back to my office. thanks again,


    VB.Net is not IIS - projects that are MVC are ASP.Net in either or mixed language.  VB.Net and C# examples won't work under MVC without reconfiguring the IIS site and adjusting all security and remoting configs that are required to allow PS remoting to work.  Without some basic understanding of how these projects and apps work this will be pretty much impossible for you to resolve.  You also did not answer any of my questions.  If you can answer all three questions you will be able to understand what the issues are.

    \_(ツ)_/

    Tuesday, October 15, 2019 3:41 PM
    Moderator
  • Added, I just added this line of code:

    var IsOpened = runspace.RunspaceStateInfo.State == RunspaceState.Opened;

    the IsOpened is "false" in debug mode, any idea for this result?

    Tuesday, October 15, 2019 3:55 PM
  • Please note that max memory on Windows 10 is already set to the maximum:

    MaxMemoryPerShellMB = 2147483647

    Which is about 2Gb.  There is no need to set this up on Windows 10.

    There is no need to check Net F4ramwework on WIn110 as WMF 5.1 comes with all of the required Net assemblies to support remoting and PowerShell.

    The major issues are:

    1 Is WinRM working from PowerShell?
    2.  Are you using the correct reference assembly (3.0)
    3.  Is the web server correctly configured for PowerShell remoting. (it is not by default).


    \_(ツ)_/

    Hi jrv, I just added this code to test: var IsOpened = runspace.RunspaceStateInfo.State == RunspaceState.Opened;

    the IsOpened is false in debug mode, any idea?


    Tuesday, October 15, 2019 3:56 PM
  • Please post questions about code in the ASP.Net forum. This iis not an ASP.Net forum. We cannot help you.


    \_(ツ)_/

    • Proposed as answer by Mashkig Friday, October 18, 2019 12:57 PM
    • Unproposed as answer by Mashkig Friday, October 18, 2019 12:58 PM
    Tuesday, October 15, 2019 4:01 PM
    Moderator
  • Hi jrv,

    Thanks a lot for your help and all other experts in this thread, much appreciated for all responses, which gives me big hints and help,

    Finally I resolved the issue from Visual Studio, yes, it has nothing to do with powershell,

    Here is what I did:

    1. EntityFrameWork needs to upgrade to 6.3, in order to make System.Management.Automation (3.0) works

    2. The controller has to inherit from Controller, NOT "BaseController",

    Again, thanks all

    • Proposed as answer by Mashkig Friday, October 18, 2019 1:01 PM
    Friday, October 18, 2019 1:01 PM
  • Hi,

    Was your issue resolved?

    If you resolved it using our solution, please "mark it as answer" to help other community members find the helpful reply quickly.

    If you resolve it using your own solution, please share your experience and solution here. It will be very beneficial for other community members who have similar questions.

    If no, please reply and tell us the current situation in order to provide further help.

    Best Regards,

    Lee

    Please remember to mark the replies as answers if they help.
    If you have feedback for TechNet Subscriber Support, contact tnmff@microsoft.com.

    Friday, November 8, 2019 2:00 PM
    Moderator