如何在 PowerShell 中使用 here-string 正确写入 pip.ini 和 .condarc 文件?

How to use here-string in PowerShell to write pip.ini and .condarc file properly?

免责声明:我对 PowerShell 越来越习惯了,但对 PowerShell 还是比较缺乏经验。

我想结合使用 PowerShell 和 here-string 语法来编写 pip.ini.condarc 配置文件,以分别使用 Python 包管理器 pip、conda。 使用 .condarc,没有错误,但我认为我必须在 Notepad++ 中再次重写它才能真正使其工作 - 我认为这是一个文件编码问题:

mkdir 'C:\ProgramData\conda\.condarc'
echo @"
show_channel_urls: true
allow_other_channels: false
report_errors: false
remote_read_timeout_secs: 120
"@ > C:\ProgramData\conda\.condarc

由于 [global]:

,下面给出了 pip.ini 的错误
mkdir 'C:\ProgramData\pip\pip.ini'
echo @"
[global]
index = https://xxxx/nexus/repository/xxxx/pypi
index-url = https://xxxx:8443/nexus/repository/xxxx/simple
trusted-host = xxxx:8443
"@ > C:\ProgramData\pip\pip.ini

Get-Content pip.ini 效果很好,但是 pip config list -v returns:

PS C:\Program Files> pip config list -v
Configuration file could not be loaded.
File contains no section headers.
file: 'C:\ProgramData\pip\pip.ini', line: 1
'ÿþ[\x00g\x00l\x00o\x00b\x00a\x00l\x00]\x00\n'

备注:xxxx代表敏感的公司信息,因此替换真实文本。

我也试过用`转义方括号,但是没有成功。

有没有办法像上面的 UTF-8 那样指定一些文件编码,或者可以用另一种自动化方式以某种方式解决问题?

Out-File Windows PowerShell 5.1 中的默认值是 Unicode,在 PowerShell Core 7+ 中是 UTF8。

您正在使用 mkdir 创建一个我无法理解的文件。

无论如何都不需要预先创建文件。所以,结合以上可能是这样的:

@"
show_channel_urls: true
allow_other_channels: false
report_errors: false
remote_read_timeout_secs: 120
"@ | Out-File  C:\ProgramData\conda\.condarc

@"
[global]
index = https://xxxx/nexus/repository/xxxx/pypi
index-url = https://xxxx:8443/nexus/repository/xxxx/simple
trusted-host = xxxx:8443
"@ | Out-File C:\ProgramData\pip\pip.ini

Windows Powershell中,重定向运算符使用Unicode编码(换句话说,UTF-16 with the little-endian byte order.) 这就是为什么你会看到这样 奇怪的 文件内容的原因。要么

  • 运行 您的代码来自 PowerShell Core (pwsh.exe),或
  • 使用 Out-File cmdlet 及其 Encoding 参数而不是 > 重定向器。

但是,请注意 Windows PowerShell 倾向于使用以下代码片段添加 UTF-8 byte order mark

$MyRawString = @"
[global]
index = https://xxxx/nexus/repository/xxxx/pypi
index-url = https://xxxx:8443/nexus/repository/xxxx/simple
trusted-host = xxxx:8443
"@
$MyPath = "C:\ProgramData\pip\pip.ini"
$MyRawString | Out-file -FilePath $MyPath -Encoding utf8

Windows PowerShell 的解决方案。使用 .NET's UTF8Encoding class and passing $False to the constructor seems to work (stolen from this M. Dudley's answer):

$Utf8NoBomEncoding = New-Object System.Text.UTF8Encoding $False
[System.IO.File]::WriteAllLines($MyPath, $MyRawString, $Utf8NoBomEncoding)

说明Get-Help About_Redirection

  • Windows PowerShell (powershell.exe)

When you are writing to files, the redirection operators use Unicode encoding. If the file has a different encoding, the output might not be formatted correctly. To redirect content to non-Unicode files, use the Out-File cmdlet with its Encoding parameter.

  • PowerShell Corepwsh.exe,版本 6+)

When you are writing to files, the redirection operators use UTF8NoBOM encoding. If the file has a different encoding, the output might not be formatted correctly. To write to files with a different encoding, use the Out-File cmdlet with its Encoding parameter.

注意 PowersShell 5.1 的 online version of the About_Redirection Help topic 中有一个 错误