locked
C# codes error for launching Powershell remotely to connect Exchange server, help please! RRS feed

  • Question

  • User-1651604128 posted

    Hi ,

    after posting this question in Powershell forum, I was told that I have to post it in this forum. Here is the details.

    I am converting a vb.net web app to MVC C#, these are the codes in VB, basically, it gets the userID and password from webconfig and remote launch powershell in exchange server and then pull the data from exchange server, but I got the error in runspace.Open().

    Dim newCred As PSCredential
            Dim connectionInfo As WSManConnectionInfo
            Dim runspace As Runspace
            Dim powershell As PowerShell

    Dim sUser = System.Configuration.ConfigurationManager.AppSettings.Get("ProdUser").ToString  //here the ProdUser = "Prod\Prod_User" in web config file


            Dim sPassword As String = System.Configuration.ConfigurationManager.AppSettings.Get("ProdPassword").ToString

            Dim securePassword As System.Security.SecureString = New System.Security.SecureString()
            Dim c As Char
            For Each c In sPassword.ToCharArray()
                securePassword.AppendChar(c)
            Next

            '-- set credentials
            newCred = New PSCredential(sUser, securePassword)

            '-- set connection info
            connectionInfo = New WSManConnectionInfo(New Uri(System.Configuration.ConfigurationManager.AppSettings.Get("WSManConnectionInfo").ToString), "http://schemas.microsoft.com/powershell/Microsoft.Exchange", newCred)
            connectionInfo.AuthenticationMechanism = AuthenticationMechanism.Kerberos

            '-- create remote runspace
            runspace = RunspaceFactory.CreateRunspace(connectionInfo)
           
            Try
                'open the remote runspace
                runspace.Open() //Got error in here

                powershell = powershell.Create()

    ...........

    The above codes are working file in VB, but after I encountered the error when I convert it to C# MVC app as the following codes:

     PSCredential newCred;
       WSManConnectionInfo connectionInfo;
       Runspace runspace;
       PowerShell powershell;
       PSCommand command;
      
       string sResult = "";
             
       string sUser = System.Configuration.ConfigurationManager.AppSettings.Get("ProdUser").ToString();  //here the ProdUser = "Prod\Prod_User" in web config file

       string sPassword = ConfigurationManager.AppSettings.Get("ProdPassword").ToString();

       SecureString securePassword =  new SecureString();
       //char c;
       foreach (char c in sPassword.ToCharArray())
       {
        securePassword.AppendChar(c);
       }

       //-- set credentials

       newCred = new PSCredential(sUser, securePassword);

                //--set connection info
                connectionInfo = new WSManConnectionInfo(new Uri(ConfigurationManager.AppSettings.Get("WSManConnectionInfo").ToString()), "http://schemas.microsoft.com/powershell/Microsoft.Exchange", newCred);       
                connectionInfo.AuthenticationMechanism = AuthenticationMechanism.Kerberos;     
                //--create remote sunspace        
                runspace = RunspaceFactory.CreateRunspace(connectionInfo);   
                try
       {
                                 runspace.Open(); // In C# code, Here I got the error message below:

        //--create powershell

        powershell = PowerShell.Create();

    //Here is the error message on the above runspace.Open(); line code  at runtime:

    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="servername.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.

    Running Environment:

    Windows 10, Visual Studio 2017 enterprise version, the version of System.Management.Automation.dll is 3.0.

    The same VB codes runs well in the same local computer with the same visual studio 2017.

    Does anybody know what is wrong with my C# codes? Appreciate any help,  

    Edit: I also found this link:

    https://stackoverflow.com/questions/18620440/invoking-powershell-via-c-sharp-in-a-web-application-issues-with-app-pool-iden

    I tried to add the line of code:

    connectionInfo.EnableNetworkAccess = true;

    But I got the same error, any idea?

    Wednesday, October 16, 2019 7:00 PM

Answers

  • User-1651604128 posted

    Finally, I resolved it my self, solution:

    1. EntityFramework needs to upgrade from 6.1 to 6.3

    2. System.Management.Automation needs 3.00

    3. The Controller needs to inherit from Controller, NOT the "BaseController" //this is key

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Friday, October 18, 2019 12:56 PM

All replies

  • User-1651604128 posted

    Any idea? some people said "Just to make sure, do both ends have the same version of PowerShell? ", but what I am wondering the same VB codes is working fine using the same local machine and remote, why this C# codes does not work? if is it is the powershell version problem, then the VB codes should have the same error,

    any idea? thanks

    Thursday, October 17, 2019 12:49 PM
  • User-1651604128 posted

    I just found that I have the exact the same issue with this post, I ran my codes in console app is fine, but I got the exact same error when running in MVC web app. but its solution does not work for my case,

    https://social.technet.microsoft.com/Forums/en-US/12a3f804-563f-42a6-a9de-8149fa92c438/problem-when-opening-a-runspace-in-a-web-application?forum=winserverpowershell

    But since I am running in Windows 10 with Visual Studio 2017, and I checked my winrs as this:

    Winrs
            AllowRemoteShellAccess = true
            IdleTimeout = 7200000
            MaxConcurrentUsers = 2147483647
            MaxShellRunTime = 2147483647
            MaxProcessesPerShell = 2147483647
            MaxMemoryPerShellMB = 2147483647  // so here the memory is much more than 150 mb,
            MaxShellsPerUser = 2147483647

    You can see in my case , the MaxMemoryPerShellMB = 2147483647  , so it is not the memory issue for my case, any other solutions? thanks

    Thursday, October 17, 2019 1:40 PM
  • User-1651604128 posted

    Finally, I resolved it my self, solution:

    1. EntityFramework needs to upgrade from 6.1 to 6.3

    2. System.Management.Automation needs 3.00

    3. The Controller needs to inherit from Controller, NOT the "BaseController" //this is key

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Friday, October 18, 2019 12:56 PM