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.

    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 = "";
                     foreach (char x in str_password) { password.AppendChar(x); }
                     PSCredential credential = new PSCredential(username, password);
                     WSManConnectionInfo connectionInfo = new WSManConnectionInfo(new Uri(""), "", credential);
                     Runspace runspace = RunspaceFactory.CreateRunspace(connectionInfo);
                     PowerShell powershell = PowerShell.Create();
                     PSCommand command = new PSCommand();
                     command.AddParameter("ResultSize", int.MaxValue);
                     powershell.Commands = command;
                     powershell.Runspace = runspace;
                     Collection<PSObject> usersList = powershell.Invoke();
                     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