2

I am using Exchange server 2013. I am getting below Exception when try to run my C# code in IIS.

System.Management.Automation.Remoting.PSRemotingTransportException: Connecting to remote server exchangeserver.admin.com failed with the following error message : Access is denied. For more information, see the about_Remote_Troubleshooting Help topic.

But locally [in Visual studio] my code is working fine , after publish my code and running through IIS i am getting Exception.

Please see the code

 var runspace = RunspaceFactory.CreateRunspace();

            runspace.Open();

            object psSessionConnection;

            // Create a powershell session for remote exchange server
            using (var powershell = PowerShell.Create())
            {
                var command = new PSCommand();
                command.AddCommand("New-PSSession");
                command.AddParameter("ConfigurationName", "Microsoft.Exchange");
                command.AddParameter("ConnectionUri", new Uri("http://ExchangeServer.admin.com/powershell/Microsoft.Exchange"));
              //  command.AddParameter("Authentication", "Kerberos");
                powershell.Commands = command;
                powershell.Runspace = runspace;

                // TODO: Handle errors
                var result = powershell.Invoke();

                Collection<ErrorRecord> errors = powershell.Streams.Error.ReadAll();
                if (errors != null || errors.Count > 0)
                {
                    foreach (ErrorRecord er in errors)
                    {
                        //log.Error(er.Exception.ToString());
                        return er.Exception.ToString();
                    }
                }

                psSessionConnection = result[0];
            }

IIS was unable to access Powershell session for remote Exchange server, why ?

Please share your Ideas

user3796521
  • 21
  • 1
  • 3

4 Answers4

1

This is a really old thread, but the suggested answers did not work for me. I finally figured it out and wanted to share my solution. This Access Denied message was being caused by the user who was running the application. Therefore, the Application Pool Identity was causing the error for me.

I changed it from ApplicationPoolIdentity to NetworkService and it worked!

zpert
  • 706
  • 9
  • 18
0

remote conections using powershell are not allowed per default. The server needs to allow this explicitly per user. See: Enable-PSRemoting

(Hint: it seems not to be possible to do any connection accros two domains.)

You first should try to do the remote connection using powershell to ensure it's no C# implementation issue:

PS D:\temp> $cerd = Get-Credential
...
PS D:\temp> $Session = New-PSSession -ConfigurationName Microsoft.Exchange 
-ConnectionUri http://ExchangeServer.admin.com/PowerShell/ -Authentication Kerberos -Credential 
$cerd

PS D:\temp> Import-PSSession $Session
.....
PS D:\temp> Get-TransportAgent
Harry B.
  • 51
  • 2
0

I had similar problem. My web application hosted on IIS worked just fine. One day this problem has occurred. Remote powershell from IIS server (just powershell console, not web application) worked fine.

I found out (after days od playing with Exchange roles), that restarting application pool is not enough to load new rights for application pool identity(I have changed AD "Member of"). This was very confusing during debugging.

When I have set application pool identity as Administrator on IIS server, everything started working again. Now I'm going to decrease these permissions to minimum.

Miroslav Adamec
  • 1,060
  • 1
  • 15
  • 23
  • So, I assume that you must log out the user or kill the IIS process to ensure that you load new rights? – GhotiPhud Jun 01 '16 at 18:45
0

My Application Pool is executed with an AD User. I had the same problem and could solve it as follows: I have set the properties Load User Profile to True.

SCREENSHOT I hope this solution can help someone

Aymen
  • 1