是否可以使用重定向运算符将输出重定向到文件,而无需在Powershell中编写字节顺序标记? [英] Is it possible to redirect output to a file using the redirection operator without writing a byte-order mark in Powershell?

查看:105
本文介绍了是否可以使用重定向运算符将输出重定向到文件,而无需在Powershell中编写字节顺序标记?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在将输出流重定向到文件时,是否有任何方法可以省略字节顺序标记?例如,如果我要获取XML文件的内容并将字符串替换为新值,则需要创建新的编码并将新的输出写入类似以下内容的文件: /p>

$newContent = ( Get-Content .\settings.xml ) -replace 'expression', 'newvalue'
$UTF8NoBom = New-Object System.Text.UTF8Encoding( $false )
[System.IO.File]::WriteAllText( '.\settings.xml', $newContent, $UTF8NoBom )

我也尝试使用Out-File,但是将UTF8指定为编码仍包含BOM表:

( Get-Content .\settings.xml ) -replace 'expression', 'newvalue' | Out-File -Encoding 'UTF8' .\settings.xml

我想做的就是简单地重定向到没有BOM的文件:

( Get-Content .\settings.xml ) -replace 'expression, 'newvalue' > settings.xml

问题是,从其他应用程序读取这些文件时,添加到输出文件中的BOM通常会引起问题(最明显的是,如果我修改XML并以BOM表开头,大多数读取XML的应用程序都会爆炸, Chef Client也不喜欢JSON属性文件中的BOM.缺少编写Write-FileWithoutBom之类的函数来接受管道输入和输出路径的方法,我有什么办法可以在将输出重定向到文件时简单地关闭"编写BOM?

解决方案不一定必须使用重定向运算符.如果有内置的cmdlet,我可以使用它输出到没有BOM的文件,那也是可以接受的.

解决方案

  • 从v5.1开始的 Windows PowerShell 中,没有没有内置方法来避免使用UTF- 8种编码方式 (如您所演示的那样,直接调用.NET框架不足).

    • 在v5.1 +中,您可以按以下方式更改>/>>的默认编码,但是,如果选择utf8,则 still 会得到BOM: /p>

      • $PSDefaultParameterValues['Out-File:Encoding'] = 'utf8'
    • 考虑使用我的答案中的第三方功能Out-FileUtf8NoBom.

    • 不幸的是, Windows PowerShell 不太可能不会支持创建无BOM的UTF-8文件 [1] ,但希望PowerShell Core 不仅支持该功能,而且默认将无BOM的文件解释为UTF-8(参见下文),最终将在Windows上成为可行的替代方案.

  • 相比之下,
  • PowerShell Core 默认情况下使用 BOM-less UTF-8 (都适用于Out-File/>Set-Content),并通过-Encoding指示符utf8utf8BOM为您提供BOM或无BOM的选择.


[1]来自 Windows PowerShell 5.1 ,与.NET Framework 4.x一样,将继续构建-in Windows 10和Windows Server 2016的受支持组件.但是,它可能不会收到主要功能更新或较低优先级的错误修复..",并在注释中,"目标使用PowerShell Core 6.0 ,并且所有兼容性修补程序都是在PowerShell Core上融合生态系统时取代Windows PowerShell 6.0的需求.所以,目前,我们没有任何功能计划制作Windows PowerShell 6.0."

Is there any way to omit the byte-order mark when redirecting the output stream to a file? For example, if I want to take the contents of an XML file and replace a string with a new value, I need to do create a new encoding and write the new output to a file like the following which is rather ham-handed:

$newContent = ( Get-Content .\settings.xml ) -replace 'expression', 'newvalue'
$UTF8NoBom = New-Object System.Text.UTF8Encoding( $false )
[System.IO.File]::WriteAllText( '.\settings.xml', $newContent, $UTF8NoBom )

I have also tried using Out-File, but specifying UTF8 as the encoding still contains a BOM:

( Get-Content .\settings.xml ) -replace 'expression', 'newvalue' | Out-File -Encoding 'UTF8' .\settings.xml

What I want to be able to do is simply redirect to a file without a BOM:

( Get-Content .\settings.xml ) -replace 'expression, 'newvalue' > settings.xml

The problem is that the BOM which is added to the output file routinely cause issues when reading these files from other applications (most notably, most applications which read an XML blow up if I modify the XML and it begins with a BOM, Chef Client also doesn't like a BOM in a JSON attributes file). Short of me writing a function like Write-FileWithoutBom to accept pipeline input and an output path, is there any way I can simply "turn off" writing a BOM when redirecting output to a file?

The solution doesn't necessarily have to use the redirection operator. If there is a built-in cmdlet I can use to output to a file without a BOM, that would be acceptable as well.

解决方案


[1] From a Microsoft blog post, emphasis added: "Windows PowerShell 5.1, much like .NET Framework 4.x, will continue to be a built-in, supported component of Windows 10 and Windows Server 2016. However, it will likely not receive major feature updates or lower-priority bug fixes." and, in a comment, "The goal with PowerShell Core 6.0 and all the compatibility shims is to supplant the need for Windows PowerShell 6.0 while converging the ecosystem on PowerShell Core. So no, we currently don’t have any plans to do a Windows PowerShell 6.0."

这篇关于是否可以使用重定向运算符将输出重定向到文件,而无需在Powershell中编写字节顺序标记?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆