powershell - 如何从 Publish-Module 中捕获异常?
问题描述
我正在使用 Publish-Module,其中一个模块的 psd1 文件错误。PowerShell 按预期抛出异常。对 Publish-Module 的调用位于 try 块中,但 catch 块中的错误处理代码从未运行过。似乎未捕获此错误。
在我发布的模块已存在于存储库中的同一 PowerShell 代码中,还会发生另一个错误。当该错误发生时,catch 块中的代码运行并处理异常。第一个异常会导致绕过 catch 块有什么不同吗?
代码片段:
try {
Publish-Module -Path .\$moduleName -Repository MyRepo -NuGetApiKey ghehdue
"Module $moduleName published."
}
catch {
if ($_.Exception.Message -ilike "*cannot be published as the current version*is already available in the repository*") {
"The latest version of module $moduleName already exists in the repository."
}
else {
$exitCode += 1
Write-Error $_
}
}
}
未捕获的错误
Microsoft.PowerShell.Core\Test-ModuleManifest : The module manifest 'J:\Builds\
Jenkins\PROJECT_2456764.0\Applications\ALM\PSModules\MyCompany.Build\MyCompany.B
uild.psd1' could not be processed because it is not a valid Windows PowerShell
restricted language file. Remove the elements that are not permitted by the
restricted language:
At J:\Builds\Jenkins\PROJECT_2456764.0\Applications\ALM\PSModules\MyCompany.Bui
ld\MyCompany.Build.psd1:13 char:9
+ GUID = 'ccaa548f-8194-4cfa-a659-260f6ddc556b'
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Unexpected token 'ccaa548f-8194-4cfa-a659-260f6ddc556b'
# Author of this module
Author = 'MyCompany'
# Company or vendor of this module
CompanyName = 'MyCompany' in expression or statement.
At J:\Builds\Jenkins\PROJECT_2456764.0\Applications\ALM\PSModules\MyCompany.Bui
ld\MyCompany.Build.psd1:13 char:9
+ GUID = 'ccaa548f-8194-4cfa-a659-260f6ddc556b'
+ ~
The hash literal was incomplete.
At J:\Builds\Jenkins\PROJECT_2456764.0\Applications\ALM\PSModules\MyCompany.Bui
ld\MyCompany.Build.psd1:19 char:25
+ CompanyName = 'MyCompany, Inc.'
+ ~
Missing argument in parameter list.
At J:\Builds\Jenkins\PROJECT_2456764.0\Applications\ALM\PSModules\MyCompany.Bui
ld\MyCompany.Build.psd1:118 char:1
+ }
+ ~
Unexpected token '}' in expression or statement.
At C:\Program Files
(x86)\WindowsPowerShell\Modules\PowerShellGet\1.0.0.1\PSModule.psm1:989 char:27
+ ... $module = Microsoft.PowerShell.Core\Test-ModuleManifest -Path $mani ...
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : ResourceUnavailable: (J:\Builds\Jenki...Quip.Bui
ld.psd1:String) [Test-ModuleManifest], MissingMemberException
+ FullyQualifiedErrorId : Modules_InvalidManifest,Microsoft.PowerShell.Com
mands.TestModuleManifestCommand
捕获的错误
publish-module : The module 'DqCryptography' with version '1.0.2' cannot be published as the current version '1.0.2' is already available in the repository 'http://usas26:8624/nuget/PROJECTPowerShell/'.
At line:1 char:1
+ publish-module -Path DqCryptography -Repository PROJECTPowerShell - ...
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : InvalidOperation: (:) [Publish-Module], InvalidOperationException
+ FullyQualifiedErrorId : ModuleVersionIsAlreadyAvailableInTheGallery,Publish-Module
解决方案
在 try 块内向 Publish-Module 添加错误操作
Publish-Module -Path .\$moduleName -Repository MyRepo -NuGetApiKey ghehdue -ErrorAction Stop
推荐阅读
- reactjs - 关于 React 18 SSR(在 Amazon S3 上)
- android - 如何通过在颤动中点击电话号码来显示选择电话应用程序的选项?
- laravel - 在 Laravel 8 中获取所有后代
- flutter - 如何使用 audio_service 在 Android 上显示当前正在播放的音频?
- seaborn - 由于该方法不带参数,如何更改热图的标题?
- python-3.x - 如何为 win32evtlogutil.ReportEvent() 正确定义 eventID
- node.js - VS 代码扩展 - 如何设置 NODE_EXTRA_CA_CERTS?
- powershell - 为什么 PowerShell 中的 WSL2 导入命令输出“访问被拒绝”?
- java - 如何将 Object 类型转换为 HashMap
- go - 堆栈跟踪中的这些数字是什么意思?