Home > Software design >  Calling another script (here: Python) from a Powershell script with non-ASCII-chars in argument
Calling another script (here: Python) from a Powershell script with non-ASCII-chars in argument

Time:12-24

I'm on Windows 10 with Powershell 5.1

printargs.py is:

#! /usr/bin/python3
import sys
for i in range(len(sys.argv)):
    print(sys.argv[i])

Case 1

I have a Windows batch file runme.bat:

chcp 65001
py printargs.py ä

Note: py.exe is the Python launcher for Windows

This is working: I invoke the batch file in a Powershell terminal and I get output

printargs.py
ä

Case 2

Now I want powershell script runme.ps1 doing exactly the same thing:

# What code must go here?
& py printargs.py ä

This is NOT working: Because of some encoding problem I get

printargs.py
ä

I' am aware of this question.

I tried without success:

$OutputEncoding = [console]::InputEncoding = [console]::OutputEncoding = New-Object System.Text.UTF8Encoding

CodePudding user response:

This comes up a lot. Powershell 5.1 can't read utf8 no bom. This will covert the script to utf8 with bom. Powershell 7 can handle utf8 no bom.

(get-content -encoding utf8 script.ps1) | set-content script.ps1 -encoding utf8


# bom tests
(get-content script.ps1 -AsByteStream)[0] -eq 0xef -and 
(get-content script.ps1 -AsByteStream)[1] -eq 0xbb

True


# right side gets converted to string
'239 187' -eq (get-content script.ps1 -AsByteStream)[0..1]

True
  • Related