I've been trying to track down an odd encoding issue with artifacts coming out of GitLab.
One XML file was going in as UTF8
and coming out as UCS-2 LE BOM
after a stack of testing I'm genuinely shocked to discover it's PowerShell doing the damage.
The powershell script is even running on a Windows box!! I have this code in a script:
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
}
}
And I know I need to specify an encoding. From looking at other answers on SO I should be able to do this but I just cannot find the right syntax.
I've tried:
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
}
}
As per the other examples but that just results in empty files.
How can I stop powershell from breaking my files and setting the right encoding? I'm running PS 5.1