简体   繁体   中英

Remote connection to an exchange Powershell hosted on IIS

I am trying to create a remote connection to an exchange Powershell hosted on IIS 8.5 - Windows Server 2012 R2.

Here's my code sample :

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;
    }

The problem :

On : runspace.Open(); , I have this error : 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.

I don't understand what's going on... I have made this verifications :

  • I have make sure the user have the good rights.
  • I have enabled the Basic's authentication on IIS for the Powershell.
  • My development station can communicate with the server without any problems.

Do I miss something ? Thank you.

  1. Is the server you are trying to connect to in the WSManConnectionInfo object an Exchange server?

  2. You say your dev workstation connects fine to the server. Which server are you referring to? The Exchange server or the one hosting this code?

To open the remote runspace with the Microsoft.Exchange shell, you need to be connecting to an Exchange server. By your wording, it seems to me you're trying to host a service that calls Exchange cmdlets? If so, you'll either need to send the commands over to the Exchange server to run, or you'll need to import them to run them. You're current structure would be to send the commands to the server.

Have you tried the resolution described here: http://support.microsoft.com/kb/2269634/en-us ?

This is useful if the cause is "the Window Remote Management service and its listener functionality are broken."

Being a server issue, not an issue with your code.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM