远程连接到 IIS 上托管的交换 Powershell

Remote connection to an exchange Powershell hosted on IIS

我正在尝试创建到 IIS 8.5 上托管的交换 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.

我不明白这是怎么回事...我已经进行了验证:

我错过了什么吗?谢谢。

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

如果原因是"the Window Remote Management service and its listener functionality are broken."

,这很有用

是服务器问题,不是您的代码问题。

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

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

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