From f47b27e61a9f8b8fd5c04cee5ff50b22b544b0c7 Mon Sep 17 00:00:00 2001 From: Luis Toledo Date: Thu, 9 May 2019 17:44:35 -0400 Subject: [PATCH] possible fix for UnicodeDecodeError: 'utf8' codec can't decode byte --- contents/winrm-exec.py | 6 +++--- contents/winrm_session.py | 4 ++-- 2 files changed, 5 insertions(+), 5 deletions(-) diff --git a/contents/winrm-exec.py b/contents/winrm-exec.py index 080cf40..ce2e53d 100644 --- a/contents/winrm-exec.py +++ b/contents/winrm-exec.py @@ -162,9 +162,9 @@ sys.stderr.seek(lasterrorpos) errorread=session._clean_error_msg(sys.stderr.read()) if isinstance(errorread, str): - realstdout.write(errorread) + realstderr.write(errorread) else: - realstdout.write(errorread.decode(charset)) + realstderr.write(errorread.decode(charset)) lasterrorpos = sys.stderr.tell() except Exception as e: traceback.print_exc(file=sys.stdout) @@ -178,7 +178,7 @@ sys.stderr = realstderr if tsk.e_std: - sys.stderr.write("Execution finished with the following error:\n %s" % tsk.e_std) + sys.stderr.write("Execution finished with the following error") sys.exit(1) else: sys.exit(tsk.stat) \ No newline at end of file diff --git a/contents/winrm_session.py b/contents/winrm_session.py index 45f1d55..0a01841 100644 --- a/contents/winrm_session.py +++ b/contents/winrm_session.py @@ -37,7 +37,7 @@ def run_ps(self, script, out_stream=None, err_stream=None): encoded script command """ # must use utf16 little endian on windows - encoded_ps = b64encode(script.encode('utf_16_le')).decode() + encoded_ps = b64encode(script.encode('utf_16_le')).decode('ascii') rs = self.run_cmd('powershell -encodedcommand {0}'.format(encoded_ps),out_stream=out_stream, err_stream=err_stream) return rs @@ -46,7 +46,7 @@ def run_ps(self, script, out_stream=None, err_stream=None): def _clean_error_msg(self, msg): #data="" if isinstance(msg, bytes): - msg = msg.decode('utf-8') + msg = msg.decode('utf-8', errors="ignore") if msg.startswith("#< CLIXML") or "" in msg: # for proper xml, we need to remove the CLIXML part