首页 > 解决方案 > 如何从 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

标签: powershell

解决方案


在 try 块内向 Publish-Module 添加错误操作

Publish-Module -Path .\$moduleName -Repository MyRepo -NuGetApiKey ghehdue -ErrorAction Stop

推荐阅读