How to get out of PowerShell's encoding hell?

China☆狼群 提交于 2021-02-19 05:44:05

问题


> cat .\foo.txt
abc

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

> md5sum foo.txt
b79545611b3be30f90a0d21ef69bca82 *foo.txt

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

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.

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...).

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)

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


回答1:


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

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

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




回答2:


If you're using the Get-Content cmdlet, then follow the recommendation given at https://technet.microsoft.com/en-us/library/hh847788.aspx for dealing with binary data:

When reading from and writing to binary files, use a value of Byte for the Encoding dynamic parameter and a value of 0 for the ReadCount parameter.

Regardless of whether or not you're using Get-Content, you'll probably want to avoid ever having your data represented as a String. The String type is designed for character data, and doesn't do well for handling binary data.



来源:https://stackoverflow.com/questions/40479515/how-to-get-out-of-powershells-encoding-hell

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!