No one has responded to this discussion for at least a year, so this information may be out of date. If you're looking for information about this topic, please search for a more recent discussion or post a new question.

System.Management.Automation.Remoting.PSRemotingTransportException

  • duplicate post

  • Hello,

           I am trying to execute my exchange server monitoring commands using a C# application(I have successfully tested this with power-shell console). I have used the following code in my C# project. 

    <pre>

    try
                {
                    string userName = @"abc\Administrator";
                    string password = "xxx";


                    SecureString secureString = new SecureString();
                    foreach (char c in password) secureString.AppendChar(c);
                    secureString.MakeReadOnly();


                    PSCredential credential = new PSCredential(userName, secureString);


                    WSManConnectionInfo connectionInfo = new WSManConnectionInfo(new
                   Uri("http://192.168.1.221/powershell/"),
                   "http://schemas.microsoft.com/powershell/Microsoft.Exchange", credential) {                      

                    AuthenticationMechanism = AuthenticationMechanism.Basic };


                    Runspace runspace = RunspaceFactory.CreateRunspace();
                    PowerShell powershell = PowerShell.Create();
                    PSCommand command = new PSCommand();
                    command.AddCommand("New-PSSession");
                    command.AddParameter("ConfigurationName", "Microsoft.Exchange");
                    command.AddParameter("ConnectionUri", new Uri("http://192.168.1.221/powershell/"));
                    command.AddParameter("Credential", credential);
                    command.AddParameter("Authentication", "Basic");


                    powershell.Commands = command;


                    runspace.Open();


                    powershell.Runspace = runspace;


                    Collection<PSSession> result = powershell.Invoke<PSSession>();
                    foreach (ErrorRecord current in powershell.Streams.Error)
                        MessageBox.Show(String.Format("The following Error happen when opening the remote 

                        Runspace: {0} | InnerException: {1}",
                         current.Exception, current.Exception.InnerException));


                    if (result.Count != 1)
                        throw new Exception("Unexpected number of Remote Runspace connections returned.");


                    powershell = PowerShell.Create();
                    command = new PSCommand();
                    command.AddCommand("Set-Variable");
                    command.AddParameter("Name", "ra");
                    command.AddParameter("Value", result[0]);
                    powershell.Commands = command;
                    powershell.Runspace = runspace;
                    powershell.Invoke();


                    powershell = PowerShell.Create();
                    command = new PSCommand();
                    const string CreateMailBoxScript = "new-mailbox -Name:’{0}’ -Password:(ConvertTo-SecureString 

                    {1}  -AsPlainText -Force) -WindowsLiveID:{2} -FirstName:{3} -LastName:{4} -DisplayName:’{5}’";
                    command.AddScript(string.Format(CreateMailBoxScript, "sxavier", "textboxwithmail", 

                      "s@hotmail.com", "Sebastian", "Xavier", "Sebastian Xavier"));
                    powershell.Commands = command;
                    powershell.Runspace = runspace;
                    powershell.Invoke();


                    runspace.Dispose();
                    runspace = null;
                    powershell.Dispose();
                    powershell = null;
                }
                catch (Exception Ex)
                {
                    MessageBox.Show(Ex.Message);
                }

    </pre>


    But I am ended up in an exception.


    <pre>

    System.Management.Automation.Remoting.PSRemotingTransportException:
    Connecting to remote server failed with the following error message : The WinRM client cannot process the request. The authentication mechanism requested by the client is not supported by the server or unencrypted traffic is disabled in the service configuration. Verify the unencrypted traffic setting in the service configuration or specify one of the authentication mechanisms supported by the server. To use Kerberos, specify the 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 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.

    </pre>


    It would be a great help to me , if someone of you can help me.

    Thanking you in advance


    Sebastian

  • Hi Sebastian,

    I understand you have tested the PowerShell cmdlets with power-shell console successfully. Just would like to confirm are you running the c# code on the same computer with the Powershell console you test? According to the error message, it sounds the WinRM service failed the request to authenticate with Exchange online. I suggest you try to configure Windows PowerShell on the computer you are running the program and ensure the PowerShell console can connect to Exchange Online successfully.

    To install and configure Windows PowerShell, please refer to the following article:
    http://help.outlook.com/en-us/140/cc952756.aspx

    Since it's development related question, I also suggest you ask it in the Windows PowerShell forum.

    Windows PowerShell forum:
    http://social.technet.microsoft.com/Forums/en-US/winserverpowershell/threads

    Thanks,
    Reken Liu

  • Yes, I am running my c# application from my local PC only, from which I have successfully executed Exchange commands in powershell console. And WinRM service is running both on server & my machine.

    Thanks for your reply

  • After enabling basic authentication of power-shell virtual directory in IIS, I have tried the same code as a console application and have a new exception..

    The term 'new-mailbox' is not recognized as the name of a cmdlet, function, script file, or operable program. Check the spelling of the name, or if a path was included, verify that the path is correct and try again.


    When I tried to fetch data using getmailbox, I got another error as follows...


    Active Directory operation failed on WIN-010LMRATJ8N.abc.com. The supplied credential for 'abc\123' on Bind operation is invalid. Error Code: 0x31.


    anymore hep would be great


    Regards

    Sebastian

  • Hello sxavier,

    As far as the first error, it sounds as though you did not import the cmdlets. There are two sets of cmdlets, one from the Microsoft Online Services Module and another imported to PowerShell. To get both sets of cmdlets, run the following from PowerShell:

    $Creds = Get-Credential

    $Session = New-PSSession -ConfigurationName Microsoft.Exchange -ConnectionUri https://ps.outlook.com/powershell/ -Credential $Creds -Authentication Basic -AllowRedirection

    Import-PSSession $Session -AllowClobber

    Import-Module MSOnline

    Connect-MsolService –Credential $Creds

    If you need to download the Microsoft Online Services Module, please see Step 2 on the page http://onlinehelp.microsoft.com/en-us/office365-enterprises/ff652560.aspx 

    A reference for the cmdlets is available at http://onlinehelp.microsoft.com/en-us/office365-enterprises/hh125002.aspx

    As to your second error, it appears that the credential for the user specified is incorrect. Please check to see that the user you are signing in with is a valid administrator and that the password is correct.

    Hope this helps,
    Gary Moore
    Office 365 Community Forums Moderator

  • Hello Gary Moore,

    Thanks for your help.

    I have tried the following commands , But I got some exceptions... 




    Now, when I tried to install Microsoft Online Services Module I have got another error....

    "In order to install Microsoft Online services Module for windows powershell, you must have microsoft online services sign-in assistant version 7.0 or greater installed on this computer"

    But i was unable to find the download link of microsoft online services sign-in assistant version 7.0

    I would be grateful to you, for any more help.

    Regards

    Sebastian

  • Hello sxavier,

    The errors you are getting would be expected if you have not installed the Microsoft Online Services Module. I also understand that you're getting an error when trying to install the Module.

    The Microsoft Online Services Sign-in Assistant can be downloaded from http://www.microsoft.com/en-us/download/details.aspx?displaylang=en&id=11859. Once that is installed, you should be able to install the Module and that should solve your problem.

    By the way, the new-mailbox cmdlet is in the cmdlets you were able to import before you got the error. You can use

    Get-Command New-Mailbox

    in PowerShell to confirm that. for more information on the New-Mailbox cmdlet, type

    Get-Help New-Mailbox

    Hope this helps,

    Gary Moore
    Office 365 Forum Moderator

  • Hi Gary,

    Thanks for your valuable time.

    I have downloaded and installed online services sign in assistant using following link.

    http://blogs.technet.com/b/hot/archive/2011/11/29/how-to-configure-user-passwords-to-never-expire-in-office-365.aspx

    But when I tried the commands I got the following error. (The username which I gave was user@domain.com )


    I hope you can further help me.


    Thanks
    Sebastian




  • Hello Sebastian,

    The credentials you provide must be the correct O365 email address and password of an Administrator on your account. Using the correct administrator credentials should clear up this problem.

    Thanks,

    Gary Moore
    Office 365 Community Forums Moderator

  • Hello Sebastian,

    I apologize for the inconveniences this issue may have caused you and your organization. Please confirm if you were able to use the correct administrator credentials, also if you are able to run the PowerShell cmdlet.

     

    Best regards,
    Sherika MSFT Support

  • Hello Sebastian,

    How are things going? 

    If you have any other questions or concerns, please do not hesitate to contact us. It is always our pleasure to be of assistance.

    Regards
    Samuel

  • I can run these commands using power-shell console. I have rechecked the credentials and that is correct.

    Regards

    Sebastian

  • Hi Samuel,

    I still can't execute this. I am fed up with this...

    Regards

    Sebastian

  • Hello sxavier,

     I am sory you are having so much problems and want to verify the steps you are taking to run the  PowerShell commands.
     I am going to detail some steps below.  I am providing a link to PowerShell v 2.0 below

    Please download PowerShell v 2.0 from this link http://onlinehelp.microsoft.com/en-us/Office365-enterprises/ff652560.aspx and install it.

    Follow the steps below carefully 1-6  to connect PowerShell to Exchange Online.

    1. Click Start > All Programs > Accessories > Windows PowerShell > Windows PowerShell. Right click it to run as administrator.

    2. Run the following command:    $LiveCred = Get-Credential


    3. In the Windows PowerShell Credential Request window that opens, type the credentials of an account in your cloud-based organization. When you are finished, click OK.


    4. Run the following command:


    $Session = New-PSSession -ConfigurationName Microsoft.Exchange -ConnectionUri https://ps.outlook.com/powershell/ -Credential $LiveCred -Authentication Basic -AllowRedirection


    Note The AllowRedirection parameter enables cloud-based organizations in datacenters all over the world to connect Windows PowerShell to the cloud-based service by using the same URL.


    5. Run the following command:
    Import-PSSession $Session


     If it fails with an error message we may need to set the Execution policy to allow running remote PowerShell scripts.
    Run Get-ExecutionPolicy .
    If the value returned is anything other than RemoteSigned, you need to change the value to RemoteSigned by running
    Set-ExecutionPolicy RemoteSigned


    import-module MSOnline


    get-module MSOnline will list all available MODULES


    6. Run the following command:     Connect-MsolService -Credential $LiveCred



    Please follow the steps outlined above carefully and report back on your progress.

    Best regards

    Samuel