GitLab脚本中的PowerShell编码

GitLab脚本中的PowerShell编码,powershell,Powershell,我一直在试图找出一个奇怪的编码问题,即GitLab中出现的工件 一个XML文件以UTF8的形式输入,经过一系列测试后以UCS-2 LE BOM的形式输出,我真的很震惊地发现是它的PowerShell造成了损坏 powershell脚本甚至在Windows机箱上运行!!我在脚本中有以下代码: function Update-SourceDataFileVersion { Param ([string]$Version) foreach ($o in $input) { Wr

我一直在试图找出一个奇怪的编码问题,即GitLab中出现的工件

一个XML文件以
UTF8
的形式输入,经过一系列测试后以
UCS-2 LE BOM
的形式输出,我真的很震惊地发现是它的PowerShell造成了损坏

powershell脚本甚至在Windows机箱上运行!!我在脚本中有以下代码:

function Update-SourceDataFileVersion
{
  Param ([string]$Version)

  foreach ($o in $input) 
  {
    Write-output $o.FullName 
    $TmpFile = $o.FullName + ".tmp" 

     get-content $o.FullName | 
        %{$_ -replace 'x.x.x.x', $Version } > $TmpFile

     move-item $TmpFile $o.FullName -force
  }
}
我知道我需要指定一个编码。从其他答案来看,我应该能够做到这一点,但我就是找不到正确的语法

我试过:

function Update-SourceDataFileVersion
{
  Param ([string]$Version)

  foreach ($o in $input) 
  {
    Write-output -Encoding utf8 $o.FullName 
    $TmpFile = $o.FullName + ".tmp" 

     get-content -Encoding utf8 $o.FullName | 
        %{$_ -replace 'x.x.x.x', $Version } > $TmpFile -Encoding utf8

     move-item $TmpFile $o.FullName -force
  }
}
根据其他示例,但这只会导致空文件


如何阻止powershell破坏我的文件并设置正确的编码?我正在运行PS 5.1,在您的示例中,您正在使用重定向
将输出保存到文件中<代码>>它是一个操作员,不支持选项。因此,设置编码没有任何区别

相反,您希望使用


顺便说一句:我认为你用错了:它是用来沿着管道传递对象,而不是写入文件。如果要记录文件名,则应使用
Write Host
代替

Good。我不怎么碰PS,有那么多问题。我发现这也很有效
$outpunecoding=[System.Console]::outpunecoding=[System.Console]::inpunecoding=[System.Text.Encoding]::UTF8$PSDefaultParameterValues['*:Encoding']='UTF8'
当你读到“>”是xxxx的快捷方式时,这并不明显。但它并没有告诉你关键的警告,你不能使用选项,这只会导致混乱。谢谢你的帮助!不客气。关于快捷方式:我养成了不使用它们的习惯。它变得更有价值,但它从一些问题中节省了很多。不幸的是,Windows PowerShell与默认字符编码不一致,不像PowerShell(Core)7+,后者现在一直默认为无BOM的UTF-8-请参阅。另一方面,如果输入对象是字符串,则使用它比使用/
,,在编写大型文件时,这可能很重要-请参阅。注意:Windows PowerShell的
设置内容
默认为活动的ANSI代码页。不幸的是,Windows PowerShell与PowerShell(Core)7+不同,后者现在一直默认为无BOM的UTF-8。请注意,在首先执行
$PSDefaultParameterValues['*:Encoding']='utf8'
时,Windows PowerShell v5.1的
操作员可以生成UTF-8文件,但它们总是有一个BOM表-请参阅。
function Update-SourceDataFileVersion
{
  Param ([string]$Version)

  foreach ($o in $input) 
  {
    $TmpFile = $o.FullName + ".tmp" 

     get-content -Encoding utf8 $o.FullName | `
        %{$_ -replace 'x.x.x.x', $Version } | `
        Out-File -FilePath $TmpFile -Encoding utf8

     move-item $TmpFile $o.FullName -force
  }
}