远程连接到IIS上托管的Exchange Powershell

时间:2015-01-19 21:30:22

标签: c# asp.net powershell iis exchange-server

我正在尝试创建与IIS 8.5托管的Exchange Powershell的远程连接 - Windows Server 2012 R2。

这是我的代码示例:

protected void Page_Load(object sender, EventArgs e)
{
    List<string> test = new List<string>();

    test = GetMailboxDatabase();
}

public static List<String> GetMailboxDatabase()
{
    List<string> Listdatabase = new List<string>();

    var runspace = RunspaceFactory.CreateRunspace(getconinfo());

    var command = new Command("Get-MailboxDatabase");

    // Add the command to the runspace's pipeline
    runspace.Open();
    var pipeline = runspace.CreatePipeline();
    pipeline.Commands.Add(command);

    Collection<PSObject> results = pipeline.Invoke();

    // close the runspace

    runspace.Close();

    foreach (PSObject obj in results)
    {
        Listdatabase.Add(obj.ToString());
    }

    return Listdatabase;
}

    public static WSManConnectionInfo getconinfo()
    {
        // Prepare the credentials that will be used when connecting
        // to the server. More info on the user to use on the notes
        // below this code snippet.
        string runasUsername = "xxx";
        string runasPassword = "xxx";
        SecureString ssRunasPassword = new SecureString();
        foreach (char x in runasPassword)
            ssRunasPassword.AppendChar(x);
        PSCredential credentials =
            new PSCredential(runasUsername, ssRunasPassword);

        // Prepare the connection
        var connInfo = new WSManConnectionInfo(
            new Uri("https://(server - ipadress)/PowerShell"),
            "http://schemas.microsoft.com/powershell/Microsoft.Exchange",
            credentials);
        connInfo.AuthenticationMechanism =
            AuthenticationMechanism.Basic;
        connInfo.SkipCACheck = true;

        connInfo.SkipCNCheck = true;

        return connInfo;
    }

问题:

开:runspace.Open();,我有这个错误:Connecting to remote server failed with the following error message : The WinRM client received an HTTP bad request status (400), but the remote service did not include any other information about the cause of the failure.

我不明白发生了什么......我已经做了这个验证:

  • 我确保用户拥有良好的权利。
  • 我已在IIS上为Powershell启用了基本身份验证。
  • 我的开发站可以毫无问题地与服务器通信。

我错过了什么吗?谢谢。

2 个答案:

答案 0 :(得分:1)

  1. 您尝试在WSManConnectionInfo对象中连接的服务器是Exchange服务器吗?

  2. 您说您的开发工作站可以很好地连接到服务器。你指的是哪个服务器? Exchange服务器或托管此代码的服务器?

  3. 要使用Microsoft.Exchange shell打开远程运行空间,您需要连接到Exchange服务器。根据您的措辞,在我看来,您正在尝试托管一个称为Exchange cmdlet的服务?如果是这样,您需要将命令发送到Exchange服务器才能运行,或者您需要导入它们才能运行它们。您当前的结构是将命令发送到服务器。

答案 1 :(得分:0)

您是否尝试过此处所述的解决方案:http://support.microsoft.com/kb/2269634/en-us

如果原因是“Window远程管理服务及其侦听器功能已损坏”,则此功能非常有用。

是服务器问题,而不是代码问题。