持续为所有用户安装PowerShell模块 [英] Installing PowerShell module persistently for all users
问题描述
我正在通过 Octopus部署将PowerShell模块安装到许多不同的服务器上.出于测试目的,我使用了Microsoft文档的
I'm installing a PowerShell module via Octopus Deploy onto a number of different servers. For testing purposes, I went with the guidance of Microsoft's documentation for installing PowerShell Modules.
这很好,但是如文档所述,我的更改仅在当前会话中可见.也就是说,如果我要执行以下操作:
This worked fine, but as the documentation stated, my changes would be visible only for the current session. That is, if I were to do the following:
$modulePath = [Environment]::GetEnvironmentVariable("PSModulePath", [EnvironmentVariableTarget]::Machine)
# More practically, this would be some logic to install only if not present
$modulePath += ";C:\CustomModules"
[Environment]::SetEnvironmentVariable("PSModulePath", $modulePath, [EnvironmentVariableTarget]::Machine)
在触手服务器上自动运行此安装程序时,以后的PowerShell会话似乎看不到新安装的模块.
When running this installer automatically on tentacle servers, future PowerShell sessions do not appear to see the newly installed modules.
如何以与配置文件无关的方式安装PowerShell模块,以便每个启动的PowerShell会话都能看到它?
How can I install a PowerShell module in a profile agnostic way so that every PowerShell session started can see it?
推荐答案
PowerShell只能查看"安装在$env:PSModulePath
中列出的目录之一中的模块.否则,您必须导入具有完整路径的模块.
PowerShell can only "see" modules installed in one of the directories listed in $env:PSModulePath
. Otherwise you'll have to import the module with its full path.
要使新模块对所有用户可见,您基本上有两个选择:
To make a new module visible to all users you basically have two options:
- 将模块安装到默认的系统级模块目录(
C:\Windows\system32\WindowsPowerShell\v1.0\Modules
). - 修改系统环境,以便
PSModulePath
变量已包含您的自定义模块目录(例如,通过组政策首选项).
- Install the module to the default system-wide module directory (
C:\Windows\system32\WindowsPowerShell\v1.0\Modules
). - Modify the system environment so that
PSModulePath
variable already contains your custom module directory (e.g. via a group policy preference).
但是,后者仅对进行修改后启动的PowerShell会话有效.
The latter will only become effective for PowerShell sessions started after the modification was made, though.
这篇关于持续为所有用户安装PowerShell模块的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!