pywinrm icon indicating copy to clipboard operation
pywinrm copied to clipboard

Exception on a valid XML with non-ASCII character

Open Ferenc- opened this issue 4 years ago • 0 comments

Run the example PS script on a remote machine that is not set to English, but a non-ASCII language. Like in this case German:

Warning: there was a problem converting the Powershell error message: not well-formed (invalid token): line 1, column 205
b'#< CLIXML\r\n<Objs Version="1.1.0.1" xmlns="http://schemas.microsoft.com/powershell/2004/04"><Obj S="progress" RefId="0"><TN RefId="0"><T>System.Management.Automation.PSCustomObject</T><T>System.Object</T></TN><MS><I64 N="SourceId">1</I64><PR N="Record"><AV>Module werden f\x81r erstmalige Verwendung vorbereitet.</AV><AI>0</AI><Nil /><PI>-1</PI><PC>-1</PC><T>Completed</T><SR>-1</SR><SD> </SD></PR></MS></Obj></Objs>'

The XML part of the message is a valid XML. Despite what the comment here assumes:

            except Exception as e:
                # if any of the above fails, the msg was not true xml

This seems like a false assumption, just like that we can call fromstring like this:

root = ET.fromstring(msg_xml)

when msg_xml is not a string, but an undecoded byte array, that ElementTree can't always deal with.

My suggestion is to decode it into a string first. In this case CP 437 definitely works, and I have seen that the pywinrm codebase uses CP 437 as a default codepage and also for encoding input, so I would risk going with that by default:

string_xml = msg_xml.decode('437')
root = ET.fromstring(string_xml)

Ferenc- avatar Sep 29 '21 18:09 Ferenc-