【发布时间】: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 - 都是 stripped 当
Get-Content返回输入文件的行作为一个数组。在稍后使用Out-File(或Set-Content,...)的输出中,各个字符串与 platform-appropriate 换行序列连接,这在 Windows 上意味着您最终将使用 CRLF-newline文件。
标签: powershell