如何摆脱 PowerShell 的编码地狱? [英] How to get out of PowerShell's encoding hell?

查看:44
本文介绍了如何摆脱 PowerShell 的编码地狱?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

> cat .\foo.txt
abc

> cat .\foo.txt | md5sum
c13b6afecf97ea6b38d21a8f5167fa1e *-

> md5sum foo.txt
b79545611b3be30f90a0d21ef69bca82 *foo.txt

catmd5sum 是 unix 端口(来自 Windows Git 发行版).

cat and md5sum are the unix port (from the Windows Git distribution).

这是我真实用例的一个玩具示例,它将二进制数据通过管道传输到我无法更改的旧版 Python 脚本.由于管道进行编码,二进制文件损坏.

This is a toy example for my real use case which is piping of a binary data to a legacy python script that I can't change. Because of the pipe doing encoding, the binary file becomes corrupted.

我尝试更改 $OutputEncoding[Console]::OutputEncoding 并使用 chcp,但都没有帮助(但也许我是做得不对,这一切都非常复杂......).

I tried changing $OutputEncoding, [Console]::OutputEncoding and using chcp, all didn't help (but maybe I was not doing it right, this is all very convoluted...).

PowerShell 的管道添加换行符 中的实用程序对我不起作用,因为它如何处理流程参数(我需要将一些参数传递给遗留脚本,有些需要引用,但该实用程序将所有参数作为一个字符串接受)

The utility in PowerShell's pipe adds linefeed doesn't work for me because of how it handles the process arguments (I need to pass some argument to the legacy script and some need to be quoted, but the utility accepts all arguments as one string)

我以某种方式告诉 powershell 完全关闭编码并仅表现为 unix/cmd 的最佳解决方案.

The optimal solution for me to somehow tell powershell to turn off encoding completely and just behave as unix/cmd.

推荐答案

除了使用 cmd 运行包括管道在内的命令外,别无他法:

There is no way around it, except to use cmd to run the commands including the pipe:

cmd /c cat.exe .\foo.txt "|" md5sum

注意管道字符被引用,所以它由 cmd 而不是 powershell 解释.

Note the pipe character is quoted, so it is interpreted by cmd and not powershell.

这篇关于如何摆脱 PowerShell 的编码地狱?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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