PowerShell icon indicating copy to clipboard operation
PowerShell copied to clipboard

[Ubuntu][New-PSSession] WS-Management service cannot process the request because the XML is invalid.

Open BellaLi opened this issue 8 years ago • 2 comments

Steps to reproduce

Wrap New-PSSession in C#, build with .netstandard2.0 and pscore, run in Ubuntu 16.04.

public class NewExoPSSession : Cmdlet
{
    …..
    protected override void ProcessRecord()
    {
                var modifiedConnectionUriBuilder = new UriBuilder(ConnectionUri);
                NameValueCollection queryParameters = …….;
                modifiedConnectionUriBuilder.Query = queryParameters.ToString();

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

                command.AddCommand("New-PSSession");
                command.AddParameter("AllowRedirection", true);
                command.AddParameter("Authentication", "Basic");
                command.AddParameter("ConfigurationName", "Microsoft.Exchange");
                command.AddParameter("ConnectionUri", modifiedConnectionUriBuilder.ToString());
                command.AddParameter("Credential", basicAuthCredentials);

                if (psSessionOption != null)
                {
                    command.AddParameter("SessionOption", psSessionOption);
                }

                powershell.Commands = command;
                psSessions = powershell.Invoke();

                if (powershell.HadErrors)
                {
                    if (powershell.Streams != null && powershell.Streams.Error != null &&
                        powershell.Streams.Error.Count > 0)
                    {
                        throw powershell.Streams.Error[0].Exception;
                    }
                    else
                    {
                        throw new RuntimeException(NewExoPSSession.GetPowerShellError(powershell));
                    }
                }
    }
}

Expected behavior

Calling New-ExoPSSession should succeed in creating a remote pssession.

Actual behavior

New-ExoPSSession : Connecting to remote server outlook.office365.com failed with the following error message : UNKNOWN ERROR: The WS-Management service cannot process the request because the XML is invalid.  For more information, see the about_Remote_Troubleshooting Help topic.

> $e.Exception.ToString()
System.Management.Automation.Remoting.PSRemotingTransportException: Connecting to remote server outlook.office365.com failed with the following error message : UNKNOWN ERROR: The WS-Management service cannot process the request because the XML is invalid.  For more information, see the about_Remote_Troubleshooting Help topic.
   at Microsoft.Exchange.Management.ExoPowershellSnapin.NewExoPSSession.ProcessRecord() in C:\git\migration\exchange\PSModuleCore\ExoPowerShellModule\NewExoPSSession.cs:line 300
   at System.Management.Automation.Cmdlet.DoProcessRecord()
   at System.Management.Automation.CommandProcessor.ProcessRecord()

Environment data

Name                           Value
----                           -----
PSVersion                      6.1.0-preview.2
PSEdition                      Core
GitCommitId                    v6.1.0-preview.2
OS                             Linux 4.15.0-1014-azure #14~16.04.1-Ubuntu SMP Thu Jun 14 15:42:55 UTC 2018
Platform                       Unix
PSCompatibleVersions           {1.0, 2.0, 3.0, 4.0...}
PSRemotingProtocolVersion      2.3
SerializationVersion           1.1.0.1
WSManStackVersion              3.0

BellaLi avatar Jul 12 '18 00:07 BellaLi

I found the root cause.

ConnectionUri, used UriBuilder to build the query parameters. It turns out to be sth like: HTTPS://outlook.office365.com:443/PowerShell-LiveId?BasicAuthToOAuthConversion=true&[email protected];PSVersion=6.1.0

The URI format, works fine on Windows because '&' will be automatically replaced by "&". However, on Linux, it won't. As '&' has special meaning in xml, it'll be treated as invalid xml in request.

The fix for me is to change below: uriBuilder.Query = queryParameters.ToString(); to: uriBuilder.Query = queryParameters.ToString().Replace("&", "&");

WinRM on Linux might want to handle this issue, so it won't bug many people!

BellaLi avatar Jul 19 '18 22:07 BellaLi

@BellaLi I've created issue https://github.com/Microsoft/omi/issues/550 in the Microsoft/OMI which is where this logic resides.

dantraMSFT avatar Jul 19 '18 23:07 dantraMSFT