【问题标题】:How do I change my Powershell script so that it writes out-file in ANSI - Windows-1252 encoding?如何更改我的 Powershell 脚本,以便它以 ANSI - Windows-1252 编码写入输出文件?
【发布时间】:2019-08-11 09:45:09
【问题描述】:

我有一个银行应用程序脚本,它通过从每日输入银行文件中删除错误记录来生成“过滤”输出文件(请参阅How do I create a Windows Server script to remove error records, AND the previous record to each, from a file with the results written to a NEW file)。 “过滤”的输出文件将被发送到州以更新其系统。附带说明一下,我们从银行收到的原始输入文件在我的文件编辑器 (UltraEdit) 中显示为 Unix 1252 (ANSI Latin 1),并且每条记录仅以换行符结束。

我将“干净”(无错误)和“脏”(包含 4 个错误)输入文件生成的几个测试输出文件发送到州进行测试,以确保在实施之前一切正常,但是有点担心,因为输出文件是用带有 CRLF 行结尾的 UTF-16 编码生成的,其中输入和当前未过滤的输出在 Windows-1252 中编码。此系统上的所有其他输出文件都是 Windows-1252 编码的。

果然……我收到消息说该州的系统编码不正确。他们的cmets是: “该文件采用 UCS-2 Little Endian 编码,需要转换为 ANSI 才能在我们的系统上运行。这是出乎意料的。

之后,没有详细交易的文件将通过我们的 EFT 拒绝程序运行。

它似乎处理得很好,但我们必须做一些转换。可以用 ANSI 发送还是需要在 UCS 2 Little Endian 中完成?”

我尝试将 –Encoding “Windows-1252” 和 –Encoding windows-1252 添加到我的 out-file 语句中,但均返回消息: 输出文件:无法验证参数“编码”的参数。论据 “Windows-1252”不属于该集合 "未知,字符串,unicode,bigendianunicode,utf8,utf7,utf32,ascii,默认,oem" 由 ValidateSet 属性指定。提供集合中的参数 然后再次尝试该命令。 在 C:\EZTRIEVE\PwrShell\TEST2_FilterR02.ps1:47 char:57 + ... 输出字符串 |输出文件 $OutputFileFiltered -编码“Windows-1252” + ~~~~~~~~~~~~~~ + CategoryInfo : InvalidData: (:) [Out-File], ParameterBindingVal idationException + FullyQualifiedErrorId : ParameterArgumentValidationError,Microsoft.Power Shell.Commands.OutFileCommand

几天来,我一直在寻找一些帮助,但没有什么是真正清楚的,而且我发现的绝大多数内容都涉及从 Windows-1252 转换为另一种编码。昨天,我在 stackoverflow 上的某处发现“ANSI”与 Windows-1252 相同的评论,但到目前为止,我还没有找到任何东西告诉我如何正确地将 Windows-1252 编码选项附加到我的输出文件语句中,所以Powershell 将接受它。我真的需要完成这个项目,这样我才能处理接下来添加到我队列中的几个。是否有可能我遗漏了需要附加到 –Encoding 的子参数?

这是在 Dollar Universe(作业调度程序)下在运行 Windows Server 2016 Standard 和 Powershell 5.1 的新备份服务器上进行测试的。我们的生产系统在 Windows Server 2012 R2 和 Powershell 5.1 上运行 Dollar Universe(是的,我们正在寻找足够的升级窗口 :-)

截至我上次尝试时,我的 Powershell 脚本是:

 [cmdletbinding()]
 Param
 (
     [string] $InputFilePath
 )   

 # Read the text file
 $InputFile = Get-Content $InputFilePath

# Initialize output record counter
$Inrecs = 0
$Outrecs = 0

# Get the time
$Time = Get-Date -Format "MM_dd_yy"

# Set up the output file name
$OutputFileFiltered = "C:\EZTRIEVE\CFIS\DATA\TEST_CFI_EFT_RETURN_FILTERED"

# Initialize the variable used to hold the output
$OutputStrings = @()

# Loop through each line in the file
# Check the line ahead for "R02" and add it to the output
# or skip it appropriately
for ($i = 0; $i -lt $InputFile.Length - 1; $i++)
{
    if ($InputFile[$i + 1] -notmatch "R02")
    {
        # The next record does not contain "R02", increment count and add it to the output
        $Outrecs++
        $OutputStrings += $InputFile[$i]
    }
    else
    {
        # The next record does contain "R02", skip it
        $i++
    }
}

# Add the trailer record to the output
$OutputString += $InputFile[$InputFile.Length - 1]

# Write the output to a file
# $OutputStrings | Out-File $OutputFileFiltered
$OutputStrings | Out-File $OutputFileFiltered -Encoding windows-1252

# Display record processing stats:

$Filtered = $Outrecs-$i

Write-Host $i  Input records processed

Write-Host $Filtered  Error records filtered out

Write-Host $Outrecs  Output records written

【问题讨论】:

  • 我记得在我的搜索过程中,有一个问题的评论,与我的问题有点相关,关于更改控制台输出的编码会影响 Powershell 的输出...
  • 我不认为有像 Unix CP252 和 Windows CP252 这样的东西,而是格式相同,只是一个 CP252,在第一个上,行仅使用换行符分隔而不是通常的回车+换行。因此,要“转换”,您需要将特殊字符 \r\n 替换为 \n 如果应用 $OutputStrings.ToString().Replace("n","r`n") | 会发生什么Out-File $OutputFileFiltered 如果您包含一个随时可运行的 powershell 脚本示例,最终将更容易获得帮助。
  • @AdminOfThings:只有当字符串碰巧不包含 7 位 ASCII 范围之外的任何字符时,它才会起作用;任何 8 位 ANSI 字符。 (例如,重音字符。如ü)会音译为literal ? 字符,导致信息丢失。
  • @K9-Guy:console 的 编码在这里不起作用,只有 PowerShell 的默认文件输出编码,您可以使用 -Encoding 更改它。如果您碰巧在使用 Windows-1252 作为活动 ANSI 代码页的系统上,只需使用 -Encoding Default;如果没有,则需要做更多工作 - 请参阅我的答案。
  • @P.Lion:你说得对,只有一个Windows-1252 code page。然而,Unix 风格的 LF-only 换行在这里 不是 问题(它们很少出现在 PowerShell 中,它同样识别 LF 和 CRLF 换行):换行 - 无论是 LF-only 还是 CRLF - 都是 strippedGet-Content 返回输入文件的行作为一个数组。在稍后使用Out-File(或Set-Content,...)的输出中,各个字符串与 platform-appropriate 换行序列连接,这在 Windows 上意味着您最终将使用 CRLF-newline文件。

标签: powershell


【解决方案1】:

注意:

  • 您后来澄清说您需要 LF(Unix 风格)换行符 - 请参阅底部部分。

  • 下一部分处理最初提出的问题,并提供导致文件带有 CRLF(Windows 样式)换行符(在 Windows 上运行时)的解决方案。


如果您的系统的Language for non-Unicode programs setting(又名系统语言环境)恰好有Windows-1252作为活动的ANSI代码页(例如, 在美国英语或西欧系统上),使用 -Encoding Default,因为 Default 指的是 Windows PowerShell 中的代码页(但不是 em> 在 PowerShell Core 中,默认为无 BOM 的 UTF-8,不支持 Default 编码标识符)。

验证:(Get-ItemPropertyValue HKLM:\SYSTEM\CurrentControlSet\Control\Nls\CodePage ACP) -eq '1252'

... | Out-File -Encoding Default $file

注意:

  • 如果您确定您的数据实际上完全由 ASCII 范围字符(代码点在 7 位范围内的字符,不包括 ü 等重音字符)组成,@即使您的系统区域设置使用 Windows-1252 以外的 ANSI 代码页其他,987654331@ 也可以工作,因为所有(单字节)ANSI 代码页共享其 7 位子范围中的所有 ASCII 字符;然后您也可以使用-Encoding ASCII,但请注意,如果毕竟存在非ASCII字符,它们将被音译为literal ?字符。导致信息丢失。 p>

  • Set-Content cmdlet 实际上默认为 Windows PowerShell 中的Default 编码(但不是 PowerShell Core,其中一致的默认值是 UTF-8 没有 BOM) .

  • 虽然Set-Content 的字符串化行为与Out-File - see this answer 的不同 - 如果要写入文件的对象已经字符串,这实际上是更好的选择。


否则,您有两种选择:

  • 直接使用 .NET Framework 文件 I/O 功能,您可以在其中使用 .NET 支持的任何编码;例如:

      $lines = ...  # array of strings (to become lines in a file)
      # CAVEAT: Be sure to specify an *absolute file path* in $file,
      #         because .NET typically has a different working dir.
      [IO.File]::WriteAllLines($file, $lines, [Text.Encoding]::GetEncoding(1252))
    
  • 使用 PowerShell Core,它允许您将任何支持的 .NET 编码传递给
    -Encoding 参数:

      ... | Out-File -Encoding ([Text.Encoding]::GetEncoding(1252)) $file
    

请注意,在 PSv5.1+ 中,您实际上可以更改>>> 运算符使用的编码,详见this answer。 但是,在 Windows PowerShell 中,您再次受限于 Out-File-Encoding 参数支持的编码。


在 Windows 上使用 LF(Unix 风格)换行符创建文本文件:

PowerShell(总是)和 .NET(默认情况下)在将字符串作为行写入文件时使用适合平台的换行符序列 - 正如 [Environment]::NewLine 中所反映的那样。 换句话说:在 Windows 上,您最终会得到带有 CRLF 换行符的文件,而在类 Unix 平台(PowerShell Core)上,您会得到带有 LF 换行符的文件。

请注意,以下解决方案假定要写入文件的数据是一个字符串数组,它们表示要写入的行,例如由Get-Content 返回(其中结果数组元素是输入文件的行,没有尾随的换行序列)。

在 Windows (PSv5+) 上显式创建带有 LF 换行符的文件:

$lines = ...  # array of strings (to become lines in a file)

($lines -join "`n") + "`n" | Set-Content -NoNewline $file

"`n" 产生一个 LF 字符。

注意:

  • 在 Windows PowerShell 中,这隐式使用活动 ANSI 代码页的编码。

  • 在 PowerShell Core 中,这会隐式创建一个没有 BOM 的 UTF-8 文件。如果您想改用活动的 ANSI 代码页,请使用:

    -Encoding ([Text.Encoding]::GetEncoding([int] (Get-ItemPropertyValue HKLM:\SYSTEM\CurrentControlSet\Control\Nls\CodePage ACP)))
    

PSv4-(PowerShell 版本 4 或更低版本)中,您必须直接使用 .NET Framework:

$lines = ...  # array of strings (to become lines in a file)


# CAVEAT: Be sure to specify an *absolute file path* in $file,
#         because .NET typically has a different working dir.
[IO.File]::WriteAllText($file, ($lines -join "`n") + "`n")

注意:

  • 在 Windows PowerShell 和 PowerShell Core 中,这都会创建一个没有 BOM 的 UTF-8 文件。

  • 如果您想改用活动的 ANSI 代码页,请将以下内容作为附加参数传递给 WriteAllText()

    ([Text.Encoding]::GetEncoding([int] (Get-ItemPropertyValue HKLM:\SYSTEM\CurrentControlSet\Control\Nls\CodePage ACP)))
    

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2012-10-26
    • 2011-11-03
    • 2017-10-19
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-01-19
    • 1970-01-01
    相关资源
    最近更新 更多