Skip to content

Commit

Permalink
possible fix for UnicodeDecodeError: 'utf8' codec can't decode byte
Browse files Browse the repository at this point in the history
  • Loading branch information
ltamaster committed May 9, 2019
1 parent bbd7fb2 commit f47b27e
Show file tree
Hide file tree
Showing 2 changed files with 5 additions and 5 deletions.
6 changes: 3 additions & 3 deletions contents/winrm-exec.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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)
4 changes: 2 additions & 2 deletions contents/winrm_session.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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 "<Objs Version=" in msg or "-1</PI><PC>" in msg:
# for proper xml, we need to remove the CLIXML part
Expand Down

0 comments on commit f47b27e

Please sign in to comment.