C# System.Management.Automation错误访问被拒绝

C# System.Management.Automation错误访问被拒绝,c#,powershell,exchange-server,C#,Powershell,Exchange Server,System.Management.Automation class I使用c#PowerShell exchange程序访问服务器,增加电子邮件帐户的任务,程序错误访问被拒绝 <pre><code> PSCredential credentials = new PSCredential(UserName, ssRunasPassword); //ExchangeUri=ExUri WSManConnectio

System.Management.Automation class I使用c#PowerShell exchange程序访问服务器,增加电子邮件帐户的任务,程序错误访问被拒绝

<pre><code>
 PSCredential credentials = new PSCredential(UserName, ssRunasPassword);
              //ExchangeUri=ExUri
                WSManConnectionInfo connectionInfo = new WSManConnectionInfo(ExchangeUri, "http://schemas.microsoft.com/powershell/Microsoft.Exchange", credentials);
                connectionInfo.AuthenticationMechanism = AuthenticationMechanism.Basic;

                Runspace runspace = RunspaceFactory.CreateRunspace(connectionInfo);
                runspace.Open();
</code></pre>

好吧。。拒绝访问错误非常简单。我稍微修改了您的代码:


如果这不起作用,您可以尝试通过建立远程会话来查看您是否确实有权访问服务器。

hi。谢谢你的回答,但似乎没有特殊的代码更改,只是添加了我没有发布的代码的用户名和密码,拒绝访问提示是我构建服务器的问题
        string username = @"domain\username";
        string password = "pass";
        SecureString securePassword = new SecureString();
        foreach (char c in password)
        {
            securePassword.AppendChar(c);
        }
        securePassword.MakeReadOnly();

        PSCredential credentials = new PSCredential(username, securePassword);
        //ExchangeUri=ExUri
        WSManConnectionInfo connectionInfo = new WSManConnectionInfo(new Uri(ExUri), "http://schemas.microsoft.com/powershell/Microsoft.Exchange", credentials);
        connectionInfo.AuthenticationMechanism = AuthenticationMechanism.Basic;

        Runspace runspace = RunspaceFactory.CreateRunspace(connectionInfo);
        runspace.Open();