我正在通过 Octopus Deploy 将 PowerShell 模块安装到许多不同的服务器上。出于测试目的,我遵循了 Microsoft 文档 installing PowerShell Modules 的指导。
这工作正常,但正如文档所述,我的更改仅对当前 session 可见。也就是说,如果我要执行以下操作:
$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)
在触手服务器上自动运行此安装程序时, future 的 PowerShell session 似乎看不到新安装的模块。
如何以与配置文件无关的方式安装 PowerShell 模块,以便启动的每个 PowerShell session 都可以看到它?
最佳答案
PowerShell 只能“查看”安装在 $env:PSModulePath
中列出的目录之一中的模块。否则,您必须使用完整路径导入模块。
要使新模块对所有用户可见,您基本上有两个选择:
C:\Windows\system32\WindowsPowerShell\v1.0\Modules
)。 PSModulePath
变量已经包含您的自定义模块目录(例如,通过 group policy preference )。 不过,后者仅对在修改后启动的 PowerShell session 有效。
关于powershell - 为所有用户持久安装 PowerShell 模块,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/20885398/