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 = "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 fromDefault app pool to MSExchangePowerShellFrontEndAppPool error resolved.