Powershell Out-File:强制编码为 Ascii

Jos*_*wer 3 powershell character-encoding

我正在尝试使用 Powershell 规范化一组制表符分隔的日志文件。

这是当前的脚本:

(Get-ChildItem *.csv) |%{
#Store the name of the file & date
$Name = $_.basename
$FileDate = $_.CreationTime
#Prepends the following to each message: unique Identifer, Hostname, date
(Get-Content $_.fullname) -replace "^","AR-LOG|$Name|$FileDate|"|
#Replaces the TAB delimeter with a Pipe delimeter
Foreach-Object {$_ -replace '   ','|'}  |
#Appends the resulting message in ascii
Out-File -append -FilePath normalized.log -Encoding ascii
Run Code Online (Sandbox Code Playgroud)

输入和输出的片段可以在这里看到:

http://pastebin.com/uaQadYUC

如何强制输出文件为 ascii 而不是某种类型的 unicode?

*** 编辑:进一步的故障排除表明输入文件实际上是 windows-1252 编码的,显然 Get-Content 无法本地处理(?)

Bru*_*tte 5

您应该能够在输出文件上使用编码标志,如... | Out-File -encoding ascii myfile.txt. 如果您正在使用append,请确保所有附加使用相同的编码,否则您最终会得到一个无法使用的文件。