PowerShell:如何对包含Unicode字符的路径执行命令? [英] Powershell: how to execute command for a path containing Unicode characters?

查看:34
本文介绍了PowerShell:如何对包含Unicode字符的路径执行命令?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我尝试在Powershell 5.1中执行命令,但当路径包含Unicode字符时失败。

例如:

(Get-Acl 'E:/test 📚.txt').access

我正在从Node.js运行命令:

let childProcess = require('child_process')
let testProcess = childProcess.spawn('powershell', [])
testProcess.stdin.setEncoding('utf-8')

testProcess.stdout.on('data', (data) => {
  console.log(data.toString())
})

testProcess.stdout.on('error', (error) => {
  console.log(error)
})

// This path is working, I get command output in the console:
// testProcess.stdin.write("(Get-Acl 'E:/test.txt').access
");

// This path is not working. I get nothing in the console
testProcess.stdin.write("(Get-Acl 'E:/test 📚.txt').access
");

我无法使用Powershell 7,因为我正在制作在预装的Powershell上运行命令的Node.js应用程序

更新

此方法似乎有效:

childProcess.spawn(
  'powershell', 
  ['-Command', '(Get-Acl "E:/test 📚.txt").access']
)

推荐答案

使用stdin输入将您的命令提供给Windows PowerShell CLIWindows PowerShell CLI您隐式依赖于系统的活动OEM代码页,因为PowerShell CLI使用该代码页解码通过stdin接收的输入。

相比之下,通过-c(-Command)CLI参数传递命令完全支持Unicode,而不考虑活动的OEM代码页,因此这是一个绕过原始问题的简单替代方案;借用您自己的更新来解决您的问题:

childProcess.spawn(
  'powershell', 
  ['-NoProfile', '-Command', '(Get-Acl "E:/test 📚.txt").access']
)

请注意,我添加了-NoProfile以使调用更可预测/加快,因为此选项禁止加载通常仅与交互式使用相关的配置文件。

这篇关于PowerShell:如何对包含Unicode字符的路径执行命令?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆