SMB文件共享上有一些模块。我们想扩展PowerShell在其中查找脚本的模块路径(暂时而非永久)。
默认情况下,$env:PSModulePath
环境变量的内容为:
> $env:PSModulePath -split ';'
C:\Users\xxx\Documents\WindowsPowerShell\Modules
C:\Program Files\WindowsPowerShell\Modules
C:\windows\system32\WindowsPowerShell\v1.0\Modules
C:\Program Files\Microsoft Monitoring Agent\Agent\PowerShell\
像documented by Microsoft一样,我想将此环境变量扩展一个或多个位置,以便调用Import-Module
甚至是#Required
标头都可以。
因此,我执行了以下命令:
$customModulePaths = @('\\myServer\myShare\subfolder\module1\',
'\\myServer\myShare\subfolder\module2\')
$env:PSModulePath = $env:PSModulePath + [System.IO.Path]::PathSeparator + $($customModulePaths -join [System.IO.Path]::PathSeparator)
脚本本身是:
\\myServer\myShare\subfolder\module1\module1.psm1
\\myServer\myShare\subfolder\module2\module2.psm1
模块名称与父文件夹相同,并且具有清单文件(.psd1)
。
我通过再次检查第一个命令来验证是否确实修改了环境变量。
接下来,我执行了Get-Module -ListAvailable -Refresh
,期望看到2个模块出现。但是,命令的输出仅引用位于原始$env:PSModulePath
文件夹中的项目。执行Import-Module module1
还会导致错误,指出找不到该模块。
我错过了什么吗?或者我想做些什么吗?还是由于我使用UNC而不是本地路径?
答案 0 :(得分:2)
查看$env:PSModulePath
中已经存在的模块路径-请注意,没有实际的模块文件夹-只有包含模块文件夹的根文件夹。
改为添加模块目录的父目录,它应该可以工作:
$env:PSModulePath = $env:PSModulePath,'\\myServer\myShare\subfolder' -join [System.IO.Path]::PathSeparator