none
Identify IMAP protocol through Powershell and C# RRS feed

  • Question

  • Hi all,

    I have been facing issues while running Powershell commands from my host machine on the remote exchange server in mixed domains using C#.

    Here's my sample code:

    class Program
        {
            static void Main(string[] args)
            {
                string userName = "username";
                string password = "*******";
                try
                {
                    // Encrypt password using SecureString class
                    SecureString securePassword = new SecureString();

                    foreach (char c in password)
                    {
                        securePassword.AppendChar(c);
                    }

                    PSCredential credential = new PSCredential(userName, securePassword);

    WSManConnectionInfo connectionInfo = new WSManConnectionInfo(false, "RemoteMachineName", 5985, "/wsman", "http://schemas.microsoft.com/powershell/Microsoft.PowerShell",credential);

    connectionInfo.AuthenticationMechanism = AuthenticationMechanism.Basic;


     using (Runspace runspace = RunspaceFactory.CreateRunspace(connectionInfo))
                    {
                        connectionInfo.MaximumConnectionRedirectionCount = 1;

                        runspace.Open(); <-------------ERROR OCCURS

                       using (PowerShell ps = PowerShell.Create())
                        {
                            ps.Runspace = runspace;

                            Command new = new Command("Get-CASMailbox");
                        new.Parameters.Add(new CommandParameter("User", "MAILBOXUSERNAME"));
                new.Parameters.Add(new CommandParameter("IMAP4Enabled",$true);  
                            ps.Commands.AddCommand(new);
                try
                        {
                               // Invoke command and store the results in a PSObject
                                Collection<PSObject> results = ps.Invoke();

                        if (ps.Streams.Error.Count > 0)
                                   {
                                    foreach (ErrorRecord error in ps.Streams.Error)
                                    {
                        Console.WriteLine(error.ToString());
                                    }
                                }

                                else
                                {
                              foreach (PSObject result in results)
                                   {

                                    Console.WriteLine(string.Format("Name: {0},                                 PrimarySmtpAddress: {1}",

                                    result.Properties["DisplayName"].Value.ToString(),

                     result.Properties["PrimarySmtpAddress"].Value.ToString()));
                                   }
                                }

                            }
                            catch (Exception ex)
                            {
                                Console.WriteLine(ex.Message);
                            }
                            finally
                            {
                                Console.WriteLine("Operation completed.");
                            }

                        }
                    }
                }
                catch (Exception ex)
                {
                    Console.WriteLine(ex.Message);
                }

                Console.ReadKey();
            }
        }

    As marked above, error occurs at "runspace.open()" line.

    I've been facing errors like:

    "Connecting to remote server failed with the following error message : WS-Managem
    ent cannot process the request. The operation failed because of an HTTP error. T
    he HTTP error (12175) is: A security error occurred . For more information, see
    the about_Remote_Troubleshooting Help topic."

    or "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 ag
    ain. For more information, see the about_Remote_Troubleshooting Help topic."

    or sometimes "INVALID Url" error.
    I've installed Windows Management Framework Core package and windows powershell 2.0 and the winrm service is also running perfectly, but still facing these issues.

    Any Help plz..

    Thanks in advance..
    Monday, April 2, 2012 2:43 PM

All replies