Powershell 脚本不写正确的变音符号 - Powershell 本身做

Powershell Script does not Write Correct Umlauts - Powershell itself does

我想转储(稍后使用)我的 SVN 存储库中本地更改文件的路径。问题是,某些文件名中有变音符号(如 ä、ö、ü)。

当我在本地主干文件夹中打开 powershell window 时,我可以执行 svn status 并使用正确的元音符号(在本例中为“ü”)获得结果:

PS C:\trunk> svn status -q
M       Std\ClientComponents\Prüfung.xaml
M       Std\ClientComponents\Prüfung.xaml.cs
M       Std\ClientComponents\PrüfungViewModel.cs

当我在我的 powershell 脚本中执行相同操作时,结果不同。

脚本“DumpChangedFiles.ps1”:

foreach ( $filename in svn status -q ) 
{
  Write-Host $filename
}

结果:

PS C:\trunk> .\DumpChangedFiles.ps1
M       Std\ClientComponents\Pr³fung.xaml
M       Std\ClientComponents\Pr³fung.xaml.cs
M       Std\ClientComponents\Pr³fungViewModel.cs

问:为什么元音变错了?如何获得正确的结果?


十六进制转储:

ef bb bf 4d 20 20 20 20 20 20 20 53 74 64 5c 43 6c 69 65 6e 74 43 6f 6d 70 6f 6e 65 6e 74 73 5c 50 72 c2 b3 66 75 6e 67 6 2e 68 0d 0a 4d 20 20 20 20 20 20 20 53 74 64 5c 43 6c 69 65 6e 74 43 6f 6d 70 6f 6e 65 6e 74 73 5c 50 72 c2 b3 66 75 6e 67 2e 4 0d 6c 7a 6d 20 20 20 20 20 20 20 53 74 64 5c 43 6c 69 65 6e 74 43 6f 6d 70 6f 6e 65 6e 74 73 5c 50 72 c2 b3 66 75 6e 67 56 69 65 77 4d 6e = 6f 64 7 6f 6e 65 6e 64 17=]


这是脚本 DumpChangedFiles 的输出。ps1 与所需命令的输出相比:

PS C:\trunk> .\DumpChangedFiles.ps1
M       Std\ClientComponents\Pr³fung.xaml
M       Std\ClientComponents\Pr³fung.xaml.cs
M       Std\ClientComponents\Pr³fungViewModel.cs

PS C:\trunk> $PSDefaultParameterValues['*:Encoding'] = 'utf8'; svn status -q
M       Std\ClientComponents\Prüfung.xaml
M       Std\ClientComponents\Prüfung.xaml.cs
M       Std\ClientComponents\PrüfungViewModel.cs

SVN--版本的输出是:

PS C:\trunk> svn  --version
svn, version 1.14.0 (r1876290)
   compiled May 24 2020, 17:07:49 on x86-microsoft-windows

Copyright (C) 2020 The Apache Software Foundation.
This software consists of contributions made by many people;
see the NOTICE file for more information.
Subversion is open source software, see http://subversion.apache.org/

The following repository access (RA) modules are available:

* ra_svn : Module for accessing a repository using the svn network protocol.
  - with Cyrus SASL authentication
  - handles 'svn' scheme
* ra_local : Module for accessing a repository on local disk.
  - handles 'file' scheme
* ra_serf : Module for accessing a repository via WebDAV protocol using serf.
  - using serf 1.3.9 (compiled with 1.3.9)
  - handles 'http' scheme
  - handles 'https' scheme

The following authentication credential caches are available:

* Wincrypt cache in C:\Users\reichert\AppData\Roaming\Subversion

问题来自 PowerShell ISE,您脚本中的 svn 命令是通过 PowerShell ISE 执行的,它使用 Windows-1252(或您的默认 windows 语言环境)对其输出进行编码。

您可以执行以下操作以获得正确的输出(检查您的 Windows 语言环境):

[Console]::OutputEncoding = [System.Text.Encoding]::GetEncoding(1252)
foreach ( $filename in svn status -q ) 
{
  Write-Host $filename
}

似乎之前未回答的问题与 ISE 的相同问题有关: Powershell ISE has different codepage from Powershell and I can not change it