0

I am trying remotely access Exchange Server 2013 using PowerShell from Windows 7. Below is the code snippet that I am using:

public static void ConnectToPowerShell()
{
        string password = "password";
        string userName = "Administrator";

        System.Uri uri = new Uri(@"http://ExchangeServer/powershell?serializationLevel=Full");
        System.Security.SecureString securePassword = String2SecureString(password);

        System.Management.Automation.PSCredential creds = new System.Management.Automation.PSCredential(userName, securePassword);

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

        PowerShell powershell = PowerShell.Create();
        PSCommand command = new PSCommand();
        command.AddCommand("New-PSSession");
        command.AddParameter("ConfigurationName", "Microsoft.Exchange");
        command.AddParameter("ConnectionUri", uri);
        command.AddParameter("Credential", creds);
        command.AddParameter("Authentication", "Default");
        PSSessionOption sessionOption = new PSSessionOption();
        sessionOption.SkipCACheck = true;
        sessionOption.SkipCNCheck = true;
        sessionOption.SkipRevocationCheck = true;
        command.AddParameter("SessionOption", sessionOption);

        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
            Collection<PSSession> result = powershell.Invoke<PSSession>();
            String str=null;
            foreach (ErrorRecord current in powershell.Streams.Error)
            {
                // Console.WriteLine("The following Error happen when opening the remote Runspace: " + current.Exception.ToString() +
                // " | InnerException: " + current.Exception.InnerException);
                str = "The following Error happen when opening the remote Runspace: " + current.Exception.ToString() +
                   " | InnerException: " + current.Exception.InnerException;
                MessageBox.Show(str);
            }
            if (result.Count != 1)
                throw new Exception("Unexpected number of Remote Runspace connections returned.");

            // Set the runspace as a local variable on the runspace
            powershell = PowerShell.Create();
...........................................
...........................................
}

At the line:

Collection<PSSession> result = powershell.Invoke<PSSession>();

It gives the following exception:

The following Error happen when opening the remote Runspace: System.Management.Automation.Remoting.PSRemotingTransportException: Connecting to remote server failed with the following error message : The WinRM client cannot process the request. The WinRM client tried to use Negotiate authentication mechanism, but the destination computer (rmex13:80) returned an 'access denied' error. Change the configuration to allow Negotiate authentication mechanism to be used or specify one of the authentication mechanisms supported by the server. To use Kerberos, specify the local computer name as the remote destination. Also verify that the client computer and the destination computer are joined to a domain. To use Basic, specify the local computer name as the remote destination, specify Basic authentication and provide user name and password. Possible authentication mechanisms reported by server: For more information, see the about_Remote_Troubleshooting Help topic

How can I fix this type of exception?

6
  • What permissions does the account being used have on the remote server? What is the current execution policy on the remote server? What type of authentication is the remote server expecting?
    – Colyn1337
    Jul 3, 2014 at 12:33
  • This looks like a question better suited over at stackoverlow.com
    – MichelZ
    Jul 4, 2014 at 7:12
  • @MichelZ I had also putt this question but not get any answer.Is there any solution to this please mention
    – Harry
    Jul 4, 2014 at 7:39
  • 1
    If you need to connect to Exchange from C# wouldn't you be better off using the EWS managed API?
    – Trondh
    Jul 7, 2014 at 10:00
  • @Trondh using EWS i can only find the data like outlook, but i wants to find many more information.
    – Harry
    Jul 8, 2014 at 4:29

0

You must log in to answer this question.

Browse other questions tagged .