Quantcast
Channel: Exchange Server Development forum
Viewing all articles
Browse latest Browse all 7132

c# powershell for exchange

$
0
0

Hi, I really need help with this one. I have been trying to figure out on how to fix the "runspace.open" issue but could notfind the true error message.

SecureString securePassword = new SecureString();
            foreach (char c in password) { securePassword.AppendChar(c); }
            PSCredential credential = new PSCredential(userName, securePassword);

            WSManConnectionInfo connectionInfo = new WSManConnectionInfo(new Uri(exchangeURL), exchangeSchema, credential);
            Runspace runspace = RunspaceFactory.CreateRunspace(connectionInfo);
            connectionInfo.AuthenticationMechanism = AuthenticationMechanism.Basic;
            PowerShell powershell = PowerShell.Create();

            PSCommand command = new PSCommand();
            //PipelineReader<object> errors;
            command.AddCommand("get-mailbox");
            powershell.Commands = command;

            try
            {
                runspace.Open();
                powershell.Runspace = runspace;
                Collection<PSObject> results = powershell.Invoke();
            }
            catch (Exception exception)
            {
                if (exception is System.Management.Automation.Remoting.PSRemotingTransportException)
                {
                    switch (((System.Management.Automation.Remoting.PSRemotingTransportException)exception).ErrorCode)
                    {
                        case 5:  //process password issue
                            break;
                        case -2144108526: //Process URL issue
                            break;
                        default:// process other errors
                            break;
                    }
                }
            }

This is the similar example that I tried to follow.

http://social.technet.microsoft.com/Forums/exchange/en-US/bc5f262c-681c-43e0-aa69-b71d1ad3d0d5/e2010psc-how-to-open-remote-powershell-runspace-and-catch-errors

Here is the error message code that I got which I don't know.

Connecting to remote server failed with the following error message : The WS-Management service does not support the request. For more information, see the about_Remote_Troubleshooting Help topic.
System.Management.Automation.RuntimeException {System.Management.Automation.Remoting.PSRemotingTransportException}
ErrorCode    -2144108428    int


Viewing all articles
Browse latest Browse all 7132

Trending Articles