什么是 Set-PSRepository 作用域?

What is Set-PSRepository scope effect?

正在执行

Set-PSRepository -Name 'PSGallery' -InstallationPolicy Trusted

在 Powershell 5 会话中,

问题

(一般来说,我不清楚如何并排安装 PowerShell 5 和 PowerShell Core 7.x.x 如何在 5->7 和 7->5 方向交叉影响)

Set-PSRepository and Register-PSRepository 的文档中所述,任何更改都是持久的,并且在用户范围内。它们适用于 所有 版本的 PowerShell,其中包括不同的 版本 。这意味着 PowerShell Core 6.x/PowerShell 7.x 也会看到同一用户的这些更改,即使在进行更改时并未安装。这也扩展到您 change/register.

的任何其他 PS 存储库

我已经启动了一个测试实例并独立验证了这一点:

  • Windows PowerShell 5.1
PS C:\Users\TestUser01> Get-CimInstance -Query 'SELECT Caption,Version FROM Win32_OperatingSystem' | select Caption,Version

Caption                              Version
-------                              -------
Microsoft Windows 10 Enterprise LTSC 10.0.17763


PS C:\Users\TestUser01> $PSVersionTable.PSVersion.ToString()
5.1.17763.1490
PS C:\Users\TestUser01> $null -eq (gcm pwsh -ea:ig)
True
PS C:\Users\TestUser01> Get-PSRepository

Name                      InstallationPolicy   SourceLocation
----                      ------------------   --------------
PSGallery                 Untrusted            https://www.powershellgallery.com/api/v2


PS C:\Users\TestUser01> Set-PSRepository PSGallery -in Trusted
PS C:\Users\TestUser01> Register-PSRepository ExampleRepo -so 'http://example.com'
PS C:\Users\TestUser01> Get-PSRepository

Name                      InstallationPolicy   SourceLocation
----                      ------------------   --------------
PSGallery                 Trusted              https://www.powershellgallery.com/api/v2
ExampleRepo               Untrusted            http://example.com/
  • 已安装 PowerShell 7.1.2 x64
PS C:\Users\TestUser01> $PSVersionTable.PSVersion.ToString()
7.1.2
PS C:\Users\TestUser01> Get-PSRepository

Name                      InstallationPolicy   SourceLocation
----                      ------------------   --------------
PSGallery                 Trusted              https://www.powershellgallery.com/api/v2
ExampleRepo               Untrusted            http://example.com/