1

I'm executing a powershell script (which talks to remote machine) using subprocess. The issue is that I'm unable to fetch/decode the correct text as expected.

The special characters are wrongly decoded/encoded somewhere during the opeation while using subprocess

When I execute the powershell script directly from console the data is intact (though it is not displayed correctly).

enter image description here

If I copy the output and paste it in notepad I can see the correct text. So no issues while executing manually.

enter image description here

Now, when executing this using subprocess, the output data is incorrect.

import subprocess

process = subprocess.Popen(
    ['powershell', 'd:\\files_list.ps1'],
    stdin=subprocess.PIPE,
    stdout=subprocess.PIPE,
    stderr=subprocess.PIPE,
    shell=True
)
output, error = process.communicate()
print(output.decode('utf-8', 'ignore'))

enter image description here

I tried various decoding options (cp1252, cp437), but I'm not able to get the correct text for both the lines. Using the default utf-8 throws exception while decoding the byte stream.

UnicodeDecodeError: 'utf-8' codec can't decode byte 0x85 in position 40: invalid start byte

My guess is that the stdout byte stream itself is incorrect. Below is the value of output bytestring. We can see that the first line already comes with ? mark.

b'H:\\sc\\tamil ????????? - Copy.txt\r\nH:\\sc\\\x85, \x8a, \x8d, \x95, \x97,\x85, \x8a, \x8d, \x95.txt\r\n'

Can someone please help on the capturing the correct output ?

Aakash Chakravarthy
  • 10,523
  • 18
  • 61
  • 78

0 Answers0