我向州发送了从“干净”(无错误)和“肮脏”(包含4个错误)的输入文件生成的几个测试输出文件进行测试,以确保在实施之前一切正常,但是有点担心,因为输出文件是以UTF-16编码并带有CRLF行结尾,而输入和当前未过滤的输出都以Windows-1252编码。此系统上的所有其他输出文件都是Windows-1252编码。
果然...我得到了回复,说编码对于国家的系统不正确。他们的评论是: “该文件已编码为UCS-2 Little Endian,需要转换为ANSI才能在我们的系统上运行。那是出乎意料的。”
“没有详细交易的文件会通过我们的EFT拒绝程序运行。”
“它似乎已经被处理了,但我们必须做一些转换。可以发送ANSI格式还是需要以UCS 2 Little Endian格式完成?”
我尝试不成功地将-编码“Windows-1252”和-编码windows-1252添加到我的out-file语句中,两者都返回以下消息: Out-File:无法验证参数'Encoding'。 参数“Windows-1252”不属于由ValidateSet属性指定的集合“unknown,string,unicode,bigendianunicode,utf8,utf7,utf32,ascii,默认值,oem”。 提供一个在集合中的参数,然后再尝试命令。 在C:\ EZTRIEVE \ PwrShell \ TEST2_FilterR02.ps1:47 char:57 + ... OutputStrings | Out-File $OutputFileFiltered -Encoding "Windows-1252" + ~~~~~~~~~~~~~~ + CategoryInfo:InvalidData:(:) [Out-File],ParameterBindingVal idationException + FullyQualifiedErrorId:ParameterArgumentValidationError,Microsoft.Power Shell.Commands.OutFileCommand
我已经仔细寻找了几天关于这个问题的帮助,但是没有什么内容真正清晰明了,而且我发现大部分都是将编码从Windows-1252转换为其他编码。昨天我在stackoverflow的某个评论中发现“ANSI”与Windows-1252相同,但到目前为止,我还没有找到任何东西来展示如何正确地将Windows-1252编码选项附加到我的out-file语句中以便Powershell接受它。我真的需要完成这个项目,这样我才能处理我的队列中添加的下几个项目。是否可能有一个子参数我错过了,需要附加到-Encoding?
此内容在运行Windows Server 2016标准版和Powershell 5.1的新备份服务器上,在Dollar Universe(任务调度程序)下进行测试。我们的生产系统在运行Windows Server 2012 R2上的Dollar Universe,也使用Powershell 5.1。(是的,我们正在寻找足够的升级时间窗口 :-))
截至最后一次尝试,我的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
n","
r`n") | Out-File $OutputFileFiltered会发生什么情况? 如果您包括一个可运行的PowerShell脚本示例,那么最终获取帮助会更容易。 - A. LionGet-Content
返回输入文件的行作为数组时,换行符(无论是LF-only还是CRLF)都会被_剥离_。在稍后使用Out-File
(或Set-Content
等)输出时,单个字符串将与_适用于平台的_换行序列连接,这意味着在Windows上你最终会得到CRLF换行符的文件。 - mklement0Out-File
(和Set-Content
)会给你CRLF换行符。PowerShell Core表现出相同的行为,因为换行符的行为与操作系统相关,而不是与PowerShell版本相关。我的答案底部部分向您展示了如何无条件地创建LF文件。您尝试过这样做了吗?有什么问题吗? - mklement0