【问题标题】:What is Set-PSRepository scope effect?什么是 Set-PSRepository 范围效应?
【发布时间】:2021-02-26 11:51:48
【问题描述】:

执行

Set-PSRepository -Name 'PSGallery' -InstallationPolicy Trusted

在 Powershell 5 会话中,

问题

  • 其效果是否会在后续的新会话中保留?
  • 如果稍后我安装 Powershell Core 7.x.x,这些会话会受到影响吗?

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

【问题讨论】:

  • 存储库是每个用户的设置并且是持久的。不知道不同的 PS 版本,但可能不是。您可以使用Get-PSRepository 测试自己
  • 在 Powershell 5 中运行命令,然后从该存储库安装一个包。转到 Powershell 7,然后尝试相同的命令。如果它有效,那么你就有了答案。如果它不起作用,你也有你的答案。这两个问题。
  • @WaitingForGuacamole:没那么简单……有很多用例,关于效果的方向和事件的时间线。例如拥有 PS 5,然后 Set-PSRepository,然后安装 PS 7,-或- 拥有 PS 5,然后安装 PS 7,然后 Set-PSRepository 7,-或- 将其安装在 PS 7 中,等等。如果我们想要可重现的结果(脚本的强制属性之一)必须了解我们的工具。

标签: powershell security package powershell-core powershell-7.0


【解决方案1】:

正如Set-PSRepositoryRegister-PSRepository 的文档中所述,任何更改都是持久的并且在用户范围内。它们适用于 所有 版本的 PowerShell,其中包括不同的版本。这意味着 PowerShell Core 6.x/PowerShell 7.x 也将看到同一用户的这些更改,即使在进行更改时未安装也是如此。这也扩展到您更改/注册的任何其他 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/

【讨论】:

  • 非常感谢。也许这可能很糟糕,但我没有在文档中发现这一点。当我在远程上下文中执行它时,我开始怀疑,然后它似乎不会留在下一个上下文中。
猜你喜欢
  • 2011-08-13
  • 2013-01-05
  • 2018-08-07
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-11-11
  • 2020-10-18
相关资源
最近更新 更多