如何更改Powershell脚本,使其以ANSI-Windows-1252编码写入文件?

K9-*_*Guy 2 powershell

我有一个银行业务应用程序脚本,该脚本通过从每日输入银行文件中删除错误记录来生成“过滤的”输出文件(请参阅如何创建Windows Server脚本以从文件中删除错误记录以及以前的记录到每个文件中)。结果写入新文件)。“过滤后的”输出文件将被发送到国家,以更新其系统。附带说明一下,我们从银行收到的原始输入文件在我的文件编辑器(UltraEdit)中显示为Unix 1252(ANSI Latin 1),并且每个记录仅以换行结尾。

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

果然……我回想起来,该州的系统编码不正确。他们的评论是:“该文件编码为UCS-2 Little Endian,需要转换为ANSI才能在我们的系统上运行。真是出乎意料。

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

看来已处理妥当,但我们必须进行一些转换。可以在ANSI中发送,还是需要在UCS 2 Little Endian中完成?”

我尝试将–Encoding“ Windows-1252”和–Encoding Windows-1252添加到我的文件外语句失败,但都返回了以下消息:Out-File:无法验证参数'Encoding'的参数。参数“ Windows-1252”不属于ValidateSet属性指定的“未知,字符串,unicode,bigendianunicode,utf8,utf7,utf32,ascii,default,oem”集合。提供集合中的参数,然后再次尝试命令。在C:\ EZTRIEVE \ PwrShell \ TEST2_FilterR02.ps1:47 char:57 + ... 输出文件$ OutputFileFiltered -Encoding“ Windows-1252” + ~~~~~~~~~~~~~ + + CategoryInfo:InvalidData:(:) [输出文件],ParameterBindingVal idationException + FullyQualifiedErrorId:ParameterArgumentValidationError,

几天来,我一直在寻求帮助,但实际上还不清楚,我发现的绝大多数涉及从Windows-1252转换为另一种编码。昨天,我在stackoverflow的某处发现“ ANSI”与Windows-1252相同,但到目前为止,我还没有发现任何东西可以向我显示如何正确地将Windows-1252编码选项附加到我的文件外语句中,因此Powershell将接受它。我真的需要完成这个项目,这样我才能解决已经添加到队列中的下几个问题。是否可能缺少我需要添加到–Encoding的子参数?

这是在运行Windows Server 2016 Standard和Powershell 5.1的新备份服务器上的Dollar Universe(作业计划程序)下进行的测试。我们的生产系统也在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
Run Code Online (Sandbox Code Playgroud)

mkl*_*nt0 5

注意:

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

  • 下一节将按照最初提出的问题进行处理,并提供解决方案,这些解决方案会导致文件带有CRLF(Windows风格)换行符(在Windows上运行时)。


如果系统的非Unicode程序语言设置(也称为系统语言环境)恰好具有Windows-1252作为活动ANSI代码页(例如,在美国英语或西欧系统上),请使用-Encoding Default,因为在中Default引用该代码页Windows PowerShell(但不是在PowerShell Core中,默认情况下为无BOM的UTF-8,并且不支持Default编码标识符)。

验证: [cultureinfo]::CurrentCulture.TextInfo.ANSICodePage -eq 1252

... | Out-File -Encoding Default $file
Run Code Online (Sandbox Code Playgroud)

注意:

  • 如果你确信你的数据是实际的ASCII码字符范围内专门组成(与7位范围内,不包括重音字符,如代码点字符ü),-Encoding Default即使你的系统区域设置使用ANSI代码页将工作比Windows-1252,假设所有(单字节)ANSI代码页在其7位子范围内共享所有ASCII字符;您也可以使用-Encoding ASCII,但是请注意,如果毕竟存在非ASCII字符,它们将被音译为文字?字符。从而导致信息丢失。

  • Set-Contentcmdlet实际上默认DefaultWindows PowerShell中的编码(但不是PowerShell Core,其一致的默认值为没有BOM的UTF-8)。

  • 虽然Set-Content的字串化行为与Out-File- 的字符串化行为不同- 请参见此答案 -如果要写入文件的对象已经字符串,则实际上是更好的选择。


否则,您有两个选择:

  • 直接使用.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))
    
    Run Code Online (Sandbox Code Playgroud)
  • 使用PowerShell Core,它允许您将任何受支持的.NET编码传递给
    -Encoding参数:

    ... | Out-File -Encoding ([Text.Encoding]::GetEncoding(1252)) $file
    
    Run Code Online (Sandbox Code Playgroud)

请注意,在PSv5.1 +中,您实际上可以更改>>>运算符使用的编码,如本答案所述
然而,在Windows PowerShell中你又受限于所支持的编码Out-File-Encoding参数。


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

[Environment]::NewLine在将字符串作为行写入文件时,PowerShell(始终)和.NET(默认情况下)都使用适合平台的换行符序列(如所反映的那样)。换句话说:在Windows上,您将最终获得带有CRLF换行符的文件,而在带有Unix的类似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
Run Code Online (Sandbox Code Playgroud)

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

注意:

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

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

    -Encoding ([Text.Encoding]::GetEncoding([cultureinfo]::CurrentCulture.TextInfo.ANSICodePage))
    
    Run Code Online (Sandbox Code Playgroud)

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")
Run Code Online (Sandbox Code Playgroud)

注意:

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

  • 如果要改用活动的ANSI代码页,请[Text.Encoding]::GetEncoding([cultureinfo]::CurrentCulture.TextInfo.ANSICodePage)作为附加参数传递给WriteAllText()