从Azure DevOps私有源安装PowerShell模块失败求助
问题:从Azure DevOps私有源安装PowerShell模块时提示找不到psd1文件
我已按照官方指南在Azure DevOps源中发布了PowerShell模块MyModule,本地也配置了PAT令牌和名为MyRepo的受信任私有PSRepository。执行Find-Module -Name MyModule -Repository MyRepo能正常找到模块,但通过管道执行Install-Module时出现错误:
Install-Package: Cannot find the path 'C:\Users\Me\AppData\Local\Temp\355836562\MyModule.0.0.2\MyModule.psd1' because it does not exist.
详细执行日志
VERBOSE: Downloading module 'MyModule' with version '0.0.2' from the repository 'https://pkgs.dev.azure.com/MyOrg/MyProject/_packaging/MyModule/nuget/v2'. VERBOSE: Searching repository 'https://pkgs.dev.azure.com/MyOrg/MyProject/_packaging/MyModule/nuget/v2/FindPackagesById()?id='MyModule'' for ''. VERBOSE: InstallPackage' - name='MyModule', version='0.0.2',destination='C:\Users\Me\AppData\Local\Temp\1414719836' VERBOSE: DownloadPackage' - name='MyModule', version='0.0.2',destination='C:\Users\Ax\AppData\Local\Temp\1414719836\MyModule.0.0.2\MyModule.0.0.2.nupkg', uri='https://pkgs.dev.azure.com/MyOrg/MyProject/_packaging/MyModule/nuget/v2?id=mymodule&version=0.0.2' VERBOSE: Downloading 'https://pkgs.dev.azure.com/MyOrg/MyProject/_packaging/MyModule/nuget/v2?id=mymodule&version=0.0.2'. VERBOSE: Completed downloading 'https://pkgs.dev.azure.com/MyOrg/MyProject/_packaging/MyModule/nuget/v2?id=mymodule&version=0.0.2'. VERBOSE: Completed downloading 'MyModule'. VERBOSE: Hash not found for the package 'MyModule'. VERBOSE: InstallPackageLocal' - name='MyModule', version='0.0.2',destination='C:\Users\Me\AppData\Local\Temp\1414719836' VERBOSE: Validating the 'MyModule' module contents under 'C:\Users\Me\AppData\Local\Temp\1414719836\MyModule.0.0.2' path. Install-Package: Cannot find the path 'C:\Users\Me\AppData\Local\Temp\1414719836\MyModule.0.0.2\MyModule.psd1' because it does not exist.
这个模块发布到PowerShell Gallery后能正常安装加载,我怀疑问题可能和文件夹名称里的版本号有关,但不确定。有没有人遇到过类似问题?
环境信息
PS> $PSVersionTable Name Value ---- ----- PSVersion 7.3.7 PSEdition Core GitCommitId 7.3.7 OS Microsoft Windows 10.0.22621 Platform Win32NT PSCompatibleVersions {1.0, 2.0, 3.0, 4.0…} PSRemotingProtocolVersion 2.3 SerializationVersion 1.1.0.1 WSManStackVersion 3.0
PS> Get-Module powershellget -ListAvailable Directory: C:\program files\powershell\7\Modules ModuleType Version PreRelease Name PSEdition ExportedCommands ---------- ------- ---------- ---- --------- ---------------- Script 2.2.5 PowerShellGet Desk {Find-Command, Find-DSCResource, Find-M…
解决方案
1. 检查Azure DevOps中模块包的结构
从Azure DevOps下载对应的MyModule.0.0.2.nupkg包,解压后查看内部结构:
- 正确的PowerShell模块nupkg结构应该是:
包根目录/模块名/模块名.psd1(比如MyModule/MyModule.psd1) - 如果解压后看到的是
MyModule.0.0.2/MyModule.psd1或者psd1文件在其他子目录,说明打包时的路径配置错误,导致Install-Module找不到文件。
2. 修正模块打包方式
如果是用Publish-Module发布到Azure DevOps:
- 确保发布时指定的
-Path参数指向模块的根目录(即包含MyModule.psd1的文件夹),而不是上级目录。 - 示例:
Publish-Module -Path .\MyModule -Repository MyRepo -NuGetApiKey $pat
如果是用nuget手动打包:
- 检查nuspec文件中的
<files>节点,确保将模块文件正确打包到与包ID同名的文件夹下,示例:<files> <file src="MyModule\**" target="MyModule" /> </files>
3. 验证临时文件结构
安装失败后,不要关闭PowerShell或删除临时文件夹,手动打开C:\Users\Me\AppData\Local\Temp\1414719836\MyModule.0.0.2目录,查看里面的文件分布:
- 如果psd1文件存在但在子文件夹中,说明打包结构错误;如果确实不存在,可能是发布时漏传了文件。
4. 升级PowerShellGet版本
当前使用的PowerShellGet 2.2.5对Azure DevOps源的处理可能存在兼容性问题,尝试升级到最新的2.x版本或3.x版本:
Install-Module PowerShellGet -Force -AllowClobber
内容的提问来源于stack exchange,提问作者Axel Andersen
相关产品推荐
相关产品推荐

