2

The following codes run:

        SecureString password = new SecureString();
        string runasUsername = "USERNAME";
        string runasPassword = "PASSWORD";

        string liveIdconnectionUri = "http://EXCHANGE_SERVER/PowerShell";

        foreach (char x in runasPassword)
        {
            password.AppendChar(x);
        }

        PSCredential credential = new PSCredential(runasUsername, password);

        // Set the connection Info
        WSManConnectionInfo connectionInfo = new WSManConnectionInfo((new Uri(liveIdconnectionUri)), "http://schemas.microsoft.com/powershell/Microsoft.Exchange",
        credential);

        connectionInfo.AuthenticationMechanism = AuthenticationMechanism.Basic; //AuthenticationMechanism.Default;

        // create a runspace on a remote path
        // the returned instance must be of type RemoteRunspace

        Runspace runspace = System.Management.Automation.Runspaces.RunspaceFactory.CreateRunspace(connectionInfo);

        PowerShell powershell = PowerShell.Create();
        PSCommand command = new PSCommand();

        command.AddCommand("Enable-Mailbox");
        command.AddParameter("Identity", "first.last");
        command.AddParameter("Alias", "Fist Last");

        powershell.Commands = command;
        try
        {
            // open the remote runspace
            runspace.Open();
            // associate the runspace with powershell
            powershell.Runspace = runspace;
            // invoke the powershell to obtain the results
            var result = powershell.Invoke();
        }
        catch (Exception ex)
        {

            Console.WriteLine(ex.Message);
        }
        finally
        {
            // dispose the runspace and enable garbage collection
            runspace.Dispose();
            runspace = null;
            // Finally dispose the powershell and set all variables to null to free
            // up any resources.
            powershell.Dispose();
            powershell = null;
        }

        Console.WriteLine("done");
        Console.Read();

Exceptions throws:

Connecting to remote server failed with the following error message : The WinRM client cannot process the request. Unencrypted traffic is currently disabled in the client configuration. Change the client configuration and try the request again. For more information, see the about_Remote_Troubleshooting Help topic.

I already set Basic Auth, allow unecrypted traffic.

I tried solution here powershell v2 remoting - How do you enable unencrypted traffic , no luck.

Community
  • 1
  • 1
unruledboy
  • 2,455
  • 2
  • 23
  • 30

3 Answers3

3

Sorry, struggled a for long time, kept changing possible combinations, and finally works with this:

The AuthenticationMechanism should be AuthenticationMechanism.Default, not AuthenticationMechanism.Basic (It's weird).

The final working version is:

        SecureString password = new SecureString();
        string runasUsername = "USERNAME";
        string runasPassword = "PASSWORD";

        string liveIdconnectionUri = "http://EXCHANGE_SERVER/PowerShell";

        foreach (char x in runasPassword)
        {
            password.AppendChar(x);
        }

        PSCredential credential = new PSCredential(runasUsername, password);

        // Set the connection Info
        WSManConnectionInfo connectionInfo = new WSManConnectionInfo((new Uri(liveIdconnectionUri)), "http://schemas.microsoft.com/powershell/Microsoft.Exchange",
        credential);

        connectionInfo.AuthenticationMechanism = AuthenticationMechanism.Default; //AuthenticationMechanism.Default;

        // create a runspace on a remote path
        // the returned instance must be of type RemoteRunspace

        Runspace runspace = RunspaceFactory.CreateRunspace(connectionInfo);

        PowerShell powershell = PowerShell.Create();
        PSCommand command = new PSCommand();

        command.AddCommand("Enable-Mailbox");
        command.AddParameter("Identity", "MAIL_USER_ID_HERE");

        powershell.Commands = command;
        try
        {
            // open the remote runspace
            runspace.Open();
            // associate the runspace with powershell
            powershell.Runspace = runspace;
            // invoke the powershell to obtain the results
            var result = powershell.Invoke();
            if (result.Count > 0)
                Console.WriteLine("sucessful!");
            else
                Console.WriteLine("failed!");
        }
        catch (Exception ex)
        {
            Console.WriteLine(ex.Message);
        }
        finally
        {
            // dispose the runspace and enable garbage collection
            runspace.Dispose();
            runspace = null;
            // Finally dispose the powershell and set all variables to null to free
            // up any resources.
            powershell.Dispose();
            powershell = null;
        }

        Console.WriteLine("done");
        Console.Read();
unruledboy
  • 2,455
  • 2
  • 23
  • 30
  • 1
    Basic is a clear-text(unencrypted) authentication similar to when you get a popupbox on a website. Default for WS-man is using Kerberos(encrypted) in domain or negotiate between non-domain computers. – Frode F. Feb 25 '13 at 07:26
1

I had the same issue. It should also be pointed out that, for the Virtual directory on the EXCHANGE_SERVER hosting the PowerShell instance, you should configure the SSL settings to "Accept" but not "Require SSL" in IIS Manager, assuming you still have the default self-signed certificate installed on the server. That plus the "AuthenticationMechanism.Default" setting got rid of the myriad exceptions I encountered at the line:

runspace.Open();

Also, if you want to unit test this locally, you should Install the Exchange Management Tools on your desktop.

...or, if you don't have Windows 8, try this approach: PowerShell Managed code in Exchange 2010.

CZahrobsky
  • 762
  • 7
  • 7
0

AuthenticationMechanism.Default worked for me but lead to another error message...

The WinRM client cannot process the request. Default authentication may be used with an IP address under the following conditions: the transport is HTTPS or the destination is in the TrustedHosts list, and explicit credentials are provided. Use winrm.cmd to configure TrustedHosts. Note that computers in the TrustedHosts list might not be authenticated. For more information on how to set TrustedHosts run the following command: winrm help config. For more information, see the about_Remote_Troubleshooting Help topic.

Note that EXCHANGE_SERVER must be a DNS name, not an IP address like I was using. I also had to set the AllowUnencrypted config setting on both the client and the Exchange server. See the link below for details on that setting.

powershell v2 remoting - How do you enable unencrypted traffic

Community
  • 1
  • 1
John81
  • 3,726
  • 6
  • 38
  • 58