【问题标题】:How to publish .nupkg to Powershell Gallery?如何将 .nupkg 发布到 Powershell Gallery?
【发布时间】:2021-05-20 20:37:14
【问题描述】:

解决方案

感谢mklement0

这是我最终将我的 bin\MyModule.0.1.0.nupkg 模块发布到 PSGallery 的操作:

  1. bin\MyModule.0.1.0.nupkg 重命名为bin\MyModule.0.1.0.zip
  2. 提取bin\MyModule.0.1.0.zipbin\MyModule\(提取的文件夹的名称必须与里面的.psd1 文件的名称匹配,所以在我的例子中,现在有一个bin\MyModule\MyModule.psd1 文件)。
  3. 从提取的文件夹中删除[Content_Types].xml文件(当您尝试从PSGallery下载和安装模块时,不删除此文件会导致错误,这似乎是由生成的.nupkg中存在该文件的2个副本引起的) .

然后我就可以运行这个命令了:

PS> Publish-Module -NuGetApiKey xxx -Path .\bin\MyModule

它奏效了!我的模块现已发布到 PSGallery。

原帖

我有一个由Autorest.Powershell 生成并打包为 .nupkg 文件的 Powershell 模块。我现在想将我的模块发布到Powershell Gallery,但不知道如何让它工作。 我试过了:

PS> Publish-Module -NuGetApiKey xxx -Path .\bin\MyModule.0.1.0.nupkg
Publish-Module: The specified path '.\bin\MyModule.0.1.0.nupkg' is not a valid directory.

PS> Publish-Module -NuGetApiKey xxx -Path .\bin\
Publish-Module: The specified module with path '.\bin' was not published because no valid module was found with that path.

PS> Publish-Module -NuGetApiKey xxx -Name .\bin\MyModule.0.1.0.nupkg
Get-Module: C:\Users\bport\Documents\PowerShell\Modules\PowerShellGet\2.2.5\PSModule.psm1:10630
 Line |
10630 |  …   $module = Microsoft.PowerShell.Core\Get-Module -ListAvailable -Name …
      |                ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
      | The specified module 'D:\workarea\bin\MyModule.0.1.0.nupkg'
      | was not found. Update the Name parameter to point to a valid path, and then try again.

Publish-Module: The specified module '.\bin\MyModule.0.1.0.nupkg' was not published because no module with that name was found in any module directory.

PS> dotnet nuget push .\bin\MyModule.0.1.0.nupkg --api-key xxx --source https://www.powershellgallery.com/api/v2
Pushing MyModule.0.1.0.nupkg to 'https://www.powershellgallery.com/api/v2'...
  PUT https://www.powershellgallery.com/api/v2/
  MethodNotAllowed https://www.powershellgallery.com/api/v2/ 580ms
error: Response status code does not indicate success: 405 (Method Not Allowed).

应该可以将 .nupkg 文件发布到 Powershell Gallery,因为您可以从 Powershell Gallery 下载包为 .nupkg 文件。此外,Autorest 文档中提到了 this 关于其生成的 nupkg 文件:

.nupkg 的结构已创建,因此它可以作为 PSRepository 的一部分加载。此外,此软件包的格式可以分发到 PSGallery。

我猜这是可能的,但我就是不知道该怎么做。任何帮助将不胜感激

【问题讨论】:

  • 我相信您尝试使用 Publish-Module 执行此操作的方式要求不要打包模块。它会为你做包装。

标签: powershell autorest powershellget


【解决方案1】:

PowerShellGet 模块的 2.2.5 版开始 - PowerShell 7.2 预览版附带的一个 - 用于将 PowerShell 模块发布到 PowerShell GalleryPublish-Module cmdlet 支持上传.nupkg文件。

相反,它需要一个(未压缩的)目录,其中包含要发布的 PowerShell 模块[1]然后压缩成幕后的(临时)NuGet 包(.nupkg 存档)并上传到库。

我不熟悉 AutoRest.PowerShell,但如果它声称它创建的 .nupkg 档案与 PowerShell 库兼容,您可以尝试提取(解压缩)这样的档案 ( NuGet 文件是 ZIP 存档,因此您可以将它们传递给 Expand-Archive)到一个目录中,并尝试将该目录的路径传递给 Publish-Module-Path 参数(选择包含模块清单文件的目录 (.psd1 ),它可能是一个子文件夹)。


[1] 如果要发布的模块位于$env:PSModulePath 中列出的目录之一,则其目录位置可以从模块的名称 中推断出来,并传递给@987654334 @参数。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多