无法将脚本发布至PowerShell Gallery,报错求助
无法将脚本发布至PowerShell Gallery的问题
执行的发布命令
Publish-Script -Path "path-to-script.ps1" -NuGetApiKey 123456789
不同PowerShell版本的报错信息
PowerShell 7.3报错
Write-Error: Failed to generate the compressed file for script 'C:\Program Files\dotnet\dotnet.exe failed to pack: error '.
PowerShell 5.1报错
Publish-PSArtifactUtility : Failed to generate the compressed file for script 'C:\Program Files\dotnet\dotnet.exe failed to pack: error '. At C:\Program Files\WindowsPowerShell\Modules\PowerShellGet\2.2.5\PSModule.psm1:11338 char:17 + ... Publish-PSArtifactUtility @PublishPSArtifactUtility_Param ... + ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ + CategoryInfo : InvalidOperation: (:) [Write-Error], WriteErrorException + FullyQualifiedErrorId : FailedToCreateCompressedScript,Publish-PSArtifactUtility
问题背景
- 脚本无依赖,此前已成功发布至少6次
- 问题持续两周,将脚本和API密钥发给国外朋友测试,出现相同错误
- 已尝试操作:重置API密钥、以管理员身份运行PowerShell,均未解决
更新:修改PowerShellGet的.NET版本后仍报错
安装.NET 7 x64运行时后,在PowerShell 5.1执行以下命令:
# 查找包含错误.NET版本的文件 $path = Get-ChildItem (Get-Module PowerShellGet -ListAvailable).ModuleBase -Recurse -File | Select-String -Pattern netcoreapp2.0 | ForEach-Object Path # 卸载模块 Remove-Module PowerShellGet -Verbose -Force -EA 0 # 更新文件内容 $path | ForEach-Object { (Get-Content -LiteralPath $_ -Raw).Replace('netcoreapp2.0', 'net7') | Set-Content $_ } Import-Module PowerShellGet -Force -Verbose # 尝试发布
执行后仍得到相同的PowerShell 5.1报错。
解决建议
验证dotnet命令可用性
- 打开PowerShell/命令提示符,直接执行
dotnet --version,确认dotnet能正常运行无报错。若dotnet本身故障,会导致打包失败。 - 检查系统环境变量
PATH是否包含C:\Program Files\dotnet\,确保dotnet可被全局调用。
- 打开PowerShell/命令提示符,直接执行
升级PowerShellGet模块
当前使用的2.2.5版本可能存在兼容性问题,尝试升级到最新版:# PowerShell 5.1中执行 Install-Module -Name PowerShellGet -Force -AllowClobber升级后重启PowerShell再尝试发布。
手动打包脚本再发布
跳过PowerShellGet自动打包步骤,手动生成nupkg文件后发布:- 下载安装NuGet CLI并添加到系统PATH;
- 为脚本创建符合PowerShell Gallery规范的nuspec文件,执行
nuget pack YourScript.nuspec -OutputDirectory .生成包; - 用
Publish-Script -Path "YourScript.nupkg" -NuGetApiKey 123456789发布已打包的文件。
检查脚本路径与权限
- 将脚本从
C:\Program Files这类高权限目录复制到桌面等普通目录再尝试发布; - 确保脚本文件名仅含英文字符、数字,避免空格或特殊符号。
- 将脚本从
确认PowerShell Gallery服务状态
虽然报错为本地打包问题,但偶尔Gallery后端故障也可能影响流程,可查看官方状态页面确认服务正常。
内容的提问来源于stack exchange,提问作者user20682592
相关产品推荐
相关产品推荐

