如何从Publish-Module中捕获异常?

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了如何从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

以上是关于如何从Publish-Module中捕获异常?的主要内容,如果未能解决你的问题,请参考以下文章

如何从 wxPython 应用程序中捕获所有异常?

如何从 MongoDB 中正确捕获异常?

如何从 http.request() 中正确捕获异常?

如何捕获从 iframe 内部抛出的异常?

如何在Qt中捕获异常?

如何从 Python subprocess.check_output() 捕获异常输出?