0

I am trying to collect the names of the Scheduled tasks in Python using subprocess

import subprocess
import sys

encoding = 'utf-8'

cmd = r'''$env:PYTHONIOENCODING = "%s";py -3 -c "print('® ¾ ü_ä_ö')"'''% encoding
#cmd = r'''$env:PYTHONIOENCODING = "%s"; schtasks /query ''' % encoding

data = subprocess.check_output(["powershell", "-C",cmd])
print((data.decode(encoding)))

This works fine when I do the dummy cmd (print the Unicode). But when I try to run the schtasks command (some task like intel and others uses unicode symbols like ® in the task name or characters like ü_ä_ö ).

This gives me the following error:

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

If I run the command from cmd prompt or powershell directly it shows fine:

C:\Users\ricar\Google Drive\Bifrost\Collectors>schtasks /query

Folder: \
TaskName                                 Next Run Time          Status
======================================== ====================== ===============
Adobe Acrobat Update Task                12/4/2020 8:00:00 AM   Ready
AdobeAAMüpdater-1.0-MicrosoftAccount-ric 12/4/2020 2:00:00 AM   Ready
AdobeGCInvoker-1.0                       12/5/2020 12:30:00 AM  Ready
HPPSDrTelemetryWatch©                    12/12/2020 12:00:00 AM Ready
Intel-IMSS®                              N/A                    Ready

Any ideas what I am doing wrong?

Thanks

  • 1
    Does this answer your question? [Changing PowerShell's default output encoding to UTF-8](https://stackoverflow.com/questions/40098771/changing-powershells-default-output-encoding-to-utf-8) – Masklinn Dec 04 '20 at 07:05
  • I have to go all the way back to Powershell 3.0 to make sure my solution works with Windows 7 and Windows 2008, so PS5.1 solution will not work here. :-( – Ricardo Abech Dec 06 '20 at 15:34

1 Answers1

0

Are you sure the schtasks output is in utf-8?

0x81 is ü in the IBM CP437 and IBM CP850 / IBM CP858 encodings.

In order to check this, the pragmatic way is to print out the string with repr() or with one of the decode(encoding, errors=...) options that outputs character codes (eg. decode(encoding, errors='xmlcharrefreplace')), then match it up with tables of encodings to see which one matches.

Jiří Baum
  • 6,697
  • 2
  • 17
  • 17
  • Yes, it is (well at least on WIndows 10 and windows 7). I can see on the prompt the symbols (but nor if I redirected to a file using > out.txt). I am just trying to list the active tasks and then read the files under C:\windows\system32\tasks. But the file name will also contain the unicode symbols, making impossible to find the file). – Ricardo Abech Dec 06 '20 at 15:32
  • 1
    The issue was the schtasks command verbose (/v) option. The verbose caused the output to be converted to ANSII and not able to encode/decode properly. So I end up having to change the approach by reading the files at C:\windows\system32\tasks first, then getting the extra properties (LastRun,m Next run, etc) from SCHTASKS without the /v option. Since this was a guide to the right direction I am accepting as the correct answer. Thanks – Ricardo Abech Dec 07 '20 at 19:04