none
Exchange Service API is not working after code published in IIS , But locally it is working. Please see the code RRS feed

  • Question

  • Hi all,

    I am using Exchanging server 2013.

    Please see code below is not working after  published in IIS. In locally it is working fine.

    I think the problem is here I am accessing Powershell script file in C# code . powershell files[.ps1] was unable to identify by IIS. so i add new MIME type for .ps1 file extension.

    but still no use.

    In the below code  , please see the path string file = "D:/Scripts/listcmd.ps1"; 

    my Remaining code is working fine in IIS , if i Comment below code . so i think the problem is here

    please help me

     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();
    psSessionConnection = result[0];
    }
    
    // Set ExecutionPolicy on the process to unrestricted
    using (var powershell = PowerShell.Create())
    {
    var command = new PSCommand();
    command.AddCommand("Set-ExecutionPolicy");
    command.AddParameter("Scope", "Process");
    command.AddParameter("ExecutionPolicy", "Unrestricted");
    powershell.Commands = command;
    powershell.Runspace = runspace;
    
    powershell.Invoke();
    }
    
    // Import remote exchange session into runspace
    using (var powershell = PowerShell.Create())
    {
    var command = new PSCommand();
    command.AddCommand("Import-PSSession");
    command.AddParameter("Session", psSessionConnection);
    powershell.Commands = command;
    powershell.Runspace = runspace;
    
    powershell.Invoke();
    }
    
    String file = "D:/Scripts/listcmd.ps1"; 
    // here i am accessing .ps1 file
    
    Pipeline pipeline = runspace.CreatePipeline();
    pipeline.Commands.AddScript(file);
    Collection<PSObject> results = pipeline.Invoke();
    runspace.Close();
    
    
    foreach (PSObject obj in results)
    {
    var name = obj.Members["Identity"].Value.ToString();
    }



    • Edited by Dora407 Friday, July 18, 2014 5:37 AM
    Thursday, July 17, 2014 7:35 PM

All replies

  • Do you get an error or exception ? if so what is the error your get can you trace it to which line of the code is failing ?

    Cheers
    Glen

    Friday, July 18, 2014 6:08 AM
  • HI Glen,

    I got Exception

    {
        "Message": "An error has occurred.",
        "ExceptionMessage": "Index was out of range. Must be non-negative and less than the size of the collection.\r\nParameter name: index",
        "ExceptionType": "System.ArgumentOutOfRangeException",
        "StackTrace": "   at ExchangeSample.Controllers.API.APIContactController.powershellcheck()\r\n   at System.Web.Http.Controllers.ReflectedHttpActionDescriptor.ActionExecutor.<>c__DisplayClass13.<GetExecutor>b__c(Object instance, Object[] methodParameters)\r\n   at System.Web.Http.Controllers.ReflectedHttpActionDescriptor.ActionExecutor.Execute(Object instance, Object[] arguments)\r\n   at System.Threading.Tasks.TaskHelpers.RunSynchronously[TResult](Func`1 func, CancellationToken cancellationToken)"
    }

    you can see the 'Exceptionmessage' : Index was out of range.

    it comes because of i am trying to getting result[0] , but result is empty. [ See line no12 in above code]

    var result = powershell.Invoke();
    psSessionConnection = result[0];

    here i am getting Result Empty.

    In locally [in visual studio 2012] , it is working fine. but after publishing code in IIS getting result empty.

    IIS was unable to create Remote power shell session   ? but locally working why ?


    Friday, July 18, 2014 8:00 AM
  • You should check the Error Stream to see what errors are coming back from Powershell eg

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

    Monday, July 21, 2014 3:52 AM
  • Hi Glen,

    1. "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."

    I tried with some solutions , by searching above error on google . none of them work for me.

    Monday, July 21, 2014 5:40 AM
  • Based on the exception that is being returned you can see that your code is failing because of the security context you code is running under. In your code I can't see you specifying explicit credentials anywhere so under what credentials is your code trying to run under ? eg are you trying to use impersonation, if so your potentially going to have problems with Kerberos delegation eg see http://blogs.msdn.com/b/emeamsgdev/archive/2012/11/05/exchange-web-services-from-a-web-application-using-windows-authentication.aspx

    Cheers
    Glen

    Tuesday, July 22, 2014 4:36 AM
  • Hi Glen,

    I am not using impersonation in my code. Just now as for your  suggestion I added my Admin credentials in the code but still same result. I removed kerberos authentication also, but still same result.

    Can u please tell me which authentication will run perfectly in IIS or what is to be used ?

    if it is basic or kerberos or any other authentication 

    what are changes need to be  modified  in web.config , in my C# code and in IIS ?

    i tried in different ways from last 3 days getting same result , but locally [ in visual studio ] working fine .

    Please help me 

    Tuesday, July 22, 2014 9:26 AM
  • >>   I added my Admin credentials in the code but still same result

    If you have specified explicit credentials in your code correctly then that shouldn't happen it sound like you have some other fundamental error happening. I would suggest you go back to first principals and test to see where the error is really occurring.

    Eg start out by testing if you can make a Remote powershell connection to the Exchange server on the Server where you want to run the code just from the console eg using something like http://technet.microsoft.com/en-us/library/dd297932(v=exchg.141).aspx

    This will confirm that you can connect okay from that server with whatever security context you intend to use and whether your run your script will run okay with error its will also tell you what authentication type you should be using in your C# code. Then try to move what you learned here into you c# code in terms of Authentication type and creds you should be able to run it with explicit credentials okay (as long as you use PSCredentials and use the secure password) as that removes any IIS configuration. Once you have that working then move onto whatever else it is your trying to do. You have to specific with each step and debug every point

    Cheers
    Glen

    Wednesday, July 23, 2014 6:12 AM