![](/img/trans.png)
[英]Calling a powershell script from another powershell script and guaranteeing it is UTF8
[英]Calling another script (here: Python) from a Powershell script with non-ASCII-chars in argument
我在装有 Powershell 5.1 的 Windows 10 上
printargs.py
是:
#! /usr/bin/python3
import sys
for i in range(len(sys.argv)):
print(sys.argv[i])
我有一个 Windows 批处理文件runme.bat
:
chcp 65001
py printargs.py ä
注意: py.exe
是 Windows 的 Python 启动器
这是有效的:我在 Powershell 终端中调用批处理文件并获得输出
printargs.py
ä
现在我想要 powershell 脚本runme.ps1
做完全相同的事情:
# What code must go here?
& py printargs.py ä
这不起作用:由于一些编码问题,我得到
printargs.py
ä
我知道这个问题。
我试过没有成功:
$OutputEncoding = [console]::InputEncoding = [console]::OutputEncoding = New-Object System.Text.UTF8Encoding
这经常出现。 Powershell 5.1 无法读取 utf8 no bom。 这会将脚本转换为带有 bom 的 utf8。 Powershell 7 可以处理 utf8 no bom。
(get-content 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
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.