如何通过PowerShell以正确的编码输出文件?

问题描述 投票:0回答:1

我正在尝试在 PowerShell 中将日志文件输出到磁盘,但我得到了错误的文件内容。

信息如下:

Windows 10 企业版 LTSC 21H2

PowerShell 版本:

Name                           Value
----                           -----
PSVersion                      7.3.6
PSEdition                      Core
GitCommitId                    7.3.6
OS                             Microsoft Windows 10.0.19044
Platform                       Win32NT
PSCompatibleVersions           {1.0, 2.0, 3.0, 4.0…}
PSRemotingProtocolVersion      2.3
SerializationVersion           1.1.0.1
WSManStackVersion              3.0

默认编码:

Preamble          :
BodyName          : utf-8
EncodingName      : Unicode (UTF-8)
HeaderName        : utf-8
WebName           : utf-8
WindowsCodePage   : 1200
IsBrowserDisplay  : True
IsBrowserSave     : True
IsMailNewsDisplay : True
IsMailNewsSave    : True
IsSingleByte      : False
EncoderFallback   : System.Text.EncoderReplacementFallback
DecoderFallback   : System.Text.DecoderReplacementFallback
IsReadOnly        : True
CodePage          : 65001

场景:

adb logcat WeatherActivity:D *:S > weather.log

the log file outputed in powershell

当我在 git bash 或 mobaxterm 中做同样的事情时,它会得到像这样的正确内容。

the log file outputed in git bash

我尝试过的: 将 PowerShell 版本从 5.1 更改为 7.3.6

PowerShell 7默认使用UTF-8输出文件,所以可能不是这个原因?

我读过的相关工作:

将 PowerShell 的默认输出编码更改为 UTF-8

我期待 PowerShell 给出正确的输出文件。

powershell encoding adb logcat file-encodings
1个回答
0
投票

@mklement0 的答案对我有用。

[Console]::OutputEncoding 必须与 adb 输出中使用的字符编码匹配。如果是 UTF-8,(暂时)设置 [Console]::OutputEncoding = [System.Text.UTF8Encoding]::new()。如果您将 UTF-8 设置为系统范围内的默认编码(如问题中的最后一个链接所示),则没有必要,但此更改会产生深远的影响。 – 马克门0

顺便说一句,这是解决此问题的另一种简单方法,如下所示。

© www.soinside.com 2019 - 2024. All rights reserved.