Vinc 웃
Vinc 웃

Reputation: 1257

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 :

Do I miss something ? Thank you.

Upvotes: 0

Views: 3369

Answers (2)

codingChris
codingChris

Reputation: 697

  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.

Upvotes: 1

Kind Contributor
Kind Contributor

Reputation: 18591

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.

Upvotes: 0

Related Questions