简体   繁体   English

PowerShell 安装模块本地 vs CurrentUser

[英]PowerShell Install-Module Local vs CurrentUser

Could someone explain this please?有人可以解释一下吗?

Import-Module PSReadLine -Scope CurrentUser

Import-Module : Cannot validate argument on parameter 'Scope'.
The argument "CurrentUser" does not belong to the set "Local,Global"
specified by the ValidateSet attribute. Supply an argument that is in the
set and then try the command again.
At line:1 char:37
+     Import-Module PSReadLine -Scope CurrentUser
+                                     ~~~~~~~~~~~
    + CategoryInfo          : InvalidData: (:) [Import-Module], ParameterBindingValidationException
    + FullyQualifiedErrorId : ParameterArgumentValidationError,Microsoft.PowerShell.Commands.ImportModuleCommand

But this works fine(!)但这很好用(!)

Install-Module PSScriptAnalyzer -Scope CurrentUser

And this works fine(!)这工作正常(!)

Import-Module PSReadLine -Scope Local

The help file for Install-Module doesn't even recognise the existence of Local|Global mentioned in the error. Install-Module的帮助文件甚至无法识别错误中提到的Local|Global的存在。 -Scope <String> Specifies the installation scope of the module. The acceptable values for this parameter are AllUsers and CurrentUser.

These scopes differ substantially (excerpted from official docs.microsoft.com ):这些范围有很大不同(摘自官方docs.microsoft.com ):

Import-Module -Scope 导入模块范围

Specifies a scope into which this cmdlet imports the module .指定一个范围成此cmdlet进口模块

The acceptable values for this parameter are:此参数可接受的值为:

Global .全球 Available to all commands in the session.可用于会话中的所有命令。 Equivalent to the Global parameter.相当于全局参数。

Local .本地. Available only in the current scope.仅在当前范围内可用。

By default, when Import-Module cmdlet is called from the command prompt, script file, or scriptblock, all the commands are imported into the global session state.默认情况下,当从命令提示符、脚本文件或脚本块调用 Import-Module cmdlet 时,所有命令都会导入到全局会话状态中。 You can use the -Scope parameter with the value of Local to import module content into the script or scriptblock scope.您可以使用值为 Local 的 -Scope 参数将模块内容导入脚本或脚本块范围。

When invoked from another module, Import-Module cmdlet imports the commands in a module, including commands from nested modules, into the caller's session state.当从另一个模块调用时,Import-Module cmdlet 将模块中的命令(包括来自嵌套模块的命令)导入调用者的会话状态。 Specifying -Scope Global or -Global indicates that this cmdlet imports modules into the global session state so they are available to all commands in the session.指定 -Scope Global 或 -Global 表示此 cmdlet 将模块导入全局会话状态,以便它们可用于会话中的所有命令。

Install-Module -Scope 安装模块范围

Specifies the installation scope of the module.指定模块的安装范围 The acceptable values for this parameter are AllUsers and CurrentUser .此参数的可接受值为AllUsersCurrentUser

The AllUsers scope installs modules in a location that is accessible to all users of the computer: AllUsers范围将模块安装在计算机的所有用户都可以访问的位置:

 $env:ProgramFiles\\PowerShell\\Modules

The CurrentUser installs modules in a location that is accessible only to the current user of the computer: CurrentUser将模块安装在只有计算机当前用户才能访问的位置:

 $home\\Documents\\PowerShell\\Modules

When no Scope is defined, the default is set based on the PowerShellGet version.未定义 Scope 时,将根据 PowerShellGet 版本设置默认值。

  • In PowerShellGet versions 2.0.0 and above, the default is CurrentUser, which does not require elevation for install.在 PowerShellGet 2.0.0 及以上版本中,默认为 CurrentUser,安装时不需要提升。
  • In PowerShellGet 1.x versions, the default is AllUsers, which requires elevation for install.在 PowerShellGet 1.x 版本中,默认为 AllUsers,安装需要提升。

For me and those who face similar issue, I had to add curly brackets "{}" around CurrentUser.对于我和那些面临类似问题的人,我必须在 CurrentUser 周围添加大括号“{}”。 Else nothing happened.别的什么都没发生。

Example: Install-Module -Name SimplySql -Scope {CurrentUser}示例:Install-Module -Name SimplySql -Scope {CurrentUser}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM