none
Access denied when using powershell in C# code , then running it from IIS RRS feed

  • Question

  • Hi ,

    I need to execute below PowerShell command from C# code. This command results Users list who has access permissions to particular user.

    Get-Mailbox -resultsize unlimited | Get-MailboxPermission | Where {(!$_.isinherited) -and ($_.User -like 'ADMIN\ExchangeAdmin') -and  ($_.accessrights -contains "fullaccess")  } | Select Identity,User

    I am already done it , getting users list successfully in C# code .  code working fine  in visual studio.

    but same code running from IIS getting Access denied . i tried to resolve it , but not yet succeed.

    please see the link for my code.

    http://social.technet.microsoft.com/Forums/office/en-US/f97b1c19-6fb4-4b77-9afa-eeae0988bd64/exchange-service-api-is-not-working-after-code-published-in-iis-but-locally-it-is-working-please?forum=exchangesvrdevelopment

    so , i would like to change the code.

    below code working fine in IIS . how can i call above power shell command from below C# code ?

     SecureString password = new SecureString();
                     string str_password = "xxxxx";
                     string username = "Administrator@admin.com";
                     foreach (char x in str_password) { password.AppendChar(x); }
                     PSCredential credential = new PSCredential(username, password);
    
                     WSManConnectionInfo connectionInfo = new WSManConnectionInfo(new Uri("http://ExchangeServer.admin.com/powershell/Microsoft.Exchange"), "http://schemas.microsoft.com/powershell/Microsoft.Exchange", credential);
    
                     Runspace runspace = RunspaceFactory.CreateRunspace(connectionInfo);
    
                     PowerShell powershell = PowerShell.Create();
    
                     PSCommand command = new PSCommand();
                     command.AddCommand("Get-Mailbox");
                     command.AddParameter("ResultSize", int.MaxValue);
                     powershell.Commands = command;
                     runspace.Open();
                     powershell.Runspace = runspace;
                     Collection<PSObject> usersList = powershell.Invoke();
                     runspace.Close();
                     return usersList;

    Initially above code also gets Access Denied Exception in IIS, After changing Application pool from Default app pool to  MSExchangePowerShellFrontEndAppPool error resolved.


    • Edited by Dora407 Saturday, July 19, 2014 10:01 AM
    Saturday, July 19, 2014 10:00 AM

All replies