PowerShell远程调用。 IIS拒绝访问

时间:2017-05-29 09:01:25

标签: c# asp.net office365 exchange-server

我从asp.net应用程序编写远程访问交换PowerShell的代码,以使用vb.net启用远程邮件,并从我的visual studio调试成功运行命令,但是当我放入iis web服务器时,它给了我< / p>

  

访问被拒绝

我将用户添加到服务器管理员/本地管理员甚至组织管理员,因为我将应用程序池更改为服务器管理员用户 并且仍然在网络服务器中得到相同的错误

这是代码

string back = "";
            try
            {
                string ServerUri = "http://{server}/PowerShell/";
                //use one of CAS servers
                string SchemaUri = "http://schemas.microsoft.com/powershell/Microsoft.Exchange";
                string userName = AccountOperatorLogon;
                //user must be member of "Recipient Management" group to create Mailboxes
                System.Security.SecureString password = new System.Security.SecureString();
                foreach (char x in AccountOperatorPassword)
                {
                    password.AppendChar(x);
                }
                string onmicrosoftemail = Email.ToLower().Replace("mail.com", "mail.onmicrosoft.com");
                PSCredential PSCredential = new PSCredential(userName, password);
                WSManConnectionInfo ConnectionInfo = new WSManConnectionInfo(new Uri(ServerUri), SchemaUri, PSCredential);
                ConnectionInfo.AuthenticationMechanism = AuthenticationMechanism.Kerberos;
                Runspace RemoteRunspace = RunspaceFactory.CreateRunspace(ConnectionInfo);
                PowerShell RemotePowerShell = PowerShell.Create();
                RemotePowerShell.AddCommand("Enable-RemoteMailbox");
                RemotePowerShell.AddParameter("Identity", samAccount);
                RemotePowerShell.AddParameter("RemoteRoutingAddress", onmicrosoftemail);
                // Open the remote runspace on the server.
                RemoteRunspace.Open();
                // Associate the runspace with the Exchange Management Shell.
                RemotePowerShell.Runspace = RemoteRunspace;
                var result = RemotePowerShell.Invoke();
                foreach (PSObject RSLT in result)
                {
                    back += RSLT.ToString();
                }
                RemoteRunspace.Close();
            }
            catch (Exception ex)
            {
                throw ex;
            }
            return back;

2 个答案:

答案 0 :(得分:1)

我发现任何人的解决方案都有同样的问题

  1. 将用户创建为(收件人管理组)
  2. 的成员
  3. IIS更改(用于Exchange服务器) 导航到IIS管理器|默认网站|电源外壳 更改物理路径:C:\ Program Files \ Microsoft \ Exchange Server \ V15 \ FrontEnd \ HttpProxy \ PowerShell to:C:\ Program Files \ Microsoft \ Exchange Server \ V15 \ ClientAccess \ PowerShell
  4. 更改后:IISRESET
  5. 它会正常工作

答案 1 :(得分:0)

1。)确保已启用Windows PowerShell进行远程处理(请参阅更多here):

  

启用-PSRemoting

2.确保本地操作系统防火墙没有阻止请求

3。)根据您希望执行的操作,您需要授予一些所需的权限。对于MS Exchange

  

Set-User David -RemotePowerShellEnabled $ True

<强> P.S。 出于安全原因,我绝不会将IIS池作为具有本地管理员权限的帐户运行,如帖子中所述!