Powershell Publish-Module 无法对空数组进行索引

概要

本文针对Powershell模块共享到 PowershellGallery 时出现的系统语言问题进行分析,解决方法是修改系统语言为英文,再使用Publish-Module

遇到的问题

Publish-Module -Name <modulename>
			   -NuGetApiKey <key>
			   -Verbose
  • modulename: 模块文件名
  • key: 进入官网创建key或复制已有的key

问题描述

从下面的错误定位到对应文件夹下Publish-PSArtifactUtility函数,经调试发现,错误输出中 已成功创建… 这部分内容使用了正则匹配

详细信息:    C:\Users\24658\AppData\Local\Temp\74144ed8-c06a-45c4-8e7e-06ac2d7be4e6\Temp.csproj 的还原在 243.17 ms
内完成。
详细信息:    Temp ->
C:\Users\24658\AppData\Local\Temp\74144ed8-c06a-45c4-8e7e-06ac2d7be4e6\bin\Debug\netcoreapp2.0\NotUsed.dll
详细信息:    已成功创建包“C:\Users\24658\AppData\Local\Temp\198666354\psadb\psadb.1.0.1.nupkg”。
详细信息:  C:\Program Files\dotnet\sdk\3.1.100\Sdks\NuGet.Build.Tasks.Pack\build\NuGet.Build.Tasks.Pack.targets(198,5):
 warning NU5100: 程序集“csadb.dll”不在 "lib"
文件夹内,因此将包安装到项目中后,不能将该程序集添加为引用。如需引用,请将其移动到 "lib" 文件夹。
[C:\Users\24658\AppData\Local\Temp\74144ed8-c06a-45c4-8e7e-06ac2d7be4e6\Temp.csproj]
详细信息: finished running C:\Program Files\dotnet\dotnet.exe with exit code 0
Publish-PSArtifactUtility : Failed to generate the compressed file for module '无法对 Null 数组进行索引。'.
所在位置 C:\Program Files\WindowsPowerShell\Modules\PowerShellGet\2.2.3\PSModule.psm1:10946 字符: 17
+ ...             Publish-PSArtifactUtility @PublishPSArtifactUtility_Param ...
+                 ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : InvalidOperation: (:) [Write-Error],WriteErrorException
    + FullyQualifiedErrorId : FailedToCreateCompressedModule,Publish-PSArtifactUtility

PowershellGet模块脚本

找到上面模块脚本中$stdOut -match "Successfully created package '(.*.nupkg)'",这里是为了找到构建的nupkg包,但由于系统语言为中文,实际上输出为已成功创建包“<package-name>.nupkg”,导致Publish-PSArtifactUtility找不到对应的输出

Write-Verbose "finished running $($processStartInfo.FileName) with exit code $($process.ExitCode)"

if (($tempPath -ne $null) -and (Test-Path -Path $tempPath)) {
    
    
    Remove-Item -Path $tempPath -Force -Recurse
}

if (-Not ($process.ExitCode -eq 0 )) {
    
    
    # nuget writes errors to stdErr, dotnet writes them to stdOut
    if ($UseDotnetCli) {
    
    
        $errors = $stdOut
    }
    else {
    
    
        $errors = $process.StandardError.ReadToEnd()
    }
    throw "$ProcessName failed to pack: error $errors"
}

$stdOut -match "Successfully created package '(.*.nupkg)'" | Out-Null
$nupkgFullFile = $matches[1] # 报错的那一行

解决方案

  1. 将系统语言改为en-us,即英语
  2. 注销重登后,即可上传自己的模块了

注:若确定自己的模块功能是正常的,且是一些未知的错误,可以暂时注释掉上述 throw 那一行,比如dotnet编译时抛出 Index was outside the bounds of the array,这种错误并非powershell脚本错误,而可能是 NuGet 项目中已修复但未同步修复 PowershellGet 而造成的

猜你喜欢

转载自blog.csdn.net/qq_41755979/article/details/103881909