diff --git a/winrm/__init__.py b/winrm/__init__.py index c3ee260..7bb49c2 100644 --- a/winrm/__init__.py +++ b/winrm/__init__.py @@ -69,7 +69,8 @@ def _clean_error_msg(self, msg): try: # remove the namespaces from the xml for easier processing msg_xml = self._strip_namespace(msg_xml) - root = ET.fromstring(msg_xml) + string_xml = msg_xml.decode('437') + root = ET.fromstring(string_xml) # the S node is the error message, find all S nodes nodes = root.findall("./S") new_msg = "" @@ -78,7 +79,8 @@ def _clean_error_msg(self, msg): # the hex chars represent CRLF so we replace with newline new_msg += s.text.replace("_x000D__x000A_", "\n") except Exception as e: - # if any of the above fails, the msg was not true xml + # if any of the above fails, the msg was either not a true xml + # or the decoding failed. # print a warning and return the original string warnings.warn( "There was a problem converting the Powershell error " diff --git a/winrm/tests/test_session.py b/winrm/tests/test_session.py index 875d493..b108585 100644 --- a/winrm/tests/test_session.py +++ b/winrm/tests/test_session.py @@ -81,6 +81,12 @@ def test_decode_clixml_no_errors(): actual = s._clean_error_msg(msg) assert actual == expected +def test_decode_clixml_non_ascii_valid_xml_no_error(): + s = Session('windows-host.example.com', auth=('john.smith', 'secret')) + msg = b'#< CLIXML\r\nSystem.Management.Automation.PSCustomObjectSystem.Object1Module werden f\x81r erstmalige Verwendung vorbereitet.0-1-1Completed-1 ' + expected = b"" + actual = s._clean_error_msg(msg) + assert actual == expected def test_decode_clixml_invalid_xml(): s = Session('windows-host.example.com', auth=('john.smith', 'secret'))