PowerShell Install-Module Local与CurrentUser [英] PowerShell Install-Module Local vs CurrentUser

查看:160
本文介绍了PowerShell Install-Module Local与CurrentUser的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有人可以解释一下吗?

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

但这很好用(!)

Install-Module PSScriptAnalyzer -Scope CurrentUser

这很好(!)

Import-Module PSReadLine -Scope Local

安装模块的帮助文件甚至无法识别错误中提到的 Local | Global 的存在. -Scope< String>指定模块的安装范围.此参数可接受的值为AllUsers和CurrentUser.

The help file for Install-Module doesn't even recognise the existence of Local|Global mentioned in the error. -Scope <String> Specifies the installation scope of the module. The acceptable values for this parameter are AllUsers and CurrentUser.

推荐答案

这些作用域存在很大差异(摘录自官方 docs.microsoft.com ):

These scopes differ substantially (excerpted from official docs.microsoft.com):

导入模块-范围

Import-Module -Scope

指定 范围该cmdlet导入模块的地方.

Specifies a scope into which this cmdlet imports the module.

此参数可接受的值为:

全局.可用于会话中的所有命令.等效于Global参数.

Global. Available to all commands in the session. Equivalent to the Global parameter.

本地.仅在当前范围内可用.

Local. Available only in the current scope.

默认情况下,从命令中调用Import-Module cmdlet时提示符,脚本文件或脚本块,所有命令均已导入进入全局会话状态.您可以将-Scope参数与将模块内容导入脚本的Local的值,或者脚本块范围.

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. You can use the -Scope parameter with the value of Local to import module content into the script or scriptblock scope.

从另一个模块调用时,Import-Module cmdlet会导入模块中的命令(包括来自嵌套模块的命令)呼叫者的会话状态.指定-Scope Global或-Global表示该cmdlet会将模块导入到全局会话状态中,因此它们可用于会话中的所有命令.

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. 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.

安装模块-范围

Install-Module -Scope

指定模块的安装范围.可以接受的此参数的值为 AllUsers CurrentUser .

Specifies the installation scope of the module. The acceptable values for this parameter are AllUsers and CurrentUser.

AllUsers 范围将模块安装在以下位置计算机的所有用户均可访问:

The AllUsers scope installs modules in a location that is accessible to all users of the computer:

$env:ProgramFiles\PowerShell\Modules

CurrentUser 将模块安装在可访问的位置仅针对计算机的当前用户:

The CurrentUser installs modules in a location that is accessible only to the current user of the computer:

$home\Documents\PowerShell\Modules

如果未定义范围,则默认设置为PowerShellGet版本.

When no Scope is defined, the default is set based on the PowerShellGet version.

  • 在PowerShellGet 2.0.0及更高版本中,默认值为CurrentUser,它不需要提升安装.
  • 在PowerShellGet 1.x版本中,默认值为AllUsers,需要提升安装权限.

这篇关于PowerShell Install-Module Local与CurrentUser的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆